Sunday, December 9, 2012

Design Patterns : Observer Pattern 2

The LoginObservable which has a observers attribute and a currentUsers_ attribute. The former is a list of all the Observers which are registered with the system and the latter is a Map of all the Users currently active on the System.

We also have two flags changed which is essentially indicator of whether the Observable system has changed.
Let us go through the Use case methods first
/**
*
*
*
*/
public IUser login(String userName, char [] password) {
// check pss
changed = true;
IUser user = new User(userName);
if(currentUsers_ == null) {
currentUsers_ = new HashMap();
}
currentUsers_.put(userName, user);
notifyObservers(user);// notify all Observers of this user
return user;
}

Now this method logs the User in. It takes a user name and password creates a
IUser object and then updates both the current User and then the changes
inidicator. The reader can take note of the notifyObservers method
which will be discussed in details later
Somewhat on similar lines the next usecase method is written
/**
*
*
*
*
*/
public IUser logout(String userName, char [] password) {
// check pss
changed = true;
IUser user = null;
if(currentUsers_ != null) {
if(currentUsers_.containsKey(userName)) {
//double check pss
user = currentUsers_.get(userName);
currentUsers_.put(userName, null);
}
}
notifyObservers(user);// notify all Observers of this user
return user;
}


the changed indicator is once again changed the current Users is updated and once
again the notifyObservers method is called.

Next we check on the same method /**
*
*/
@Override
public void notifyObservers(Object arg) {
// TODO Auto-generated method stub
Observer [] observersArray = new Observer[observers.size()];
int count = 0;
for(Observer observer : observers) {
observersArray[count++] = observer;
}

if(this.hasChanged()) {
for(Observer observer : observersArray) {
observer.update(this, arg);
}
}
clearChanged();
observersArray = null;
}
It simply creates an Observer array out of the Observer list and checks
if the Observable has changed .
Then for every element of the array the update method is called.


Finally the clearChanged is called to reset the status to
not changed and the array is freed.

Last we need a Test class and a test script to see the program in action
package org.home.project21.study.designPatterns;

import java.util.Observable;
import java.util.Observer;

public class LoginTester {

/**
* @param args
*/
public static void main(String[] args) {
ILogUser logUser = new LoginObservable(); // want to check who logs in and why
Observer observer = new LoginObserver(); // create an Observer
((Observable)logUser).addObserver(observer); // add it
logUser.login("Alan Stair", "123".toCharArray()); // log in an Observer
logUser.logout("Alan Stair", "123".toCharArray());// log out an Observer
}


}
And the script

javac -sourcepath src -classpath bin -d bin C:\work\workspace\Project21\src\org\home\project21\study\designPatterns\IUser.java
javac -sourcepath src -classpath bin -d bin C:\work\workspace\Project21\src\org\home\project21\study\designPatterns\ILogUser.java
javac -sourcepath src -classpath bin -d bin C:\work\workspace\Project21\src\org\home\project21\study\designPatterns\User.java
javac -sourcepath src -classpath bin -d bin C:\work\workspace\Project21\src\org\home\project21\study\designPatterns\LoginObserver.java
javac -sourcepath src -classpath bin -d bin C:\work\workspace\Project21\src\org\home\project21\study\designPatterns\LoginObservable.java
javac -sourcepath src -classpath bin -d bin C:\work\workspace\Project21\src\org\home\project21\study\designPatterns\LoginTester.java
java -classpath bin org.home.project21.study.designPatterns.LoginTester > ObserverPattern_output.txt
Finally the Output

Observer notes Alan Stair has logged In @ Sun Dec 09 22:34:21 IST 2012 sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
Observer notes Alan Stair has logged In @ Sun Dec 09 22:34:21 IST 2012 sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

No comments: