Observer is a Behavioral Design Pattern with the following object structural:
The GoF (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) book describes this pattern as “A one‐to‐many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.”
The GoF (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) book describes this pattern as “A one‐to‐many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.”
Basically,
the observer pattern is based on a subject
and some observers:
subject - an object that changes its state
subject - an object that changes its state
observers - objects notified when the subject
has changed its state
Between
subject and observers there is a one-to-many relationship, which practically
means that a subject has one or many observers.
Observer pattern is also called as publish-subscribe pattern. Some of it’s implementations are:
- java.util.EventListener in Swing
- javax.servlet.http.HttpSessionBindingListener
- javax.servlet.http.HttpSessionAttributeListener
In plain code the observer pattern can be easily achieved via the Java implementation of the observer pattern. The observers should implement the java.util.Observer interface and the subject should extend the java.util.Observable class, as in the below example.
In our
plain code example, the subject is a
main fire station, and the observers
are local fire stations. Each time a new fire is reported to the main fire
station the local fire stations should be immediately informed. So, the subject implementation should provide at
least the following three methods:
·
a method for registering new observers (e.g. registerLocalFireStation())
·
a method for reporting fires (e.g. fireStarted())
·
a method for notifying local fire stations about the reported fires (e.g. notifyLocalFireStations())
The local fire
stations are stored in a List<Observer>:
import
java.util.ArrayList;
import
java.util.List;
import
java.util.Observable;
import
java.util.Observer;
public class
MainFireStation extends Observable {
private final List<Observer>
localFireStations = new ArrayList<>();
public void fireStarted(String address) {
notifyLocalFireStations(address);
}
public void notifyLocalFireStations(String
address) {
for (Observer o : this.localFireStations) {
o.update(this, address);
}
}
public void registerLocalFireStation(Observer
o) {
localFireStations.add(o);
}
}
Now, we
can write the observers. Below you can see three local fire stations that want
to be notified when a new fire is reported to the main fire station:
import
java.util.Observable;
import
java.util.Observer;
public class
ViningsFireStation implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("Vinings fire
department will go to " + arg);
}
}
import
java.util.Observable;
import
java.util.Observer;
public class
BrookhavenFireStation implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("Brookhaven fire
department will go to " + arg);
}
}
import
java.util.Observable;
import
java.util.Observer;
public class
DecaturFireStation implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("Decatur fire
department will go to " + arg);
}
}
Done! Now,
we can test this implementation. First, we instantiate the main fire station:
MainFireStation
mainFireStation = new MainFireStation();
Next, we
instantiate the local fire stations:
ViningsFireStation viningsFireStation = new ViningsFireStation();
DecaturFireStation decaturFireStation = new DecaturFireStation();
BrookhavenFireStation
brookhavenFireStation = new BrookhavenFireStation();
Further, the
observers registers to the subject:
mainFireStation.registerLocalFireStation(viningsFireStation);
mainFireStation.registerLocalFireStation(decaturFireStation);
mainFireStation.registerLocalFireStation(brookhavenFireStation);
Finally,
we report several fires at main fire station:
mainFireStation.fireStarted("Home
Park Atlanta, GA");
mainFireStation.fireStarted("High
Museum of Art 1280 Peachtree St NE Atlanta, GA 30309");
At this
moment the observers will be
notified. This can be seen via the below outputs provided by observers:
Vinings fire
department will go to Home Park Atlanta, GA
Decatur fire
department will go to Home Park Atlanta, GA
Brookhaven
fire department will go to Home Park Atlanta, GA
Vinings fire
department will go to High Museum of Art 1280 Peachtree St NE Atlanta, GA 30309
Decatur fire
department will go to High Museum of Art 1280 Peachtree St NE Atlanta, GA 30309
Brookhaven
fire department will go to High Museum of Art 1280 Peachtree St NE Atlanta, GA
30309
In part
two we discuss about the observer pattern in Java EE.
Niciun comentariu :
Trimiteți un comentariu