【text】
1. Definition of observer pattern:
Simply put, the observer pattern defines a one-to-many dependency, allowing one or more observer objects to listen to a topic object. In this way, when the observed state changes, the corresponding observer needs to be notified so that these observer objects can be automatically updated. For example: the event processing mechanism in the GUI adopts the observer mode.
2. Implementation of observer mode:
Subject (object interface to be observed): specifies the unified interface of ConcreteSubject; each Subject can have multiple Observers; ConcreteSubject (specific observed objects): maintains a list of references to all specific observers; when the status changes, a notification will be sent to all registered observers. Observer (observer interface): specifies the unified interface of ConcreteObserver; defines an update() method, which will be called when the state of the observed object changes. ConcreteObserver: Maintains a reference to ConcreteSubject; the specific state is synchronized with ConcreteSubject; implements the Observer interface, the function of the update() method: Once a change in the Subject is detected, the information will be updated.
The diagram is described as follows:
Note: There is a set in the observed class to maintain all observers.
3. Give examples:
[Scheme 1]: Define the interface or class yourself to implement the observer pattern.
The steps are as follows:
(1) Define the interface that is possessed by the observer:
package com.vince.observer;public interface Observable { //Register as an observer public void registerObserver(Observer observer); //Cancel the observer public void removeObserver(Observer observer); //Notify all observers to update information public void notifyObservers();}(2) Define the specific observer: cup
package com.vince.observer;import java.util.Vector;public class Cup implements Observable{ //A list of observer objects maintained by the observer private Vector<Observer> vector = new Vector<Observer>(); private float price; public Cup(float price){ this.price = price; } public float getPrice() { return price; } public void setPrice(float price) { // Notify all observers when modifying the price this.price = price; notifyObservers(); } @Override public void registerObserver(Observer observer) { //Register observer vector.add(observer); } @Override public void removeObserver(Observer observer) { //Cancel observer vector.remove(observer); } @Override public void notifyObservers() { //Implement notify all observer objects for (Observer observer:vector) { observer.update(price); } }}(3) Define the common interface that the observer has: (Updating the data is of course the final result is to be carried out with the observer)
package com.vince.observer;public interface Observer { public void update(float price);5 }(4) Define specific observer objects:
package com.vince.observer;public class Person implements Observer{ private String name; public Person(String name){ this.name = name; } @Override public void update(float price) { System.out.println(name+"The price of the cup you are following has been updated to: "+price); }}(5) Test:
package com.vince.observer;public class Test { public static void main(String[] args) { //Create an observer object Cup doll = new Cup(3000); //Create two observer objects Person p1 = new Person("Life 1"); Person p2 = new Person("Life 2"); //Register as an observer doll.registerObserver(p1); doll.registerObserver(p2); System.out.println("First round of promotion:"); doll.setPrice(2698);// Price changes System.out.println("Second Round Promotion:"); doll.setPrice(2299);// System.out.println("Second Round Promotion:"); doll.setPrice(1998); doll.removeObserver(p2); //Remove Life 2 System.out.println("Fourth Round Promotion:"); doll.setPrice(1098); }}After running, the results are displayed as follows:
[Scheme 2]: Directly call the JDK API to implement it.
The steps are as follows:
(1) Implement the specific observer object by inheriting the Observable class:
package com.vince.observer2;import java.util.Observable;public class Cup extends Observable{ private float price; public Cup(float price){ this.price = price; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; this.setChanged();//Notification, the data has changed this.notifyObservers(); } }(2) Implement specific observer objects by implementing the java.util.Observer interface:
package com.vince.observer2;import java.util.Observable;import java.util.Observer;public class Person implements Observer{ private String name; public Person(String name){ this.name = name; } @Override public void update(Observable o, Object arg) { if(o instanceof Cup){ Cup cup = (Cup)o; System.out.println(name+"The price of the cup you are following has been updated to: "+cup.getPrice()); } }}(3) Test:
package com.vince.observer2;public class Test { public static void main(String[] args) { Cup cup = new Cup(3000); Person p1 = new Person("Life 1"); Person p2 = new Person("Life 2"); cup.addObserver(p1); cup.addObserver(p2); System.out.println("First round of promotion"); cup.setPrice(2988); System.out.println("Second round of promotion"); cup.setPrice(2698); cup.deleteObserver(p2); System.out.println("third round of promotion"); cup.setPrice(1998); }}After running, the results are as follows:
【Project Documents】
Link: http://xiazai.VeVB.COM/201609/yuanma/JavaSEguancha(VeVB.COM).rar
4. Summary: (The role of the observer mode)
The observer pattern creates an abstract coupling between the observer and the observer. All the observer characters know is only a specific list of observers.
Since the observer and the observer are not closely coupled together, they can belong to different levels of abstraction. If both the observer and the observer are thrown together, then the object must cross the levels of abstraction and concretization.
Observer mode supports broadcast communication. The observer will issue a notice to all registered observers.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.