When the state equivalence of one object changes, other objects need to be informed, which requires the observer mode. One observed person can correspond to multiple observers.
In the following example, there are four main classes:
- The abstract Observer observer is represented by an interface, usually an interface or an abstract class
- Abstract observed Subject
- The concrete observer UseObserver rewrites the method in the parent class interface
- The specific observed UseSubject inherits from the Subject
Let's take a look at specific examples to facilitate the understanding of this design pattern:
Create first Observer.class , define a getMessage() method here, which is used to receive the update information sent by the observed in the observer
//Observer public interface Observer { // public void update(); public void getMessage(String msg); }
Then create an abstract observer object Subject.class In this class, add() and remove() methods are created to add and remove observer objects to the observer list. Second, the notification () method is used to traverse the current observer list and call the observer's getMessage() method
public class Subject { //Observed public List<Observer> list = new ArrayList<Observer>(); public void add(Observer observer) { list.add(observer); } public void remove(Observer observer) { list.remove(observer); } public void notifiy(String msg) { for(Observer observer : list) { // observer.update(); observer.getMessage(msg); } } }
Next, create the specific observer and the observed. Rewrite the getMessage() method in UseObserver and read the message sent by the observer
public class UseObserver implements Observer{ private String name; private String message; public UseObserver(String name) { this.name = name; } // @Override // public void update() { // // TODO Auto-generated method stub //// System.out.println("UseObserver update notification received"); // } @Override public void getMessage(String msg) { // TODO Auto-generated method stub this.message = msg; read(); // System.out.println(msg); } public void read() { System.out.println(name + "received:" + message); } }
The specific observed inherits Subject.class The notify () method in
public class UseSubject extends Subject{ public void doSomething(String msg) { super.notifiy(msg); } }
use. Three observer objects a, B and C are defined respectively, and added to the observer list. A message is sent by the observed UseSubject
public class Test { public static void main(String[] args) { //subject UseSubject subject = new UseSubject(); // UseObserver2 observer2 = new UseObserver2(); //useObserver UseObserver observerA = new UseObserver("A"); UseObserver observerB = new UseObserver("B"); UseObserver observerC = new UseObserver("C"); subject.add(observerA); subject.add(observerB); subject.add(observerC); subject.doSomething(" Message"); } }
View run results
See that all three observers have received the message sent by the observed. That's what our observer model is for. At this time, if observer C is removed from the list of observer objects, run
public class Test { public static void main(String[] args) { //subject UseSubject subject = new UseSubject(); // UseObserver2 observer2 = new UseObserver2(); //useObserver UseObserver observerA = new UseObserver("A"); UseObserver observerB = new UseObserver("B"); UseObserver observerC = new UseObserver("C"); subject.add(observerA); subject.add(observerB); subject.add(observerC); subject.doSomething(" Message"); subject.remove(observerC); subject.doSomething("NEW message"); } }
You can see that when C is removed, new messages will not be received. This is one of the simplest applications of the observer model.
.
.
.
Welcome to communicate and correct