The observer pattern for Java design Patterns

Source: Internet
Author: User

http://www.verejava.com/?id=16999149610674

Package Com.observer.theory;public class test{public static void Main (string[] args) {StockData data=new St                Ockdata (16.1f,15.0f,17.2f,16.9f);        Buyer buyer=new Buyer (data);                Trading trading=new Trading (data);        Data.setstockdata (16.1f,15.0f,17.4f,16.9f);            Data.setstockdata (16.1f,14.5f,17.4f,16.9f); }}package com.observer.theory;import Java.util.observable;import Java.util.observer;public class Trading implements    observer{public Trading (Observable o) {o.addobserver (this);            } @Override public void update (Observable o, Object Arg) {if (o instanceof stockdata) {            StockData data= (stockdata) o;            SYSTEM.OUT.PRINTLN ("Transaction report:");            System.out.print ("Open Price:" +data.getopen ());            System.out.print ("Closing price:" +data.getclose ());            System.out.print ("Lowest Price:" +data.getlow ());            System.out.print ("High Price:" +data.gethigh ());        System.out.println (""); }   }}package com.observer.theory;import Java.util.observable;import java.util.observer;public class Buyer Implemen    TS observer{public Buyer (Observable o) {o.addobserver (this);            } @Override public void update (Observable o, Object Arg) {if (o instanceof stockdata) {            StockData data= (stockdata) o;            SYSTEM.OUT.PRINTLN ("Buyer Report:");            System.out.print ("Open Price:" +data.getopen ());            System.out.print ("Closing price:" +data.getclose ());            System.out.print ("Lowest Price:" +data.getlow ());            System.out.print ("High Price:" +data.gethigh ());        System.out.println (""); }}}package com.observer.theory;import java.util.observable;import Java.util.observer;public class StockData ex    Tends observable{private float open;    private float low;    private float high;    private float close;        Public StockData (float open, float low, float high, float close) {super ();        This.open = open; THis.low = low;        This.high = high;    This.close = close;    } public float Getopen () {return open;    } public void Setopen (float open) {this.open = open;    } public float Getlow () {return low;    } public void Setlow (float low) {this.low = low;    } public float Gethigh () {return high;    } public void Sethigh (float high) {This.high = high;    } public float Getclose () {return close;    } public void Setclose (float close) {this.close = close;        } public void Setstockdata (float open, float low, float high, float close) {this.open = open;        This.low = low;        This.high = high;            This.close = close;        Setchanged ();    Notifyobservers (); }    }

http://www.verejava.com/?id=16999149610674

The observer pattern for Java design Patterns

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.