【Example of observer Mode]
For example, when a driver arrives at the final point of a game called "need for speed", the screen of other drivers will display a prompt about how long a certain person has reached the final point, in fact, this is a typical application of the observer mode. The observer mode is a typical bidirectional one-to-many application scenario where one end is used to notify multiple ends.
========================================================== =====
The descriptions extracted from another article are as follows:
When should we use the observer mode?
When an object changes, other objects must be notified.
Gof provides the following scenarios where the observer mode is used:
1) When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.
2) When changing an object, you need to change other objects at the same time without knowing how many objects need to be changed.
3) when an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.
In fact, the observer mode shares a common use environment with the bridges and strategies mentioned above: changes are encapsulated independently to achieve maximum reuse and decoupling. The difference between the observer and the latter is that the changes of the target and observer in the Observer mode are not independent, but have some relationships.
Why the observer mode?
It is mainly capable of good decoupling.
========================================================== =====
【ObserverMode description]
Type: behavior mode
Defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of the topic object changes, all observer objects are notified so that they can update themselves.
【ObserverMode UML diagram]
【ObserverMode-Java code implementation]
Create an abstract publish notification parent class:
| Package carer_subject;
Import java. util. arraylist;
Import java. util. List;
Import carer_observer.carer_observer;
Public interface carer_subject_parent {
Public void set_state (string state_string );
Public String get_state ();
Public void add_observer (carer_observer carer_observer_ref );
Public void sub_observer (carer_observer carer_observer_ref );
Public void notityallcarer ();
} |
Create release notification implementation class:
| Package carer_subject;
Import java. util. arraylist;
Import java. util. List;
Import carer_observer.carer_observer;
Public class carer_subject_sub implements carer_subject_parent {
List <carer_observer> list_observer = new arraylist <carer_observer> ();
Private string state;
Public void set_state (string state_string ){
This. State = state_string;
}
Public String get_state (){
Return state;
}
Public void add_observer (carer_observer carer_observer_ref ){
List_observer.add (carer_observer_ref );
}
Public void sub_observer (carer_observer carer_observer_ref ){
List_observer.remove (carer_observer_ref );
}
Public void notityallcarer (){
State = "reaching the end ";
For (INT I = 0; I <list_observer.size (); I ++ ){
List_observer.get (I). show_info ();
}
}
} |
Create an observer interface:
| Package carer_observer;
Import carer_subject.carer_subject_parent;
Public interface carer_observer {
Public void show_info ();
} |
Create an observer implementation class:
| Package carer_observer;
Import carer_subject.carer_subject_parent;
Public class carer_observer_imple implements carer_observer {
Private carer_subject_parent carer_subject_parent_ref;
Public carer_subject_parent getcarer_subject_parent_ref (){
Return carer_subject_parent_ref;
}
Public void setcarer_subject_parent_ref (
Carer_subject_parent carer_subject_parent_ref ){
This. carer_subject_parent_ref = carer_subject_parent_ref;
}
Public carer_observer_imple (carer_subject_parent carer_subject_parent_ref ){
Super ();
This. carer_subject_parent_ref = carer_subject_parent_ref;
}
Public void show_info (){
System. Out. println (carer_subject_parent_ref.get_state ());
}
} |
Create a client running class:
| Package run_main;
Import carer_observer.carer_observer_imple;
Import carer_subject.carer_subject_parent;
Import carer_subject.carer_subject_sub;
Public class run_main {
Public static void main (string [] ARGs ){
Carer_subject_parent carer_subject_parent_ref = new carer_subject_sub ();
Carer_observer_imple carer_observer_imple_ref1 = new carer_observer_imple (
Carer_subject_parent_ref );
Carer_observer_imple carer_observer_imple_ref2 = new carer_observer_imple (
Carer_subject_parent_ref );
Carer_observer_imple carer_observer_imple_ref3 = new carer_observer_imple (
Carer_subject_parent_ref );
Carer_observer_imple carer_observer_imple_ref4 = new carer_observer_imple (
Carer_subject_parent_ref );
Carer_subject_parent_ref.add_observer (carer_observer_imple_ref1 );
Carer_subject_parent_ref.add_observer (carer_observer_imple_ref2 );
Carer_subject_parent_ref.add_observer (carer_observer_imple_ref3 );
Carer_subject_parent_ref.add_observer (carer_observer_imple_ref4 );
Carer_subject_parent_ref.sub_observer (carer_observer_imple_ref3 );
Carer_subject_parent_ref.set_state ("to the end ");
Carer_subject_parent_ref.notityallcarer ();
}
} |
The program running result is as follows:
The observer mode is also called the publish/subscribe mode to notify multiple parties by changing the status of one party in a one-to-many manner. The observer mode is also called the publish/subscribe mode, while the multiple parties subscribe to the same way as reading newspapers in life, if you like city Evening News, you will receive it.