Wikipedia Says:
Observer Pattern is used to observe the state of an object in a program.It relates to the principle of Implicit Invocation.
Implicit invocation: System may be structured around event handling using a form of callback.It closely relates to inversion of control what is also known as Hollywood Principle(Donot call us we will call you) which means instead of programmer specifying function calls i.e a series of events to happen,
they would rather register desired responses to particular happenings.
Hence one or more Objects(Observers or listeners) are registered with Subject to raise an event.
Responsibilities of Subject:
a) Subject can add, remove and notify observers.All observers should register with Subject by means of some utilityMethod say addObserver().Similarly observers may be removed to means of any utility method like removeObserver().
b) An Observer may be notified of changes by notify() and notifyAll() which notifies all observers.
Responsibilities of Observer:
a) All subclasses of class Observer(Ideally Observer class may be an abstract class so that subclasses can define the implementation of notify() method.As you have observed Observer Pattern defines one-to-many relationship between subject-observers.
One Advantage of Observer Pattern is it decouples the observer from the subject.
Consequences Of Observer Pattern
- Unknown Updates: Observers has no knowledge about each other and update dependency can be hard to track down.
Observer Pattern In Java:
Java Provides builtin classes Observer and Observable to achieve this functionality.AWT/Swing event model uses ObserverPattern to large extend especially in event handling.
Below example shows one usage of Observer pattern.For Example we may need to send all Admin Previlage Users Profiles to our CEO in our organisation.
class NotifyAdminUsers extends Observable {
void notifyAdminUsers(EmployeeObserver employeeObserver) {
Vector<EmployeeTO> v=employeeObserver.getEmployeeList();
addObserver(employeeObserver);
for (EmployeeTO employeeTO : v) {
if(employeeTO.isAdmin) {
setChanged();
notifyObservers(employeeTO);
}
}
}
}
class EmployeeTO implements Serializable {
String name;
//.....
boolean isAdmin;
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class EmployeeObserver implements Observer {
public Vector<EmployeeTO> employeeList;
EmployeeObserver() {
employeeList=new Vector<EmployeeTO>();
}
public void addEmployee(EmployeeTO employee) {
this.employeeList.add(employee);
}
public Vector<EmployeeTO> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(Vector<EmployeeTO> employeeList) {
this.employeeList = employeeList;
}
public void update(Observable observable,Object obj) {
// Some Condition dependent on Application.
if(observable instanceof NotifyAdminUsers) {
if(obj instanceof EmployeeTO) {
System.out.println("Emp Notified:" + ((EmployeeTO)obj).getName());
// send mail to our company CEO. oooh!
// Do stuff which is applicable.
}
}
}
}
public class ObserverPattern {
public static void main(String[] args) {
// Initialize Both Observable and Observer.
NotifyAdminUsers adminUsers=new NotifyAdminUsers();
EmployeeObserver employeeObserver=new EmployeeObserver();
for(int i=1;i<=5;i++) {
EmployeeTO employeeTO=new EmployeeTO();
employeeTO.setName("Name:" + i);
employeeTO.setAdmin((i%2)==0);
employeeObserver.addEmployee(employeeTO);
}
// Notify All Admin Users.
adminUsers.notifyAdminUsers(employeeObserver);
}
}
Hope this explanation helps.
No comments:
Post a Comment