Design Patterns-the viewer

Source: Internet
Author: User

The Observer pattern defines a one-to-many dependency between objects, and when an object changes state, all of his dependents are notified and updated automatically;
Define the theme, the function of the subject is to hold the observer's reference, provide registration, cancellation and notification, and have status; The Observer implements the Update method in the interface and must register the specific topic;

The JDK has its own support capabilities for observers
Observer observable object, providing Add/delete/notifyobserver () and setchanged () set state method
The Observer observer interface, providing an update () method for related updates.

Viewer class Diagram:


The following program simulates the status update function of a stock (using the JDK to support the viewer, refer to the Web implementation)

1. Define the observable interface implementation class, which is the information that sets up the stock and notifies all observers of the status update after the data is updated.

Package com.observer; Import java.util.Observable; public class StockData extends Observable {    private String symbol;    private float close;    private float high;    private float low;    Private long volume;     Public StockData () {    } public     String Getsymbol () {        return symbol;    }     public float Getclose () {        return close;    }     public float Gethigh () {        return high;    }     public float Getlow () {        return low;    }     Public long Getvolume () {        return volume;    }     public void Sendstockdata () {        setchanged ();        Notifyobservers ();    }     public void Setstockdata (String symbol, float close, float high, float low,            long volume) {        this.symbol = symbol;        this.close = close;        This.high = high;        This.low = low;        This.volume = volume;        Sendstockdata ();    }}

2. Realizing the viewer observer implementation class, simulating the big buyer of the stock

Package com.observer; Import Java.text.decimalformat;import Java.text.decimalformatsymbols;import Java.util.observable;import Java.util.Observer;    public class Bigbuyer implements Observer {private String symbol;    private float close;    private float high;    private float low;     Private long volume; Public Bigbuyer (Observable Observable) {observable.addobserver (this);//Registration relationship} public void Update (OBSERVAB Le observable, Object args) {if (observable instanceof stockdata) {StockData StockData = (stockdata) o            bservable;            This.symbol = Stockdata.getsymbol ();            This.close = Stockdata.getclose ();            This.high = Stockdata.gethigh ();            This.low = Stockdata.getlow ();            This.volume = Stockdata.getvolume ();        Display ();        }} public void display () {DecimalFormatSymbols DFS = new DecimalFormatSymbols (); DecimalFormat Volumeformat = new DecimalFormat ("###,###,###,###", Dfs);        DecimalFormat Priceformat = new DecimalFormat ("###.00", Dfs); System.out.println ("Big Buyer reports ...        ");        System.out.println ("\tthe Lastest stock Quote for" + symbol + ' is: ");        System.out.println ("\t$" + priceformat.format (Close) + "per share (Close).");        System.out.println ("\t$" + priceformat.format (High) + "per share");        System.out.println ("\t$" + priceformat.format (Low) + "per share");        System.out.println ("\ T" + volumeformat.format (volume) + "shares traded.");    System.out.println (); }}

3. Implement the Observer observer implementation class, as a second observer

Package com.observer; Import Java.text.decimalformat;import Java.text.decimalformatsymbols;import Java.util.observable;import Java.util.Observer; public class Tradingfool implements Observer{private String symbol;private float Close, public tradingfool (Observable obs ervable) {observable.addobserver (this);//Registration relationship} public void update (observable observable,object args) {if        (Observable instanceof StockData)        {StockData StockData = (stockdata) observable;        This.symbol = Stockdata.getsymbol ();        This.close = Stockdata.getclose ();        Display ();    }} public void display () {DecimalFormatSymbols DFS = new DecimalFormatSymbols ();    DecimalFormat Priceformat = new DecimalFormat ("###.00", Dfs); System.out.println ("Trading fool says ...    ");    System.out.println ("\ t" + symbol + "is currently trading at $" + priceformat.format (Close) + "per share.");    System.out.println (); }}
4. Testing

Package Com.observer;public class Stockquotes{public static void Main (string[] args)    {    System.out.println ();    System.out.println ("--Stock Quote application--");    System.out.println ();     StockData StockData = new StockData ();     New Tradingfool (stockdata);    New Bigbuyer (stockdata);     Stockdata.setstockdata ("Jupm", 16.10f,16.15f,15.34f, (long) 481172);    Stockdata.setstockdata ("Sunw", 4.84f,4.90f,4.79f, (long) 68870233);    Stockdata.setstockdata ("MSFT", 23.17f,23.37f,23.05f, (long) 75091400);}    }



Design Patterns-the viewer

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.