Observer Pattern also called publish-subscribe-mode, model-view-mode, source-listener-mode, or slave mode. The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified, allowing them to automatically update themselves , which is a behavioral pattern
The actors involved in the Observer pattern are:
- Abstract Theme (Subject) role: Abstract Theme The role holds all references to observer objects in a single aggregation (such as a ArrayList object), each subject can have any number of observers. Abstract topics provide an interface that can add and remove observer objects, and abstract subject roles are called Abstract Observer (Observable) roles
- specific subject (ConcreteSubject) role: The status of the State is deposited into the specific observer object, and all registered observers are notified when the internal state of the specific subject changes. The specific subject role is also called the specific Observer (concrete Observable) role
- abstract Observer (Observer) Role: defines an interface for all specific observers, updating themselves when the topic is notified, an interface called the update interface
- specific observer (concreteobserver) role: stores the state of the subject with the status itself. The specific observer role implements the update interface required by the abstract observer role in order to reconcile the state of itself with the state of the subject. If desired, the specific observer role can maintain a reference to a specific Subject object
defining abstract Theme Roles
PackageCom.observerpattern;/** * @authorYyx October 10, 2017*/ Public InterfaceSubject {/*** Add Observer Object * *@paramObserver*/ Public voidaddobserver (Observer Observer); /*** Remove Observer Object * *@paramObserver*/ Public voidremoveobserver (Observer Observer); /*** Update Observer object Information*/ Public voidnotifyobservers ();}
define specific theme roles
PackageCom.observerpattern;Importjava.util.ArrayList;Importjava.util.List;/** * @authorYyx October 10, 2017*/ Public classConcreteSubjectImplementsSubject {PrivateList<observer> observerlist =NewArraylist<observer>(); @Override Public voidaddobserver (Observer Observer) {observerlist.add (Observer); } @Override Public voidremoveobserver (Observer Observer) {observerlist.remove (Observer); } @Override Public voidnotifyobservers () { for(Observer observer:observerlist) {observer.update (); } }}
defining the abstract observer role
Package Com.observerpattern; /** @author*/Publicinterface Observer { publicvoid update ();
define specific observer roles
PackageCom.observerpattern;/** * @authorYyx October 10, 2017 * Specific observer role*/ Public classConcreteobserverfirstImplementsObserver {@Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("I am the first class to implement the Observer Interface"); }} PackageCom.observerpattern;/** * @authorYyx October 10, 2017 * Specific observer role*/ Public classConcreteobserversecondImplementsObserver {@Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("I am the second class to implement the Observer Interface"); }}
Defining Test Classes
PackageCom.observerpattern;/** * @authorYyx October 10, 2017*/ Public classPatterntest { Public Static voidMain (string[] args) {Subject Subject=NewConcreteSubject (); Observer Observerfirst=NewConcreteobserverfirst (); Observer Observersecond=NewConcreteobserversecond (); Subject.addobserver (Observerfirst); Subject.addobserver (Observersecond); Subject.notifyobservers (); }} Run Result: I am the first class to implement the Observer interface I am the second class to implement the Observer interface
Java common design Patterns-observer patterns