[行为型模式]命令模式的理解

[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=13770720612900291&size=800_600&devtype=web&v=1.0.1&rtick=14676146428611&share_qid=108635719&sign=df4c1667350df926ee18fdacc755a17d&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14665838309468885&size=800_600&devtype=web&v=1.0.1&rtick=14665838997753&share_qid=108635719&sign=935089d0615eedc8867f9bd4a8ab63cd&[/img]
[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=14665838329487458&size=800_600&devtype=web&v=1.0.1&rtick=14665838993852&share_qid=108635719&sign=0c12173f3f6846f4e6a1d5cbd984c0a3&[/img]
[color=red][b]头文件[/b][/color]

//CommandPattern.h

#ifndef COMMAND_PATTERN_H
#define COMMAND_PATTERN_H

#include <Windows.h>
#include <string>
using namespace std;


namespace CommandPattern
{

class Receiver
{
public:
void Action();
};

class AbstructCommand
{
public:
virtual ~AbstructCommand();
virtual void Execute() = 0;
};

class ConcretetCommand : public AbstructCommand
{
public:
ConcretetCommand(Receiver* pReciver);
virtual ~ConcretetCommand();
virtual void Execute();

private:
Receiver* m_pReciver;
};


class Invoker
{
public:
Invoker(AbstructCommand* pCommand);

void Order();

private:
AbstructCommand* m_pCommand;
};

//
void CommandPattern_Test();
}

#endif

[color=red][b]实现[/b][/color]

#include "CommandPattern.h"
#include <iostream>
using namespace std;

#define SAFE_DELETE(p) if (p) { delete p; p = NULL; }

namespace CommandPattern
{
//
void Receiver::Action()
{
cout << "Receiver->Action" << endl;
}

//
AbstructCommand::~AbstructCommand()
{

}

//
ConcretetCommand::ConcretetCommand(Receiver* pReciver)
: m_pReciver(NULL)
{
if (m_pReciver==NULL && pReciver!=NULL)
{
m_pReciver = pReciver;
}
}

ConcretetCommand::~ConcretetCommand()
{

}

void ConcretetCommand::Execute()
{
if (m_pReciver != NULL)
{
cout << "ConcretetCommand->Execute" << endl;
m_pReciver->Action();
}
}

//
Invoker::Invoker(AbstructCommand* pCommand)
: m_pCommand(NULL)
{
if (m_pCommand==NULL && pCommand!=NULL)
{
m_pCommand = pCommand;
}
}

void Invoker::Order()
{
cout << "Invoker->Order" << endl;
m_pCommand->Execute();
}


void CommandPattern_Test()
{
Receiver* pReceiver = new Receiver();
ConcretetCommand* pConcreteCmd = new ConcretetCommand(pReceiver);
Invoker* pInvoker= new Invoker(pConcreteCmd);

pInvoker->Order();

SAFE_DELETE(pReceiver);
SAFE_DELETE(pConcreteCmd);
SAFE_DELETE(pInvoker);
}
}

[color=red][b]客户端[/b][/color]

#include "CommandPattern.h"


#include <iostream>
using namespace std;
using namespace CommandPattern;
void main()
{
CommandPattern_Test();
}

[color=red][b]运行结果[/b][/color]

Invoker->Order
ConcretetCommand->Execute
Receiver->Action


[img]https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1466571446&di=20ae1e1632a15ea7c7026e717607590a&src=http://images.cnblogs.com/cnblogs_com/JackyTecblog/201211/201211050903506952.png[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值