作者:davidwzw2009413 | 来源:互联网 | 2023-09-25 17:17
(本博客旨在个人总结回顾)
1、详情:
命令模式:将一个请求封装成对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
说明:
优点:①降低了系统耦合度。 ②新的命令可以很容易添加到系统中去。
缺点:使用命令模式可能会导致某些系统有过多的具体命令类。
使用场景:认为是命令的地方都可以使用命令模式,比如: ①GUI 中每一个按钮都是一条命令。 ②模拟 CMD。
注意事项:系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作,也可以考虑使用命令模式,见命令模式的扩展。
2.1、UML类图:
2.2、例子源码
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//#pragma once#include "targetver.h"#include
#include #include
using namespace std;// TODO: 在此处引用程序需要的其他头文件
Receiver.h
#pragma onceclass Receiver
{
public:Receiver();~Receiver();public:void Operation(int nState);
};
Receiver.cpp
#include "stdafx.h"
#include "Receiver.h"Receiver::Receiver()
{
}Receiver::~Receiver()
{
}void Receiver::Operation(int nState)
{cout <<"执行请求:" <}
Command.h
#pragma once
#include "Receiver.h"class Command
{
public:Command(Receiver* pReceiver, int nState);virtual ~Command();public:virtual void Execute() &#61; 0;protected:Receiver* m_pReceiver;int m_nState;
};
Command.cpp
#include "stdafx.h"
#include "Command.h"Command::Command(Receiver* pReceiver, int nState): m_pReceiver(pReceiver), m_nState(nState)
{
}Command::~Command()
{
}
CommandX.h
#pragma once
#include "Command.h"class CommandX :public Command
{
public:CommandX(Receiver* pReceiver, int nState);~CommandX();public:void Execute();
};
CommandX.cpp
#include "stdafx.h"
#include "CommandX.h"CommandX::CommandX(Receiver* pReceiver, int nState) : Command(pReceiver, nState)
{
}CommandX::~CommandX()
{
}void CommandX::Execute()
{m_pReceiver->Operation(m_nState);
}
Sender.h
#pragma once
#include "Command.h"class Sender
{
public:Sender();~Sender();public:void AddCommand(Command* pCommand);void RemoveCommand(Command* pCommand);void ExecuteCommand();private:std::list m_listCommand;
};
Sender.cpp
#include "stdafx.h"
#include "Sender.h"Sender::Sender()
{
}Sender::~Sender()
{for (std::list::iterator it &#61; m_listCommand.begin(); it !&#61; m_listCommand.end(); it&#43;&#43;){delete *it;}m_listCommand.clear();
}void Sender::AddCommand(Command* pCommand)
{m_listCommand.push_back(pCommand);
}void Sender::ExecuteCommand()
{for (std::list::iterator it &#61; m_listCommand.begin(); it !&#61; m_listCommand.end(); it&#43;&#43;){(*it)->Execute();}
}void Sender::RemoveCommand(Command* pCommand)
{for (std::list::iterator it &#61; m_listCommand.begin(); it !&#61; m_listCommand.end(); it&#43;&#43;){if (*it &#61;&#61; pCommand){delete *it;m_listCommand.erase(it);break;}}
}
调用代码
CommandPatternMemo.cpp
// CommandPatternMemo.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include "Receiver.h"
#include "CommandX.h"
#include "Sender.h"int _tmain(int argc, _TCHAR* argv[])
{Receiver* pReceiver &#61; new Receiver();Command* pCommand &#61; new CommandX(pReceiver, 1);Sender sender;sender.AddCommand(pCommand);pCommand &#61; new CommandX(pReceiver, 2);sender.AddCommand(pCommand);sender.RemoveCommand(pCommand);pCommand &#61; new CommandX(pReceiver, 3);sender.AddCommand(pCommand);pCommand &#61; new CommandX(pReceiver, 4);sender.AddCommand(pCommand);sender.ExecuteCommand();delete pReceiver;pReceiver &#61; NULL;system("pause");return 0;
}
2.3、运行结果&#xff1a;