欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

C++设计模式中的观察者模式一起来看看

时间:2022-10-01 13:35:17|栏目:C代码|点击:

设计模式:观察者模式

观察者模式也称发布订阅模式,发布者发布消息,订阅者接收消息。

  • 发布者接口
#ifndef __observerPatterns_publish_hpp__
#define __observerPatterns_publish_hpp__

#include "observerPatterns_subscribe.hpp"

class observerPatterns_publish
{
public:
    virtual void registerObjectSubscribe(observerPatterns_subscribe *ops) = 0;
    virtual void removeObjectSubscribe(observerPatterns_subscribe *ops) = 0;
    virtual void notifyObjectSubscribe() = 0;
};

#endif
  • 订阅者接口
#ifndef __observerPatterns_subscribe_hpp__
#define __observerPatterns_subscribe_hpp__

#include "observerPatterns_common.hpp"

class observerPatterns_subscribe
{
public:
    virtual void update(const observerPatterns_msgPackage &opmp) = 0;
};

#endif
  • 发布者类
#ifndef __observerPatterns_object_publish_hpp__
#define __observerPatterns_object_publish_hpp__

#include <unordered_set>
#include "observerPatterns_publish.hpp"

class observerPatterns_object_publish : public observerPatterns_publish
{
private:
    observerPatterns_msgPackage _opmp;
    std::unordered_set<observerPatterns_subscribe *> _subscribeObjectBucket;
public:
    observerPatterns_object_publish();
    ~observerPatterns_object_publish();
    void registerObjectSubscribe(observerPatterns_subscribe *ops);
    void removeObjectSubscribe(observerPatterns_subscribe *ops);
    void notifyObjectSubscribe();
    void setMsgPackage(const observerPatterns_msgPackage &opmp);
};

observerPatterns_object_publish::observerPatterns_object_publish()
{
}

observerPatterns_object_publish::~observerPatterns_object_publish()
{
}

void observerPatterns_object_publish::registerObjectSubscribe(observerPatterns_subscribe *ops)
{
    _subscribeObjectBucket.insert(ops);
}

void observerPatterns_object_publish::removeObjectSubscribe(observerPatterns_subscribe *ops)
{
    _subscribeObjectBucket.erase(ops);
}

void observerPatterns_object_publish::notifyObjectSubscribe()
{
    for (auto &&_sob : _subscribeObjectBucket)
        _sob->update(_opmp);
}

void observerPatterns_object_publish::setMsgPackage(const observerPatterns_msgPackage &opmp)
{
    _opmp = opmp;
    notifyObjectSubscribe();
}

#endif
  • 订阅者类
#ifndef __observerPatterns_object_subscribe_hpp__
#define __observerPatterns_object_subscribe_hpp__

#include "observerPatterns_publish.hpp"
#include "observerPatterns_subscribe.hpp"

class observerPatterns_object_subscribe : public observerPatterns_subscribe
{
private:
    observerPatterns_msgPackage _opmp;
    observerPatterns_publish *_opb;
public:
    observerPatterns_object_subscribe(observerPatterns_publish *opb);
    ~observerPatterns_object_subscribe();
    void update(const observerPatterns_msgPackage &opmp);
};

observerPatterns_object_subscribe::observerPatterns_object_subscribe(observerPatterns_publish *opb)
    :_opb(opb)
{
    _opb->registerObjectSubscribe(this);
}

observerPatterns_object_subscribe::~observerPatterns_object_subscribe()
{
    _opb->removeObjectSubscribe(this);
}

void observerPatterns_object_subscribe::update(const observerPatterns_msgPackage &opmp)
{
    _opmp = opmp;
    printf("url:%s, msg:%s\n", _opmp.url.c_str(), _opmp.msg.c_str());
}

#endif
#ifndef __observerPatterns_object_subscribe2_hpp__
#define __observerPatterns_object_subscribe2_hpp__

#include "observerPatterns_publish.hpp"
#include "observerPatterns_subscribe.hpp"

class observerPatterns_object_subscribe2 : public observerPatterns_subscribe
{
private:
    observerPatterns_msgPackage _opmp;
    observerPatterns_publish *_opb;
public:
    observerPatterns_object_subscribe2(observerPatterns_publish *opb);
    ~observerPatterns_object_subscribe2();
    void update(const observerPatterns_msgPackage &opmp);
};

observerPatterns_object_subscribe2::observerPatterns_object_subscribe2(observerPatterns_publish *opb)
    :_opb(opb)
{
    _opb->registerObjectSubscribe(this);
}

observerPatterns_object_subscribe2::~observerPatterns_object_subscribe2()
{
    _opb->removeObjectSubscribe(this);
}

void observerPatterns_object_subscribe2::update(const observerPatterns_msgPackage &opmp)
{
    _opmp = opmp;
    printf("url:%s, msg:%s\n", _opmp.url.c_str(), _opmp.msg.c_str());
}

#endif
  • 公共头文件
#ifndef __observerPatterns_common_hpp__
#define __observerPatterns_common_hpp__

#include <string>

struct observerPatterns_msgPackage
{
    std::string url;
    std::string msg;
};

typedef struct observerPatterns_msgPackage observerPatterns_msgPackage;

#endif
  • 主函数测试
/************************************************************************
    > File Name: observerPatterns_main.cpp
    > Author: 
    > Mail: 
    > Created Time: 
************************************************************************/

#include "observerPatterns_common.hpp"
#include "observerPatterns_object_publish.hpp"
#include "observerPatterns_object_subscribe.hpp"
#include "observerPatterns_object_subscribe2.hpp"

using namespace std;

int main(int argc, char *argv[])
{
    observerPatterns_object_publish *opop = new observerPatterns_object_publish;
    
    observerPatterns_msgPackage opmp1, opmp2;
    opmp1.url = "www.aaa.com";
    opmp1.msg = "Hello!";
    opmp2.url = "www.xyzxyz.com";
    opmp2.msg = "Hello, observerPatterns!";
    
    observerPatterns_object_subscribe *oposa = new observerPatterns_object_subscribe(opop);
    observerPatterns_object_subscribe *oposb = new observerPatterns_object_subscribe(opop);
    observerPatterns_object_subscribe2 *oposc = new observerPatterns_object_subscribe2(opop);
    observerPatterns_object_subscribe2 *oposd = new observerPatterns_object_subscribe2(opop);

    opop->setMsgPackage(opmp1);
    opop->setMsgPackage(opmp2);
    
    delete oposa;
    delete opop;
    return 0;
}

上一篇:C语言中const和define的区别你了解嘛

栏    目:C代码

下一篇:C语言链接属性的实践应用

本文标题:C++设计模式中的观察者模式一起来看看

本文地址:http://www.codeinn.net/misctech/215141.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有