The Observer pattern (sometimes referred to as publish (publish)-Subscription (Subscribe), model-view, source-listener (Listener) or slave mode) is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it, and actively notifies it when its own state changes. This is usually done by calling the methods provided by the various observers. This pattern is often used to implement the event-handling system.
The Observer Model (Observer) perfectly separated the observer from the observed object. For example, the user interface can be used as an observer, the business data is observed, the user interface observes the changes in the business data, and when the data changes, it is displayed in the interface. One of the principles of object-oriented design is that each class in the system focuses on one function, not the other. An object does only one thing and does it well. The observer pattern defines clear boundaries between modules, increasing the maintainability and reusability of the application.
The observer design pattern defines a one-to-many dependency between objects so that when the state of an object changes, all objects that depend on it are notified and automatically refreshed.
Implementation mode:
There are many ways to implement the observer pattern, which in essence must contain two roles: The Observer and the observed object. In just the example, the business data is the observed object, and the user interface is the Observer. There is a logical connection between the observer and the observed, and when the observer changes, the Observer observes the change and responds accordingly. If you use such an observation process between the user interface and the business data, you can ensure that the interface and data are in a different way, assuming that the requirements of the application change, you need to modify the performance of the interface, only need to rebuild a user interface, the business data need not be changed.
1. Observers
(Observer) registers itself with the observed object (Subject), and the observed object stores the observer in a container (Container).
2. Be observed
The observed object has undergone some kind of change (as shown in the Somechange), from which all registered observers are notified and the changes are communicated to the observer.
3, the withdrawal of observation
The observer tells the Observer to withdraw the observation, and the observer removes the observer from the container.
When the observer registers himself in the observer's container, the observer should not interfere with the specific type of observer, but should use the observer's interface. The advantage is that there are other observers in the program, so long as the observer is the same interface implementation. An observed person can correspond to multiple observers, and when the observed changes, he can notify all the observers of the message. interfaces, rather than concrete implementations, provide greater flexibility for the program.
Demo Code:
Define the observed role abstract class:
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); c16/>}}}
The 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 Observer:
Package test.edu.mainrole;
public class ObserverObj1 implements iobserver{
private String observerstate;
@Override public
void Update (String state) {
observerstate = state;
SYSTEM.OUT.PRINTLN ("Observer 1 of the State 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 ("Observer 2 of the State 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!");
}
Run Result:
The status of Observer 1 is: update!
The status of Observer 2 is: update!
The above is the entire content of this article, I hope that the learning of everyone inspired.