设计模式-命令模式
2008-08-25 13:11
218 查看
1. [/b]命令模式的角色组成[/b]
1) 命令角色(Command):生命执行操作的接口。接口或抽象类来实现。
2) 具体命令角色(Concrete Command):将一个接收者对象绑定于一个动作;调用接收者相应的操作,以实现命令角色声明的执行操作的接口。
3) 客户角色(Client): 创建一个具体命令对象(并可以设定它的接收者)。
4) 请求者角色(Invoker):调用命令对象执行这个请求。
5) 接收者角色(Receiver):知道如何实施与执行一个请求相关的操作。任何类都可能作为一个接收者。
命令模式类图:
暂缺
2. [/b]应用实例[/b]
(本人感觉这个例子比较能切合类图的含义)
场景:某日,我去饭店吃饭,进去后对服务员说:“我要一个三明治”,服务员对厨师喊,“三明治一份”,我然后又说:“再来个煮鸡蛋。”服务员对厨师喊,“煮鸡蛋一份”,我说:“没了,就这些”,服务员喊,“就这些了,开做。”于是厨师开始做。
#include "stdafx.h"
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
//ICommand相当于服务员喊话的标准
class ICommand
{
public:
virtual void Execute()
{
};
};
//Receiver 相当于厨师
class Receiver
{
//做鸡蛋
public:
void CookEgg()
{
cout << "Cooking an egg./n";
}
//做三明治
void CookSandwich()
{
cout << "Cooking a sandwich./n";
}
};
//喊话内容:鸡蛋
class CookEggCommand : public ICommand
{
protected:
Receiver* receiver;
public:
CookEggCommand(Receiver* receiver)
{
this->receiver = receiver;
}
void Execute()
{
receiver->CookEgg();
}
};
//喊话内容:三明治
class CookSandwichCommand : public ICommand
{
protected:
Receiver* receiver;
public:
CookSandwichCommand(Receiver* receiver)
{
this->receiver = receiver;
}
void Execute()
{
receiver->CookSandwich();
}
};
//Invoker 相当于服务员
class Invoker
{
public:
int iCount;
map<int, ICommand*> commands;
Invoker()
{
iCount = 0;
}
//喊话
void SetCommand(ICommand* command)
{
commands.insert(map<int, ICommand*>::value_type(iCount, command));
iCount++;
}
//执行喊话内容
void ExecuteCommand()
{
int i;
ICommand* cmd;
for(i = 0; i < iCount; i++)
{
cmd = commands[i];
cmd->Execute();
}
}
};
int main()
{
Receiver receiver;
ICommand* egg= new CookEggCommand(&receiver);
ICommand* sandwich = new CookSandwichCommand(&receiver);
Invoker invoker;
//服务员喊话,三明治一份
invoker.SetCommand(sandwich);
//服务员喊话,鸡蛋一份
invoker.SetCommand(egg);
//服务员喊话,没有别的了,做吧。
invoker.ExecuteCommand();
delete egg;
delete sandwich;
return 1;
}
1) 命令角色(Command):生命执行操作的接口。接口或抽象类来实现。
2) 具体命令角色(Concrete Command):将一个接收者对象绑定于一个动作;调用接收者相应的操作,以实现命令角色声明的执行操作的接口。
3) 客户角色(Client): 创建一个具体命令对象(并可以设定它的接收者)。
4) 请求者角色(Invoker):调用命令对象执行这个请求。
5) 接收者角色(Receiver):知道如何实施与执行一个请求相关的操作。任何类都可能作为一个接收者。
命令模式类图:
暂缺
2. [/b]应用实例[/b]
(本人感觉这个例子比较能切合类图的含义)
场景:某日,我去饭店吃饭,进去后对服务员说:“我要一个三明治”,服务员对厨师喊,“三明治一份”,我然后又说:“再来个煮鸡蛋。”服务员对厨师喊,“煮鸡蛋一份”,我说:“没了,就这些”,服务员喊,“就这些了,开做。”于是厨师开始做。
#include "stdafx.h"
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
//ICommand相当于服务员喊话的标准
class ICommand
{
public:
virtual void Execute()
{
};
};
//Receiver 相当于厨师
class Receiver
{
//做鸡蛋
public:
void CookEgg()
{
cout << "Cooking an egg./n";
}
//做三明治
void CookSandwich()
{
cout << "Cooking a sandwich./n";
}
};
//喊话内容:鸡蛋
class CookEggCommand : public ICommand
{
protected:
Receiver* receiver;
public:
CookEggCommand(Receiver* receiver)
{
this->receiver = receiver;
}
void Execute()
{
receiver->CookEgg();
}
};
//喊话内容:三明治
class CookSandwichCommand : public ICommand
{
protected:
Receiver* receiver;
public:
CookSandwichCommand(Receiver* receiver)
{
this->receiver = receiver;
}
void Execute()
{
receiver->CookSandwich();
}
};
//Invoker 相当于服务员
class Invoker
{
public:
int iCount;
map<int, ICommand*> commands;
Invoker()
{
iCount = 0;
}
//喊话
void SetCommand(ICommand* command)
{
commands.insert(map<int, ICommand*>::value_type(iCount, command));
iCount++;
}
//执行喊话内容
void ExecuteCommand()
{
int i;
ICommand* cmd;
for(i = 0; i < iCount; i++)
{
cmd = commands[i];
cmd->Execute();
}
}
};
int main()
{
Receiver receiver;
ICommand* egg= new CookEggCommand(&receiver);
ICommand* sandwich = new CookSandwichCommand(&receiver);
Invoker invoker;
//服务员喊话,三明治一份
invoker.SetCommand(sandwich);
//服务员喊话,鸡蛋一份
invoker.SetCommand(egg);
//服务员喊话,没有别的了,做吧。
invoker.ExecuteCommand();
delete egg;
delete sandwich;
return 1;
}
相关文章推荐
- 设计模式学习02----之命令模式
- C++设计模式之命令模式
- 设计模式学习--命令模式
- 设计模式学习总结-命令模式(Cmmand Pattern)
- 学习设计模式第十七 - 命令模式
- 设计模式的艺术之道--命令模式
- PHP设计模式之命令模式的深入解析
- 【Unity与23种设计模式】命令模式(Command)
- Java设计模式(18)之命令模式
- Java设计模式之命令模式 .
- 一头扎进设计模式-命令模式
- 深入理解JavaScript系列(34):设计模式之命令模式详解
- 设计模式之命令模式
- Java设计模式百例 - 命令模式
- 命令设计模式
- 码农小汪-设计模式之-命令模式
- 设计模式 - 命令模式 C++
- C++设计模式——命令模式
- 设计模式C++学习笔记之十二(Command命令模式)
- 深入理解JavaScript系列(34):设计模式之命令模式