zoukankan      html  css  js  c++  java
  • 状态模式之观察者模式

    题目

    类图

    Java

    Investor接口

    package com.gazikel;
    
    public interface Investor {
    
        void update(float range);
    }
    

    Subject接口

    package com.gazikel;
    
    public interface Subject {
    
        /**
         * 添加一个股民
         */
        void addInvestor(Investor investor);
    
    
        /**
         * 移除一个股民
         */
        void removeInvestor(Investor investor);
    
        /**
         * 通知所有的股民
         */
        void notifyInvestors();
    }
    

    Investor1实现Investor接口

    package com.gazikel;
    
    public class Investor1 implements Investor {
    
        private float range;
    
        @Override
        public void update(float range) {
            this.range = range;
            display();
        }
    
        public void display() {
            if (this.range > 0) {
                // 上涨消息
                System.out.println("买股票");
            } else if(this.range < 0) {
                // 下降消息
                System.out.println("大哭一场");
            }
        }
    }
    

    Stock实现Subject接口

    package com.gazikel;
    
    public class Investor1 implements Investor {
    
        private float range;
    
        @Override
        public void update(float range) {
            this.range = range;
            display();
        }
    
        public void display() {
            if (this.range > 0) {
                // 上涨消息
                System.out.println("买股票");
            } else if(this.range < 0) {
                // 下降消息
                System.out.println("大哭一场");
            }
        }
    }
    
    

    Client

    package com.gazikel;
    
    public class Client {
    
        public static void main(String[] args) {
            Investor investor1 = new Investor1();
            Stock stock = new Stock();
            // 添加股民
            stock.addInvestor(investor1);
    
            // 上涨不足5%,不会通知股民
            stock.setData(0.03f);
            System.out.println("------------------------");
    
            // 上涨超过5%,通知股民
            stock.setData(0.06f);
            System.out.println("------------------------");
    
            // 下降超过5%,通知股民
            stock.setData(-0.07f);
            System.out.println("------------------------");
    
    
        }
    }
    

    C++

    #include<iostream>
    #include<list>
    using namespace std;
    
    class Investor {
    public:
    	virtual void update(double range) = 0;
    };
    
    class Investor1 :public Investor {
    private:
    	double range;
    public:
    	void update(double range) {
    		this->range = range;
    		display();
    	}
    	void display() {
    		if (this->range > 0) {
    			cout << "买股票" << endl;
    		}
    		else if (this->range < 0) {
    			cout << "大哭一场" << endl;
    		}
    	}
    };
    
    class Subject {
    public:
    	virtual void AddInvestor(Investor* investor) = 0;
    	virtual void NotifyInvestors() = 0;
    };
    
    class Stock :public Subject {
    private:
    	double range;
    	list<Investor*> investors;
    public:
    	void SetData(double range) {
    		this->range = range;
    		if (range > 0) {
    			cout << "股票价格上涨了" << endl;
    		}
    		else if (this->range < 0) {
    			cout << "股票价格下降了" << endl;
    		}
    		else {
    			cout << "股票价格保持平稳" << endl;
    		}
    		if (this->range >= 0.05 || this->range <= -0.05) {
    			NotifyInvestors();
    		}
    	}
    
    	void AddInvestor(Investor *investor) {
    		investors.push_back(investor);
    	}
    
    	void NotifyInvestors() {
    		list<Investor*>::iterator it;
    		for (it = investors.begin(); it != investors.end(); it++) {
    
    			(*it)->update(this->range);
    
    		}
    	}
    };
    
    int main() {
    	Investor* investor1 = new Investor1();
    
    	Stock* stock = new Stock();
    
    	// 添加股民
    	stock->AddInvestor(investor1);
    
    	// 上涨不足5%,不会通知股民
    	stock->SetData(0.03);
    	cout << "-------------------------------" << endl;
    
    	// 上涨超过5%,通知股民
    	stock->SetData(0.06f);
    	cout << "-------------------------------" << endl;
    
    	// 下降超过5%,通知股民
    	stock->SetData(-0.07f);
    	cout << "-------------------------------" << endl;
    }
    

  • 相关阅读:
    解析库的使用---页面解析总结
    django--学习笔记 一
    项目(三)--个人博客开发
    算法图解(python3版本)--读后感
    "mysql技术内幕innodb存储引擎"--读书笔记
    通过ftp同步服务器文件:遍历文件夹所有文件(含子文件夹、进度条);简单http同步服务器文件实例
    使用node-webkit(v0.35.5)和innosetup(3.6.1)打包将web程序打包为桌面客户端(安装包)
    .netcore3.1使用ELK日志中心(使用NLog数据传输)
    .netcore3.1使用skywalking8.1.0(docker-compose一键部署)
    asp.net 页面刷新锚点失效问题解决
  • 原文地址:https://www.cnblogs.com/Gazikel/p/15613822.html
Copyright © 2011-2022 走看看