Scene settings
My classmates subscribed to a number of magazines, there is impurity a, impurity B, the subscription of students have Ghost,guang
Now whenever the impurities are updated, notify the students who have subscribed to the magazine to remind them. The code that implements the logic.
First we define the interface, we need an interface Magzine and an Observer interface.
Used to give impurities and the behavior that the observer should have.
Interfaceobserver{ Public void Update();}AbstractClass magzine{protectedString name;protectedString status;protectedList<observer> observers =NewArraylist<observer> ();Abstract Public void Add(Observer Observer);Abstract Public void RM(Observer Observer);Abstract Public void Notifyy();}
For magazines, it's important to keep track of who subscribes, so you need an Add method and an RM method to manipulate subscribers (observers). You also need to have a Notifyy method to notify these subscribers of update information.
At the same time, subscribers Observer also need to be alerted, so they have update methods to receive notifications, and when Magzine the class is updated, it calls Observer's Update method to notify him.
Therefore, the specific object of observer is as follows:
class Ghost implements Observer{ PrivateMagzine magzine =NULL; PublicGhost (Magzine magzine) { This. magzine = Magzine; } @Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("Ghost:the magzine"+magzine.name +"has been updated"); }} class Guang implements Observer{ PrivateMagzine magzine =NULL; PublicGuang (Magzine magzine) { This. magzine = Magzine; } @Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("Guang:the magzine"+magzine.name+"has been updated"); }}
The specific classes of the magazine Magzine are as follows:
class MagA extends magzine{@Override Public voidAdd (Observer Observer) {observers.add (Observer); } @Override Public voidRM (Observer Observer) {observers.remove (Observer); } @Override Public voidNotifyy () { for(Observer ob:observers) {ob.update (); } }} class MAGB extends magzine{@Override Public voidAdd (Observer Observer) {observers.add (Observer); } @Override Public voidRM (Observer Observer) {observers.remove (Observer); } @Override Public voidNotifyy () { for(Observer ob:observers) {ob.update (); } }}
This can be subscribed to when the client calls:
Public Static void Main(string[] args) {//------------------Initialize Magazine and viewer--------------------Magzine Duzhemagzine =NewMagA (); Magzine Gaoxiaomagzine =NewMAGB (); Observer Aobserver =NewGhost (Duzhemagzine); Observer Bobserver =NewGuang (Gaoxiaomagzine); Duzhemagzine.name ="Duzhe"; Gaoxiaomagzine.name ="Gaoxiao";//------------------the viewer to subscribe-----------------------Duzhemagzine.add (Aobserver); Gaoxiaomagzine.add (Bobserver);//------------------magazine is updated---------------------------Duzhemagzine.status ="Version1"; gaoxiaomagzine.status="Version2";//------------------proactively inform readers--------------------------Duzhemagzine.notifyy (); Gaoxiaomagzine.notifyy (); }
The result of the execution is:
Ghost:the Magzineduzhe has been updated
Guang:the Magzinegaoxiao has been updated
The observer pattern is to manipulate the Observer 's " Update" method by keeping the observer's reference with the observer, while the Observer retains the "observed" reference to the observer, thus obtaining the status information of the observed person.
Observer pattern characteristic mode motive
Splitting a system into a series of collaborative classes has a bad side effect, which is the need to maintain consistency among related objects. We do not want to make all kinds of tight coupling in order to maintain consistency.
But one of the great benefits of this model is that the observer does not need to know exactly how many observer are watching him or what specific classes these observer are, as long as he changes the state, giving notice in turn, and Observer does not have to know exactly which specific "observer" Send him a message, but only to care about the updates it receives.
Using Scenarios to summarize
The observer pattern should be used when an object changes to notify other objects or to change other objects at the same time, and it does not know how many specific objects need to be changed. The reason that both the observer and the observer are defined as interfaces is the contact coupling, which makes both sides dependent on abstraction rather than specificity so that the changes on both sides do not affect the other side.
Design Patterns-Observer patterns