实验 21:观察者模式

实验 21:观察者模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解观察者模式的动机,掌握该模式的结构;

2、能够利用观察者模式解决实际问题。

 
   

 


[实验任务一]:股票提醒

当股票的价格上涨或下降5%时,会通知持有该股票的股民,当股民听到价格上涨的消息时会买股票,当价格下降时会大哭一场。

实验要求:

1.     提交源代码;

C++

#include<iostream>

#include<list>

using namespace std;

 

class MyObserver {

public:

    virtual void response(int rate) {};

};

 

class MySubject {

protected:

    list<MyObserver*>observers;

public:

    /*注册方法*/

    void attach(MyObserver*observer) {

        observers.push_back(observer);

    };

    /*注销方法*/

    void detach(MyObserver* observer) {

        observers.remove(observer);

    }

 

    /*抽象通知方法*/

    virtual void change(int rate) = 0;

};

 

class Stock : public MySubject {

public:

    void change(int rate) {

        list<MyObserver*>::iterator iter = observers.begin();

        for (; iter != observers.end(); iter++)

            (*iter)->response(rate);

    }

};

 

class Holders :public MyObserver {

public:

    void response(int rate) {

        if (rate >= 5) {

            cout << "股票上涨,购买股票" << endl;

        }

        else if (rate <= -5) {

            cout << "股票下跌,大哭一场" << endl;

        }

        else {

            cout << "股票还比较稳定哦~" << endl;

        }

    }

};

 

int main() {

    MySubject* subject = new Stock();

    MyObserver*obs1 = new Holders();

    subject->attach(obs1);

    cout << "----------------------股票上涨-----------------------" << endl;

    subject->change(5);

    cout << endl;

    cout << "----------------------股票下跌-----------------------" << endl;

    subject->change(-5);

    cout << endl;

    cout << "----------------------股票稳定-----------------------" << endl;

    subject->change(4);

    cout << endl;

}

运行结果:

 

Java

package observer_pattern;

 

public interface Guancha {

    public void sheng();

    public void jiang();

}

package observer_pattern;

 

public class Gumin implements Guancha{

    private String name;

    public Gumin(String a){name=a;}

    public void sheng(){

        System.out.println(name+"买了一些股票(股票升了)");

    }

    public void jiang(){

        System.out.println("股票降价了,哭了");

    }

}

package observer_pattern;

 

import java.util.ArrayList;

public class Fen{

    private ArrayList<Guancha> oblist;

    public Fen(){

        oblist=new ArrayList<>();

    }

    public void add(Guancha a){

        oblist.add(a);

    }

    public void remove(Guancha a){

        oblist.remove(a);

    }

    public void sheng(double a){

        if(a>0.05)

            for(Guancha guan:oblist){

                    guan.sheng();

            }

    }

    public void jiang(double a){

        if(a>0.05)

            for(Guancha guan:oblist){

                    guan.jiang();

            }

    }

}

package observer_pattern;

public class main {

    public static void main(String[] args) {

        Fen f=new Fen();

        Gumin g1=new Gumin("第一个股民");

        Gumin g2=new Gumin("第二个股民");

        f.add(g1);

        f.add(g2);

        System.out.println("股票升了");

        f.sheng(0.1);

        System.out.println("比率小于0.05");

        System.out.println();

        f.sheng(0.001);

       

        System.out.println("股票降了");

        f.jiang(0.1);

        System.out.println("比率小于0.05");

        System.out.println();

        f.jiang(0.001);

    }

}

运行结果:

 

 

 

2.     提交类图;

 

 

 

3.     注意编程规范。

posted @ 2022-11-17 10:25  萧贾jzm  阅读(102)  评论(0编辑  收藏  举报
//歌单id