1. What is the observer mode?
Abstract The relationship between newspapers and subscribers in the real world into the observer mode. A newspaper corresponds to multiple subscribers. Subscribers can subscribe to newspapers at any time, and unsubscribed readers can subscribe to newspapers at any time. Once a new newspaper is published, all subscribers will receive new content.
In observer mode, a newspaper is called a topic subject, and a subject is called an observer. A subject can be followed by multiple observers. The observer can remove the subject at any time, and the new observer can also follow the subject at any time. When the content of the subject changes, all observers are notified.
Ii. Example
Many online games have a Q & A activity. All players participating in the Q & A activity receive the question information at the same time (the delay is negligible). players not participating in the activity can join the Q & A activity in the middle, gamers who are answering questions can also exit at any time.
In this example, the game server is "1", the players are "more", and the question information is the message transmitted between them.
How can we design a class that meets the above requirements? Try the observer mode.
First, define the subject abstract class:
Package observerpattern; import Java. util. arraylist;/*** @ author ayqy * defines the subject abstract class **/public abstract class subject {arraylist <observer> Observers = new arraylist <observer> (); // observer list/*** register the Topic * @ Param o apply to register the Observer */Public void registsubject (Observer O) {observers. add (o);}/*** Delete Topic * @ Param o */Public void reomvesubject (Observer O) {int Index = observers. indexof (o); observers. remove (INDEX);}/*** notify all Observers * @ Param Arg the data that the subject wants to pass to Observers */Public void policyobservers (Object Arg) {for (Observer O: observers) O. update (this, ARG );}}
Note: abstract classes are used instead of interfaces. Why?
Because the behavior of subject is fixed, it does not need to be extended by sub-classes.
-------
Similarly, we define the observer abstract class:
Package observerpattern;/*** @ author ayqy * defines the observer abstract class **/public abstract class observer {subject = NULL; // define the subjectpublic abstract void Update (subject, object Arg) of the observer; // define the update interface of the observer}
-------
The following describes how to implement our custom subject -- gameserver:
Package observerpattern;/*** @ author ayqy * defines the gameserver class, inherits from the subject base class, and publishes the topic **/public class gameserver extends subject {question ques; // define the public question getques () {return ques;} public void setques (question q) {This. ques = Q; super. notifyobservers (ques); // call the parent class method to notify all observers }}
The member variable Question of the p.s. gameserver class is a simple encapsulation of question information. The question class includes the definition of Question No and content, and a tostring method. The topic description is returned.
-------
Then implement our custom observer -- PLAYER:
Package observerpattern;/*** @ author ayqy * defines playera, inherits from the observer base class, and receives new questions **/public class playera extends observer {public playera (subject sub) {subject = sub ;}@ overridepublic void Update (subject, object Arg) {question q = (question) ARG; system. out. println ("playera received" + q. tostring ());}}
P.s. In order to make the class hierarchy clearer, the player base class is not defined here
It is easy to copy and paste playerb and playerc.
Now, the preparation for the simulated Q & A activity is over. Next we need to define a test class to show the charm of the observer mode.
Iii. Example
Define the following test class:
Package observerpattern;/*** @ author ayqy * implements a test class to simulate online game Q & A activities (the game server updates question information on time and notifies all players involved in the Q &) **/public class test {public static void main (string [] ARGs) {system. out. println ("the Q & A activity is about to begin .. "); // Create server gameserver GS = new gameserver (); // create player abcobserver playera = new playera (GS); Observer playerb = new playerb (GS ); observer playerc = new playerc (GS); // register subject for AB. C is not interested in answering questions and refuses to register Gs. registsubject (playera); GS. registsubject (playerb); system. out. println ("Player AB successfully participates in the Q & A activity .. "); System. Out. println (" the answer activity officially started .. "); GS. setques (new question (1, "Question 1"); GS. setques (new question (2, "Question 2"); system. out. println ("Player A does not want to play any more. He will quit the Q & A activity .. "); GS. reomvesubject (playera); GS. setques (new question (3, "Question 3"); system. out. println ("PLAYER c wants to join the activity"); GS. registsubject (playerc); GS. setques (new question (4, "fourth question"); system. out. println ("the answer activity ends .. ");}}
Result example:
Iv. Summary
The following example shows the characteristics of the observer mode:
1. The observer mode can be used to easily establish a one-to-many dependency between objects.
2. The observer mode mechanism can easily achieve dynamic maintenance of this dependency.
Observer Pattern)