C++设计模式之观察者模式

介绍 [1]

观察者模式最重要的一个核心思想就是一个类是消息源,这个类负责给若干个需要这个消息的类进行传消息。只要能实现这个目的即可。在C++中其实有很简单的实现方法,那就是在消息源这个类中申明一个list,里边存放需要接受消息的类。然后负责操作这个list即可。

例子介绍

这个例子的背景是球员和教练。这里把教练的命令可以理解为消息源。然后把那么场上的球员理解为观察者。然后,消息源(教练)发出指令,观察者(球员)收到指令后做出相应的动作。


UML类图
#include <iostream>
#include <list>
#include <string.h>

using namespace std;

class Command;
//这个类作为观察者的抽象基类
class Player 
{
public:
    Player(string name,Command *command)
    {
        this->name = name;
        this->m_command = command;
    }
    virtual void execCommand(string content) = 0;
    string Name()
    {
        return name;
    }
protected:
    string name;
    Command *m_command;

};


class Forward:public Player
{
public:
    Forward(string name,Command *command):Player(name,command)
    {
    }
    void execCommand(string content);
};

class Guard:public Player
{
public:
    Guard(string name,Command *command):Player(name,command)
    {
    }
    void execCommand(string content);
};

class Command
{
public:
        virtual void attach(Player *player) = 0;
    virtual void detach(Player *player) = 0; 
    virtual void notify(string content) = 0;
protected:
        list<Player *> m_list;
};

class CoachCommand:public Command
{
public:
    void attach(Player *player)
    {
        m_list.push_back(player);
    }
    void detach(Player *player)
    {
        list<Player *>::iterator item = m_list.begin();
        while(item != m_list.end())
        {
            if((*item) == player)

            {
                cout<<player->Name()<<"被换下场"<<endl;
                item = m_list.erase(item);
                            
            }
            else
                ++item;
        }
    }
    void notify(string content)
    {
        list<Player *>::iterator it;
        for(it = m_list.begin() ; it != m_list.end() ; it ++)
        {
            (*it)->execCommand(content);
        }
    }
};

一、正文标题

void Forward::execCommand(string content)
{
    if(content == "开始强攻")
        cout<<name<<"收到:"<<content<<"的指令,然后采取:"<<"开启疯抢模式的战术"<<endl;
    if(content == "开始防守")
        cout<<name<<"收到:"<<content<<"的指令,然后采取:"<<"回到本方半场进行防守,伺机进行反击的战术"<<endl;
}


void Guard::execCommand(string content)
{
    if(content == "开始强攻")
        cout<<name<<"收到:"<<content<<"的指令,然后采取:"<<"往前提防线,保持高压态势的战术"<<endl;
    if(content == "开始防守")
        cout<<name<<"收到:"<<content<<"的指令,然后采取:"<<"龟缩到禁区附近,进行全力防守的战术"<<endl;
}


int main()
{
    CoachCommand *Kolpp;
    Forward *Salah,*Mane;
    Guard *vanDijk,*Matip;
    Kolpp = new CoachCommand();
    Salah = new Forward("萨拉赫",Kolpp);
    Mane = new Forward("大马内",Kolpp);
    vanDijk = new Guard("范戴克",Kolpp);
    Matip = new Guard("马蒂普",Kolpp);
    Kolpp->attach(Salah);
    Kolpp->attach(Mane);
    Kolpp->attach(vanDijk);
    Kolpp->attach(Matip);
    Kolpp->notify("开始强攻");
    cout<<"比赛进行到75分钟,利物浦已经3:0领先!"<<endl;
    
    Kolpp->detach(vanDijk);
    Kolpp->notify("开始防守");
    delete Matip;
    delete vanDijk;
    delete Mane;
    delete Salah;
    delete Kolpp;

};


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,385评论 19 139
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,830评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,800评论 1 32
  • A是一名心理咨询师,50岁左右,阅人无数,咨询费是一个小时200,为了业务生存,个人标签为“愿意为你提供心理学支持...
    哥斯拉天下无敌阅读 1,625评论 4 2
  • 动画片本来是为孩子设计的,从孩子能够理解的角度,或故事性,或趣味性,或教育性,具有一定美感的艺术作品。 然而,在一...
    多瑞果阅读 4,481评论 0 0