博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 设计模式 之 观察者模式
阅读量:4994 次
发布时间:2019-06-12

本文共 2873 字,大约阅读时间需要 9 分钟。

package com.observer.theory;public class Test{    public static void main(String[] args)    {        StockData data=new StockData(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("交易报告:");            System.out.print("开盘价:"+data.getOpen());            System.out.print("收盘价:"+data.getClose());            System.out.print("最低价:"+data.getLow());            System.out.print("最高价:"+data.getHigh());            System.out.println("");        }    }        }package com.observer.theory;import java.util.Observable;import java.util.Observer;public class Buyer implements 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("买家报告:");            System.out.print("开盘价:"+data.getOpen());            System.out.print("收盘价:"+data.getClose());            System.out.print("最低价:"+data.getLow());            System.out.print("最高价:"+data.getHigh());            System.out.println("");        }    }        }package com.observer.theory;import java.util.Observable;import java.util.Observer;public class StockData extends 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();    }    }

转载于:https://www.cnblogs.com/verejava/p/9237060.html

你可能感兴趣的文章
BZOJ.3990.[SDOI2015]排序(DFS)
查看>>
hdu 1358
查看>>
“-fembed-bitcode is not supported on versions of iOS prior to 6.0” 错误
查看>>
[转]jquery mobile中redirect重定向问题
查看>>
[django]表格的添加与删除实例(可以借鉴参考)
查看>>
Mockito一个采用Java编写用于单元测试的Mocking框架
查看>>
把elipse非maven的Struts2+Spring+Ibatis项目导入Idea中
查看>>
SVGImageView
查看>>
Android UI 优化 使用<include/>和 <merge />标签
查看>>
linux命令--使用fsck修复文件系统
查看>>
洛谷 P2324 [SCOI2005]骑士精神
查看>>
leetcode(64)最小路径和
查看>>
Select文字居右显示
查看>>
mycat操作MySQL第一篇:全局表
查看>>
MySQL数据库表分区
查看>>
python多个装饰器的执行顺序
查看>>
岗顶-一图一世界
查看>>
一步步构造自己的vue2.0+webpack环境
查看>>
分页类
查看>>
Python装饰器的个人小理解
查看>>