Observer mode (sometimes called publish-subscribe mode, model-view mode, source-listener mode or slave mode) is a type of software design mode. In this mode, a target object manages all observer objects that depend on it and actively issues notifications when its state changes. This is usually achieved by calling each observer's methods. This pattern is usually used to implement event processing systems.
Observer mode perfectly separates the observer from the observed object. For example, the user interface can be used as an observer, and the business data is the observer. The user interface observes the changes in the business data. After discovering the changes in the data, it will be displayed on the interface. One principle of object-oriented design is that each class in the system will focus on one function, not other aspects. A person does only one thing and does it well. Observer mode draws clear boundaries between modules, improving the maintainability and reusability of the application.
The observer design pattern defines a one-to-many dependency between objects so that when an object's state changes, all objects that depend on it are notified and automatically refreshed.
Implementation method:
There are many ways to implement the Observer pattern, and fundamentally, the pattern must contain two roles: the observer and the object being observed. In the example just now, the business data is the object to be observed and the user interface is the observer. There is a logical relationship between the observer and the observer. When the observer changes, the observer will observe such changes and respond accordingly. If such an observation process is used between the user interface and business data, it can be ensured that the boundary between the interface and the data is drawn. Assuming that the application's needs change, the performance of the interface needs to be modified. Only a user interface needs to be reconstructed, and the business data does not need to change.
1. Observer
(Observer) registers itself in the Subject, and the observed object stores the observer in a container.
2. Being observed
The object being observed has some change (as shown in SomeChange in the figure), and all registered observers are obtained from the container and notified the observer of the change.
3. Revoke observation
The observer tells the observer to cancel the observation and removes the observer from the container.
When an observer registers himself into the container of the observer, the observer should not ask about the specific type of the observer, but should use the observer's interface. This advantage is: assuming there are other observers in the program, as long as this observer is also implemented by the same interface. A person being observed can correspond to multiple observers. When the person being observed changes, he can notify all observers one by one. Based on interfaces, not specific implementations - this provides greater flexibility for programs.
Demonstration code:
Define the role abstract class to be observed:
package test.edu.mainrole; import java.util.ArrayList; public abstract class AbsRole { private ArrayList<IObserver> list = new ArrayList<IObserver>(); public void add(IObserver observer) { list.add(observer); } public void delete(IObserver observer) { list.remove(observer); } public void nodifyObservers(String newState) { for (IObserver observer: list) { observer.update(newState); } } } Observed role subclass:
package test.edu.mainrole; public class Role extends AbsRole { private String state; public String getState() { return state; } public void change(String nupdate) { state = nupdate; this.nodifyObservers(state); } } Define the observer interface:
package test.edu.mainrole; public interface IObserver { public void update(String newState); } Specific observers:
package test.edu.mainrole; public class ObserverObj1 implements IObserver{ private String observerState; @Override public void update(String state) { observerState = state; System.out.println("The status of observer 1 is: " + observerState); } } package test.edu.mainrole; public class ObserverObj2 implements IObserver { private String observerState; @Override public void update(String state) { observerState = state; System.out.println("The status of observer 2 is: " + observerState); } } Test client:
package test.edu.mainrole; public class Client { /** * @param args */ public static void main(String[] args) { Role subject = new Role(); IObserver observer1 = new ObserverObj1(); IObserver observer2 = new ObserverObj2(); subject.add(observer1); subject.add(observer2); subject.change("update!"); } } Running results:
The state of observer 1 is: update! The state of observer 2 is: update!
The above is all about this article, and I hope it will inspire you to learn.