设计模式(c++) 观察者模式

#include <iostream>
#include <vector>
using namespace std;
class Observer
{
private :
    int x,y;
public :
    void Update(int x,int y)
    {
        this->x = x;
        this->y = y;
    }
    void Print()
    {
        cout << "x = " << x << ",y = " << y << endl;
    }
};
class Subject
{
private :
    int x,y;
    vector<Observer *> vec;
public :
    Subject()
    {
        x = 0;
        y = 0;
        Notify();
    }
    Subject(int x,int y)
    {
        this->x = x;
        this->y = y;
        Notify();
    }
    void AddObserver(Observer *t)
    {
        vec.push_back(t);
    }
    void Notify()
    {
        vector <Observer *>::iterator it;
        for(it = vec.begin();it != vec.end();it++)
        {
            (*it)->Update(x,y);
        }
    }
    void DataChange(int x,int y)
    {
        this->x = x;
        this->y = y;
        Notify();
    }
    void Print()
    {
        vector <Observer *>::iterator it;
        for(it = vec.begin();it != vec.end();it++)
        {
            (*it)->Print();
        }
    }
};
int main()
{
    Observer *obj1 = new Observer();
    Observer *obj2 = new Observer();
    Subject *s = new Subject();
    s->AddObserver(obj1);
    s->AddObserver(obj2);
    s->Notify();
    s->Print();
    s->DataChange(3,5);
    s->Print();

    return 0;
}


(2)
#include <iostream>
#include <vector>
using namespace std;
class Observer
{
private :
    int x,y;
    Subject *s;
public :
    Observer(Subject *s)
    {
        this->s = s;
        s->Add(this);
    }
    void Update(int x,int y)
    {
        this->x = x;
        this->y = y;
    }
    void Print()
    {
        cout << "x = " << x << "y = " << y << endl;
    }
};
class Subject
{
private :
    int x,y;
    vector<Observer *> vec;
public :
    Subject()
    {
        x = 0;
        y = 0;
        Notify();
    }
    Subject(int x,int y)
    {
        this->x = x;
        this->y = y;
        Notify();
    }
    void Add(Observer *t)
    {
        vec.push_back(t);
    }
    void Notify()
    {
        vector<Observer *>::iterator it;
        for(it = vec.begin();it != vec.end();it++)
        {
            (*it)->Update(x,y);
        }
    }
    void DataChange(int x,int y)
    {
        this->x = x;
        this->y = y;
        Notify();
    }
    void Print()
    {
        vector <Observer *>::iterator it;
        for(it = vec.begin();it != vec.end();it++)
        {
            (*it)->Print();
        }
    }
};
int main()
{
    Subject *su = new Subject();
    Observer *obj1 = new Observer(su);
    Observer *obj2 = new Observer(su);
    su->Notify();
    su->Print();
    su->DataChange(3,5);
    su->Print();

    return 0;
}


(3)
#include <iostream>
#include <vector>
using namespace std;
class Subject
{
public :
    virtual void Add(Observer *t) = 0;
    virtual void Notify() = 0;
};
class ConcreteSubjectA : public Subject
{
private :
    int x,y;
    vector<Observer *> vec;
public :
    ConcreteSubjectA()
    {
        x = 0;
        y = 0;
        Notify();
    }
    ConcreteSubjectA(int x,int y)
    {
        this->x = x;
        this->y = y;
        Notify();
    }
    void Add(Observer *t)
    {
        vec.push_back(t);
    }
    void Notify()
    {
        vector<Observer *>::iterator it;
        for(it = vec.begin();it != vec.end();it++)
        {
            (*it)->Update(x,y);
        }
    }
    void DataChange(int x,int y)
    {
        this->x = x;
        this->y = y;
        Notify();
    }
};
class ConcreteSubjectB : public Subject
{
private :
    int z;
    vector<Observer *> vec;
public :
    ConcreteSubjectB()
    {
        z = 0;
        Notify();
    }
    ConcreteSubjectA(int z)
    {
        this->z = z;
        Notify();
    }
    void Add(Observer *t)
    {
        vec.push_back(t);
    }
    void Notify()
    {
        vector<Observer *>::iterator it;
        for(it = vec.begin();it != vec.end();it++)
        {
            (*it)->Update(z);
        }
    }
    void DataChange(int z)
    {
        this->z = z;
        Notify();
    }
};
class Observer
{
protected :
    Subject *s;
public :
    Observer(Subject *t)
    {
        s = t;
        s->Add(this);
    }
    virtual void Update(int x,int y) = 0;
};
class ConcreteObserverA : public Observer
{
private :
    int x,y;
public :
    ConcreteObserverA(Subject *t) : Observer(t){}
    void Update(int x,int y)
    {
        this->x = x;
        this->y = y;
    }
};
class ConcreteObserverB : public Observer
{
private :
    int z;
public :
    ConcreteObserverB(Subject *t) : Observer(t){}
    void Update(int x,int y)
    {
        this->z = x+y;
    }
};
int main()
{
    ConcreteSubjectA *cs1 = new ConcreteSubjectA();
    ConcreteObserver *obA1 = new ConcreteObserverA(cs1);
    ConcreteObserver *obA2 = new ConcreteObserverA(cs1);
    cs1->DataChange(3,5);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值