Java教程

软件设计-观察者模式

本文主要是介绍软件设计-观察者模式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

 

类图

 

 

源代码

Java

package rjsj.no21;

public class ConcreteObs implements Observer{
    @Override
    public void response1() {
        System.out.println("买股票!");
    }

    @Override
    public void response2() {
        System.out.println("大哭一场!");
    }
}
package rjsj.no21;

public class ConcreteSub extends Subject{
    @Override
    public void up() {
        System.out.println("价格上涨5%");
        for (Object obs:observers){
            ((Observer)obs).response1();
        }
    }

    @Override
    public void down() {
        System.out.println("价格下降5%");
        for (Object obs:observers){
            ((Observer)obs).response2();
        }
    }
}
package rjsj.no21;

public interface Observer {
    void response1();
    void response2();
}
package rjsj.no21;

import java.util.ArrayList;

public abstract class Subject {
    protected ArrayList observers = new ArrayList();

    public void attach(Observer obs){
        observers.add(obs);
    }

    public void detach(Observer obs){
        observers.remove(obs);
    }

    public abstract void up();
    public abstract void down();
}
package rjsj.no21;

public class Client {
    public static void main(String[] args) {
        Subject subject=new ConcreteSub();
        Observer observer = new ConcreteObs();
        //添加观察者
        subject.attach(observer);

        subject.up();
        subject.down();
    }
}

 

 

C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyObserver
{
public:
    virtual void response1() = 0;
    virtual void response2() = 0;
};

class Investor:public MyObserver
{
public:
     void response1(){
        cout<<"买股票"<<endl;
    }
    void response2(){
        cout<<"大哭一场"<<endl;
    }
};
class Subject
{
public:
    Subject(){}
    virtual ~Subject(){}
    virtual void attach(MyObserver *observer)=0;
    virtual void detach(MyObserver *observer)=0;
    virtual void stockUp()=0;
    virtual void stockDown()=0;
};
class Stock:public Subject
{
public:
    void attach(MyObserver *observer)
    {
        observers.push_back(observer);
    }
    void detach(MyObserver *observer)
    {
        vector<MyObserver*>::iterator observer_rm = find(observers.begin(), observers.end(), observer);
        observers.erase(observer_rm);
    }
    void stockUp()
    {
         cout<<"-----股票上涨5%-----"<<endl;
         vector <MyObserver *>::iterator iObserver = observers.begin();
         while(iObserver != observers.end())
         {
              (*iObserver)->response1();
              iObserver++;
         }
    }
     void stockDown()
    {
         vector <MyObserver *>::iterator iObserver = observers.begin();
         cout<<"-----股票下降5%-----"<<endl;
         while(iObserver != observers.end())
         {
              (*iObserver)->response2();
              iObserver++;
         }
    }
private:
    vector<MyObserver*> observers;
};
int main()
{
    Stock subject;
    Investor obs;
    subject.attach(&obs);
    subject.stockUp();
    subject.stockDown();
}

 

运行截图

 

这篇关于软件设计-观察者模式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!