Use
Observer Mode (Observer)
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time.
This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves.
The Observer pattern is a
Behavioral Patterns。
structure
Figure-Viewer Pattern structure diagram
Subject: The subject class, which holds all observers who subscribe to this topic, the Observer's
the quantity is arbitrary。 Defined
Add Observer
(
Attach)And
Delete Viewer (Detach)The interface.AbstractclassSubject {
protectedString name;
protectedString State;
protectedList<observer> observers =NewArraylist<observer> ();
PublicAbstractString getState ();
PublicAbstractvoidSetState (String state);
PublicAbstractvoidNotify ();
PublicSubject (String name) {
This. name = name;
}
PublicvoidAttach (Observer Observer) {
OBSERVERS.ADD (Observer);
}
Public voidDetach (Observer Observer) {
OBSERVERS.REMOVE (Observer);
}
}
Observer : The Observer class, which defines update interfaces (updates), Observer need to synchronize update information when Subject notifications are received.
AbstractclassObserver {
protectedString name;
protectedSubject Subject;
PublicObserver (String name, Subject Subject) {
This. name = name;
This. Subject = subject;
}
PublicAbstractvoidUpdate ();
}
ConcreteSubject : A specific topic class that stores all the observers interested in this topic. When the internal state changes, all registered observers (Notify)should be notified .
classConcreteSubjectextendsSubject {
PublicConcreteSubject (String name) {
Super(name);
}
@Override
PublicString GetState () {
returnState
}
@Override
PublicvoidSetState (String state) {
This. state = State;
}
@Override
PublicvoidNotify () {
System.out.println ("=======" + This. Name + "topic post new Message =======");
for(Observer observer:observers) {
Observer. Update ();
}
}
}
concreteobserver : The specific observer class that implements the update interfacefor Observer to synchronize state information with Subject.
classConcreteobserverextendsObserver {
PrivateString State;
PublicConcreteobserver (String name, Subject Subject) {
Super(name, subject);
}
@Override
PublicvoidUpdate () {
State = Subject.getstate ();
System.out.println ( This. Name + "received current status:" + state);
}
}
Test code
PublicclassObserverpattern {
PublicStaticvoidMain (string[] args) {
ConcreteSubject subject =NewConcreteSubject ("Weather");
Concreteobserver Observer1 =NewConcreteobserver ("Zhang San", subject);
Concreteobserver Observer2 =NewConcreteobserver ("John Doe", subject);
Concreteobserver Observer3 =NewConcreteobserver ("Harry", subject);
Subject. Attach (Observer1);
Subject. Attach (OBSERVER2);
Subject. Attach (OBSERVER3);
Subject.setstate ("Rain Today");
Subject. Notify ();
Subject. Detach (OBSERVER2);
Subject.setstate ("Sunny Tomorrow");
Subject. Notify ();
}
}View Code
Run results
======= Weather Topics release new messages =======
Zhang San received current status: Rain Today
John Doe received current status: Rain Today
Harry received current status: Rain Today
======= Weather Topics release new messages =======
Zhang San receive the current status: Sunny Tomorrow
Harry receive the current status: Sunny TomorrowView Code
JAVA Design Pattern Observer pattern