Quote Encyclopedia
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.
Related Roles
The Observer model mainly includes the following roles:
1. Abstract theme (Subject) role: Abstract theme Roles Keep all references to observer objects in a cluster (such as a ArrayList object), each subject can have any number of observers. Abstract topics provide an interface for adding and removing observer objects, and abstract theme roles are also called abstract-observer (observable) roles.
2, the specific theme (Realsubject) role: The relevant state into the specific observer object, in the specific subject of the internal status changes, to all registered observers to give notice. The specific theme role is also called the specific Observer (concrete observable) role.
3. Abstract observer (OBSERVER) Role: Define an interface for all the specific observers and update themselves when they are notified of the subject, which is called the update interface.
The specific observer (REALOBSERVER) role: The State of the store and the topic itself. The specific observer role implements the update interface required by the abstract observer role to coordinate itself with the state of the subject. The specific observer role can maintain a reference to a specific subject object, if necessary.
Concrete Implementation
Specific Code
1. Abstract Theme Class
Abstract Subject class public
abstract class Subject {
//Observer collection
private list<observer> List = new arraylist< Observer> ();
Add observer public
void Register (Observer Observer) {
list.add (Observer);
}
Delete public
void remove (Observer Observer) {
if (List.contains (Observer)) {
list.remove (Observer);
}
}
public void change (String status) {
this.notifyobserver (status);
}
Notifies all observers that
private void notifyobserver (String status) {for
(Observer observer:list) {
observer.update ( status);}}
2, the theme of specific implementation of the class
Specific theme Role class public class
Realsubject extends Subject {
private String status;
Public String GetStatus () {return
status;
}
public void SetStatus (String status) {
this.status = status;
}
public void change (String status) {
System.out.println (current topic Status: + status);
Super.change (status);
}
3. Observer interface
Observer interface Public
interface Observer {public
void update (String status);
}
4, the specific observer interface implementation
The specific observer class public
class Realobserver implements Observer {
private String name;
Private String innerstatus;
Public Realobserver (String name) {
super ();
this.name = name;
@Override public
void Update (String status) {
this.innerstatus = status;
SYSTEM.OUT.PRINTLN ("Observer" + Name + ", Status:" + Innerstatus);
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
}
5. Client-side test
public class Client {public
static void Main (string[] args) {
//create theme
Realsubject subject = new Realsubject () ;
Create Observer
Observer Observera = new Realobserver ("John");
Observer Observerb = new Realobserver ("Dick");
Add observer
Subject.register (Observera) to the subject;
Subject.register (Observerb);
Change theme Content Status
subject.change ("start");
}
The above implements the observer mode with simple code and runs the output:
Current topic Status: Start
Observer John, Status: Start
Observer Dick, Status: Start
At run time, the client first creates an instance of the specific subject class, as well as an observer object. It then invokes the register () method of the Subject object, registering the Observer object with the subject object, that is, adding it to the subject object's aggregation. At this point, the client invokes the change () method of the theme, changing the internal state of the subject object. The Subject object invokes the Notifyobservers () method of the superclass when the state changes, notifying all registered observer objects.