Observer Design Pattern:
Now we starting Observer. The definition of this design pattern is that "The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems."
Now i am not defining you the Observer definition. I know it is little bit tricky or complex. But here we start code and after that you easily learn the definition of Observer.
Step1:
Create New Java Project.
Now first i thing i define little bit about our scenario. Here we create a project like for some factory or building here we use the temperature sensors. Now we want when any where in the building fire takes place. We want to inform to Fire department, Police Department, Emergency services department or any other departments which you want you can add. Now here we use the Observer Patterns. Now start code. Yeh!
Step2:
Now here first we make two packages.
1. com.interfaces
2. com.main ( Describe in Step3)
com.interfaces:
Here we make two interfaces with Name Observer and Subject.
Here Observer mean any thing which want to know if any thing happen. Like fire department want to know when fire takes place in a building. And Subject or Observable mean any thing for which Observer want to know some event occur like Building is Subject or Observable for fire event.
Now see code.
Observer:
Here fireFire() is only one method. And i describe this method later.
Subject or Observable:
Here we have 3 methods. Now this interface we use on Subject or Observable like we implement this interface on Building class later.
And same we implement Observer interface on every class which want to know if fire take place in building. So we use this interface in Police,Fire,EmergencyServices departments respectively. And i describe this later.
Step3:
com.main:
Here "O" means are those classes which implement the Observer interface and "S" means the Class which is Subject or Observable.
Now here i only describe only one Observer class other 2 are same.
Like PoliceDepartment.Java:
Due to Observer interface here we implement fireFire() method. And all those things which we require from Police department at when the fire takes place in a building we write here. Same in other two classes.
Now Subject means Building class:
1. mRegisterForFireEvent = new Vector<Observer>();
We use the vector class to save our all observers means all those classes which want to know if fire take place in a building.
2. public void registerObserver(Observer o) {
mRegisterForFireEvent.add(o);
}
Only add the Observer object in our vector list.
3. public void removeObserver(Observer o) {
mRegisterForFireEvent.remove(o);
}
If any Observer want he did not notify if any thing happen we remove that observer. Like Policedepartment want that he did not notify when fire take place in a building than we use this method to remove PoliceDepartment from our list.
4.public void notifyToAll() {
Enumeration<Observer> observersEnumeration = mRegisterForFireEvent
.elements();
while (observersEnumeration.hasMoreElements()) {
Observer observer = (Observer) observersEnumeration.nextElement();
observer.fireFire();
}
}
This method call at that time when fire takes place in a building. And this code automatically notify to every one who is registered as an observer in our vector list.
Step4:
First we create all objects of our classes. These objects highlight with yellow color.
Now after that you see "O" where all observers (Police,Fire,EmergencyServices) are register with Building object.
Now after that where "1" take place in image when you run this code. It stop and wait for input from user. Now if you write "Fire" the notifyToAll() function call and every one who register notified.
The output shown below.
I think we grasp the Observer Pattern concept.
Now if you want you can download the complete code from https://code.google.com/p/design-pattern-observer/.
Now in which situation we use this Patterns.
1. If you want to update every employee on the basis of changing in Database. You can use this design pattern.
2. We can use this in GPS apps where our all functionality depend on GPS value change.
3. Easy formula if you feel any where in a program your scenario is one to many and many part completely depend on One part at that time this Pattern use.
4. This pattern also use in MVC pattern which we describe in future.
No comments:
Post a Comment