Observer pattern,
have been using, but do not know what is the Observer pattern. To see an article about the observer pattern, it is plainly that an object (the observer) holds a reference to a bunch of objects (the observer), which are placed in a list, and when the state of the observer is changed, invokes the update () method of the reference (Observer). Let all the observers know that the state of the Observer has changed, and then it changes itself.
Key points:
1. A one-to-many relationship, an observer (object), one or more observers (Observer)
2. The observer holds a reference to each observer
3. The Observer has the method of registering and canceling the registration
4. In the class of the observer, use the list to store the observer's reference
5. When the state of the observer is changed, call the relevant notify () method and invoke the update () method of each observer in the Notify () method to let the observer know about the event.
I have a piece of code: (using a traffic light case,
1. Traffic lights are observed, cars are observers
2. When you need to see the traffic lights during the day, when the light is green, the car is driving and the traffic lights are red, the car waits
3. When 12 o'clock in the morning to 4, do not need to look at the traffic lights, you can pass freely, this time traffic lights on the car does not work. )
Observed (TrafficLight)
1 Public classTrafficLight {2 Private Static FinalString TAG = "TrafficLight";3 PrivateTrafficLightStatus defaultstatus =Trafficlightstatus.green;4 PrivateList<icar> observerlist =NewArraylist<>();5 6 Publictrafficlightstatus Getlightstatus () {7 returndefaultstatus;8 }9 Ten Public voidsettrafficlightstatus (trafficlightstatus status) { OneDefaultstatus =status; A } - - Public voidNotifystatuschange () { the if(Observerlist.size () > 0) { - for(inti = 0; I < observerlist.size (); i++) { - if(Defaultstatus = =trafficlightstatus.green) { - Observerlist.get (i). Move (); +}Else { - Observerlist.get (i). Stop (); + } A } at } - } - - Public voidregistertrafficlight (ICar car) { - if(!observerlist.contains (CAR)) { - Observerlist.add (car); in } - } to + Public voidunregistertrafficlight (ICar car) { - if(Observerlist.contains (car)) the observerlist.remove (car); * } $}
Viewer (ICar)
1 Public Interface ICar {2 void move (); 3 void stop (); 4 }
The observer's implementation class (Toytacar ... )
1 Public classToytacarImplementsICar {2 Private Static FinalString TAG = "Toytacar";3 4 @Override5 Public voidMove () {6LOG.E (TAG, "Toytacar, Green, driving");7 }8 9 @OverrideTen Public voidStop () { OneLOG.E (TAG, "Toytacar, Red, wait"); A } -}
mainactivity,4 Button, registration, cancellation, status change (green, red)
1 Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener {2 PrivateList<icar> Carlist =NewArraylist<>();3 Button btnredlight, Btngreenlight, Btnfreelight, btnlimitlight;6 TrafficLight TrafficLight;7 ICar Toytacar, Nissancar, Mazdacar, Hondacar , Fordcar, Bydcar, Bmwcar, Audicar;8 9 @OverrideTen protected voidonCreate (Bundle savedinstancestate) { One Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); atBtnredlight =(Button) Findviewbyid (r.id.btn_red_light); -Btngreenlight =(Button) Findviewbyid (r.id.btn_green_light); -Btnfreelight =(Button) Findviewbyid (r.id.btn_free_light); -Btnlimitlight =(Button) Findviewbyid (r.id.btn_limit_light); -Btnredlight.setonclicklistener ( This); -Btngreenlight.setonclicklistener ( This); inBtnfreelight.setonclicklistener ( This); -Btnlimitlight.setonclicklistener ( This); toTrafficLight =NewTrafficLight (); +Toytacar =NewToytacar (); -Nissancar =NewNissancar (); theMazdacar =NewMazdacar (); *Hondacar =NewHondacar (); $Fordcar =NewFordcar ();Panax NotoginsengBydcar =NewBydcar (); -Bmwcar =NewBwmcar (); theAudicar =NewAudicar (); + Carlist.add (toytacar); A Carlist.add (nissancar); the Carlist.add (mazdacar); + Carlist.add (hondacar); - Carlist.add (bydcar); $ Carlist.add (bmwcar); $ Carlist.add (audicar); - Carlist.add (fordcar); the } - Wuyi @Override the Public voidOnClick (View v) { - Switch(V.getid ()) { Wu CaseR.id.btn_green_light: - Trafficlight.settrafficlightstatus (trafficlightstatus.green); About Trafficlight.notifystatuschange (); $ Break; - CaseR.id.btn_red_light: - Trafficlight.settrafficlightstatus (trafficlightstatus.red); - Trafficlight.notifystatuschange (); A Break; + CaseR.id.btn_free_light: the for(inti = 0; I < carlist.size (); i++) { - Trafficlight.unregistertrafficlight (Carlist.get (i)); $ } the Break; the CaseR.id.btn_limit_light: the for(inti = 0; I < carlist.size (); i++) { the Trafficlight.registertrafficlight (Carlist.get (i)); - } in Break; the default: the Break; About } the } the}
Log output:
1. When the button is clicked-See traffic lights (registration), click the Traffic light button to call ICar's move () or stop () method, print out the log
1 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/toytacar:toytacar, red, waiting2 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/nissancar:nissancar, red, waiting3 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/mazdacar:mazdacar, red, waiting4 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/hondacar:hondacar, red, waiting5 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/bydcar:bydcar, red, waiting6 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/bwmcar:bwmcar, red, waiting7 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/audicar:audicar, red, waiting8 01-09 22:32:54.454 6900-6900/cc.lijingbo.pattern_observer e/fordcar:fordcar, red, waiting9 01-09 22:32:56.844 6900-6900/cc.lijingbo.pattern_observer e/toytacar:toytacar, green, drivingTen 01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer e/nissancar:nissancar, green, driving One 01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer e/mazdacar:mazdacar, green, driving A 01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer e/hondacar:hondacar, green, driving - 01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer e/bydcar:bydcar, green, driving - 01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer e/bwmcar:bwmcar, green, driving the 01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer e/audicar:audicar, green, driving -01-09 22:32:56.854 6900-6900/cc.lijingbo.pattern_observer E/fordcar:fordcar, Green, driving
2. When click Button-free pass (cancel registration), this time click the traffic lights button does not work, do not print the log
Reference:
"Linkage between objects-Observer mode": http://blog.csdn.net/lovelion/article/details/7720232
The interview was asked design mode? Don't be afraid to look here: Viewer mode: Http://mp.weixin.qq.com/s/pH7ifcOPzVxdrAIk4W8HiQ
The observer pattern of design patterns: http://www.jianshu.com/p/d55ee6e83d66#
Design mode-Viewer mode