[HEADFIRST] Observer mode-The story of the old King's jumping ash machine

Source: Internet
Author: User

Blog, look at the blog, to be directly into the theme, not too much foreplay, otherwise the reader looked at the no interest.

So look directly at the design pattern-an illustration of the viewer pattern:

The so-called subject, that is, the object of the viewer, when the object is changed, the object notifies, then subscribers who subscribe to the topic will receive notification and carry out related actions. Subscribers can subscribe to multiple topics, and a topic can be subscribed to by multiple subscribers.

Note that the so-called theme, subscribers, and notifications in this place are the same as notify. The answer is also, notify is a kind of ESB, involving the message center, sender, subscribers three aspects, and the sender of the message is not to distinguish whether the data is changed, but once the need to send a message directly, different content of the message, Subscribers subscribe to a specific type of message processing.

Here we give an example, Lao Wang Kai Gray Machine, the front wing of the gray machine if the parameters are abnormal, let the old King jump machine. So look at the code:

The front wing of the plane, which is the observed object:

Package com.observer; Import java.util.Observable; /** * Front wing of the grey machine * <p> * Viewer mode-Subject Object * </p> * @author quzishen * * public class Chiziinfo extends Observable {//~ ~ ~ Flight parameters private double flyparam; ~ ~ ~ Speed of rotation private double; ~ ~ ~ temperature private float temperature; ~ ~ Whether you need to check private Boolean Isneedcheck = true; /** * Notify subscriber parameter change */public void paramchanged () {setchanged (); notifyobservers (Isneedcheck);} public Chiziinfo (double Flypa RAM, double speed, float temperature) {super (); this.flyparam = Flyparam; this.speed = speed; this.temperature = Temperat Ure }/** * Set parameters * @param flyparm * @param speed * @param temperature */public void SetParam (double flyparm,double Speed,floa T temperature) {This.flyparam = Flyparm; this.speed = speed; this.temperature = temperature;//When parameters change, trigger notification paramchanged (); } public double Getflyparam () {return flyparam;} public void Setflyparam (double flyparam) {this.flyparam = Flyparam;} Public double GetSpeed () {return speed;} public void setspEed (double speed) {This.speed = speed,} public float gettemperature () {return temperature;} public void Settemperature (float temperature) {this.temperature = temperature;} }

Note that when the parameters change, it is the manual trigger to send the notification.

The balance machine of the plane, the observer.

Package com.observer; Import java.util.Observable; Import Java.util.Observer; /** * Grey Machine Balancing Machine * <p> * Viewer mode-Viewer * </p> * @author quzishen * */public class Balancemechine implements Obser ver {//~~~ The front wing of the gray machine private chiziinfo chiziinfo;/** * Initialize */public void init (Chiziinfo chiziinfo) {//Subscribe topic Chiziinfo.addo Bserver (this); The public void update (Observable o, Object arg) {//subject type determines if (o instanceof chiziinfo) {Boolean Isneedcheck = (Boolean) arg; Chiziinfo = (chiziinfo) o; if (isneedcheck) {//~~~ flight parameter double Flyparam = Chiziinfo.getflyparam ();//~~~ rotation speed double velocity = chiziinfo.getspeed ();//~ ~ ~ Temperature Float temperature = chiziinfo.gettemperature (); if (Flyparam > && speed > 10000 && temperature >) {publishmsgtodriver ("Lao Wang, quick jump machine. "); } } } } /** * Send signal to pilot * @param msg */private void Publishmsgtodriver (String msg) {//Publish the message to driver SYSTEM.OUT.PR INTLN (msg); } }

After you receive the message, decide which type to start with, because you may have subscribed to multiple topics.

Test code:

Package com.observer; public class Test {/** * Test Gray clerk Old King Jump machine * @param args */public static void main (string[] args) {Chiziinfo chiziinfo = new Ch Iziinfo (0,0,0); Balancemechine balancemechine = new Balancemechine (); Balancemechine.init (Chiziinfo); Chiziinfo.setparam (10, 10, 10); Chiziinfo.setparam (100, 10, 10); Chiziinfo.setparam (101, 10001, 300); } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.