设计模式-观察者模式

Observer 模式应该可以说是应用最多、影响最广的模式之一,因为 Observer 的一个实例 Model/View/Control( MVC) 结构在系统开发架构设计中有着很重要的地位和意义, MVC实现了业务逻辑和表示层的解耦。

今天就来简单实现以下观察者模式。

我们首先来模拟一个场景,比如网上购物,现在有一款手机,价格会随着时间发生变化,如果顾客想要以较低的价格来购买,就需要不停去刷新页面,这样会给顾客带来很大的不便,如果能一降价顾客就知道就好了。

我们接下来就想办法,让手机降价后,顾客便可以立即知道。

我们的思路是这样的,如果顾客对这款手机感兴趣,就对这款手机设置价格变动的提醒,如果手机价格发生变化,自动通知每一个设置过提醒的顾客来关注价格的变化。

然后用C++来进行
这是goods.h

#ifndef _GOODS_H_
#define _GOODS_H_

#include<list>
#include<string>

using namespace std;

class Customer;

class Goods
{
public:
    virtual ~Goods();

    //将顾客入链,价格变动时,通知所有链上的顾客
    void register_customer(Customer *cus);
    //顾客不再关心,从链中移除
    void unregister_customer(Customer *cus);
    //对链上顾客挨个通知
    void notify();
    //设置价格
    virtual void SetPrice(string price) = 0;
    //获取当前价格
    virtual string GetPrice() = 0;

protected:
    Goods();
private:
    list<Customer *> *_cus;
};

class Phone : public Goods
{
public:
    void SetPrice(string price);
    string GetPrice();
private:
    string _price;
};

#endif

接下来是对应的实现 goods.cpp

#include"goods.h"
#include"customer.h"

Goods::Goods()
{
    _cus = new list<Customer *>;
}

Goods:: ~Goods()
{
    delete _cus;
}

void Goods::register_customer(Customer *cus)
{
    _cus->push_front(cus);
}

void Goods::unregister_customer(Customer *cus)
{
    _cus->remove(cus);
}

void Goods::notify()
{
    list<Customer *>::iterator it;
    for(it = _cus->begin(); it != _cus->end(); ++it)
    {
        //调用每个用户的更新函数来进行通知
        (*it)->update(this);
    }
}

void Phone::SetPrice(string price) 
{
    _price = price;
}

string Phone::GetPrice()
{
    return _price;
}

然后我们看看对应的observer类,即顾客
下面是customer.h

#ifndef _CUSTOMER_H_
#define _CUSTOMER_H_

#include"goods.h"
using namespace std;

class Customer
{
public:
    virtual ~Customer();
    //对应顾客的更新操作
    virtual void update(Goods *g) = 0;

protected:
    Customer();
    string _notice;
};

class CusW : public Customer
{
public:
    CusW(Goods *g);
    ~CusW();
    void update(Goods *g);
private:
    //指向关心的商品
    Goods *_g;
};

class CusZ : public Customer
{
public:
    CusZ(Goods *g);
    ~CusZ();
    void update(Goods *g);
private:
    //指向关心的商品
    Goods *_g;
};

#endif

接下来是其实现customer.cpp

#include"customer.h"
#include"goods.h"

#include<iostream>
using namespace std;

Customer::Customer()
{
    _notice = '\0';
}

Customer:: ~Customer()
{}

CusW::CusW(Goods *g)
{
    _g = g;
    _g->register_customer(this);
}

CusW::~CusW()
{
    _g->unregister_customer(this);
}

void CusW::update(Goods *g)
{
    _notice = g->GetPrice();
    cout<<"w顾客了解到,价格改为"<<_notice<<endl;
}

CusZ::CusZ(Goods *g)
{
    _g = g;
    _g->register_customer(this);
}

CusZ::~CusZ()
{
    _g->unregister_customer(this);
}

void CusZ::update(Goods *g)
{
    _notice = g->GetPrice();
    cout<<"z顾客了解到,价格改为"<<_notice<<endl;
}

最后我们来测试一下

#include"goods.h"
#include"customer.h"
using namespace std;

int main()
{
    //实例化一个手机商品
    Goods *p = new Phone();
    //两位关心此商品价格的顾客
    CusW *pw = new CusW(p);
    CusZ *pz = new CusZ(p);
    //手机价格发生变动
    p->SetPrice("1000");
    //通知关心价格的顾客
    p->notify();

    p->SetPrice("3000");
    p->notify();

    return 0;
}

我们来看看运行结果
这里写图片描述
可以看到,两名顾客都收到了两次价格变化的通知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值