1。如果invoker中不必保存命令集,而仅执行一条命令的情况下,invoker类可以被省略
2。在命令类(Command中直接保存接收者(Receiver)的指针或者 引用的方式,并不可取,宜改为存储ID(前提是所有的receiver有统一的基类,并由全局管理)
下面是代码的对比,由于是演示代码,写的比较粗糙。
保存指针方式
下面的代码,是保存命令接收者ID的方式,这种方式可以用于将不同命令纷发到各个模块(receiver)处理之类的需求。比如将客户端发向服务器的消息,发到各自的模块去处理。
1 class Command;
2 class Receiver;
3
4 #include<stdio.h>
5 #include<string>
6 usingnamespace std;
7 class Receiver
8 {
9 public:
10 void action(string str ){
11 printf("%s excute command\n",str.c_str());
12 }
13 };
14
15 class Invoker
16 {
17 public:
18 Command* cmd;
19 };
20 class Command
21 {
22 public:
23 Command(Receiver* r){
24 rec = r;
25 }
26 virtualvoid excute() =0;
27 virtualvoid setArgs(string arg) =0;
28 public:
29 Receiver* rec;
30 };
31 class ConcreteCommand : public Command
32 {
33 public:
34 ConcreteCommand(Receiver* r):Command(r){};;
35 virtualvoid setArgs(string arg){
36 arg1 = arg;
37 }
38 public:
39 virtualvoid excute(){
40 rec->action(arg1);
41 }
42 private:
43 string arg1;
44 };
45
46 int main()
47 {
48 Receiver* rec =new Receiver();
49 Command* cmd =new ConcreteCommand( rec );
50 cmd->setArgs(" i am concretecommand");
51 Invoker*in=new Invoker();
52 in->cmd = cmd;
53 cmd->excute();
54 return0;
55 }
2 class Receiver;
3
4 #include<stdio.h>
5 #include<string>
6 usingnamespace std;
7 class Receiver
8 {
9 public:
10 void action(string str ){
11 printf("%s excute command\n",str.c_str());
12 }
13 };
14
15 class Invoker
16 {
17 public:
18 Command* cmd;
19 };
20 class Command
21 {
22 public:
23 Command(Receiver* r){
24 rec = r;
25 }
26 virtualvoid excute() =0;
27 virtualvoid setArgs(string arg) =0;
28 public:
29 Receiver* rec;
30 };
31 class ConcreteCommand : public Command
32 {
33 public:
34 ConcreteCommand(Receiver* r):Command(r){};;
35 virtualvoid setArgs(string arg){
36 arg1 = arg;
37 }
38 public:
39 virtualvoid excute(){
40 rec->action(arg1);
41 }
42 private:
43 string arg1;
44 };
45
46 int main()
47 {
48 Receiver* rec =new Receiver();
49 Command* cmd =new ConcreteCommand( rec );
50 cmd->setArgs(" i am concretecommand");
51 Invoker*in=new Invoker();
52 in->cmd = cmd;
53 cmd->excute();
54 return0;
55 }
命令中保存接收者ID
class Command;
class Receiver;
#include<stdio.h>
#include<string>
usingnamespace std;
class Receiver
{
public:
virtualvoid action(string str )=0;
public:
int receiverID;
virtualint getReceiverID() =0;
};
class ReceiverManager
{
public:
enum { NORMAL =0};
public:
static ReceiverManager* getInstance(){
if( !instance ){
instance =new ReceiverManager();
}
return instance;
}
void action(int id ,string args){
receivers[id]->action(args);
}
private:
ReceiverManager(){};
~ReceiverManager(){
if( instance ){
delete instance ;
instance =0;
}
};
public:
Receiver* receivers[100];
private:
static ReceiverManager* instance;
};
ReceiverManager* ReceiverManager::instance =0;
class Invoker
{
public:
Command* cmd;
};
class Command
{
public:
Command(int id){
receiverID = id;
}
virtualvoid excute() =0;
virtualvoid setArgs(string arg) =0;
public:
int receiverID;
};
class ConcreteCommand : public Command
{
public:
ConcreteCommand(int id ):Command(id){};;
virtualvoid setArgs(string arg){
arg1 = arg;
}
public:
virtualvoid excute(){
ReceiverManager* manager = ReceiverManager::getInstance();
manager->action( receiverID ,arg1);
}
private:
string arg1;
};
class NormalReceiver: public Receiver
{
public:
NormalReceiver()
{
receiverID = ReceiverManager::NORMAL;
}
public:
void action(string str ){
printf("%s excute command,and i am normalreceiver\n",str.c_str());
}
int getReceiverID(){
return receiverID;
}
};
int main()
{
Receiver* rec =new NormalReceiver();
ReceiverManager* manager = ReceiverManager::getInstance();
manager->receivers[rec->getReceiverID()] = rec;
Command* cmd =new ConcreteCommand( ReceiverManager::NORMAL);
cmd->setArgs(" this is concretecommand");
Invoker*in=new Invoker();
in->cmd = cmd;
cmd->excute();
return0;
}
class Receiver;
#include<stdio.h>
#include<string>
usingnamespace std;
class Receiver
{
public:
virtualvoid action(string str )=0;
public:
int receiverID;
virtualint getReceiverID() =0;
};
class ReceiverManager
{
public:
enum { NORMAL =0};
public:
static ReceiverManager* getInstance(){
if( !instance ){
instance =new ReceiverManager();
}
return instance;
}
void action(int id ,string args){
receivers[id]->action(args);
}
private:
ReceiverManager(){};
~ReceiverManager(){
if( instance ){
delete instance ;
instance =0;
}
};
public:
Receiver* receivers[100];
private:
static ReceiverManager* instance;
};
ReceiverManager* ReceiverManager::instance =0;
class Invoker
{
public:
Command* cmd;
};
class Command
{
public:
Command(int id){
receiverID = id;
}
virtualvoid excute() =0;
virtualvoid setArgs(string arg) =0;
public:
int receiverID;
};
class ConcreteCommand : public Command
{
public:
ConcreteCommand(int id ):Command(id){};;
virtualvoid setArgs(string arg){
arg1 = arg;
}
public:
virtualvoid excute(){
ReceiverManager* manager = ReceiverManager::getInstance();
manager->action( receiverID ,arg1);
}
private:
string arg1;
};
class NormalReceiver: public Receiver
{
public:
NormalReceiver()
{
receiverID = ReceiverManager::NORMAL;
}
public:
void action(string str ){
printf("%s excute command,and i am normalreceiver\n",str.c_str());
}
int getReceiverID(){
return receiverID;
}
};
int main()
{
Receiver* rec =new NormalReceiver();
ReceiverManager* manager = ReceiverManager::getInstance();
manager->receivers[rec->getReceiverID()] = rec;
Command* cmd =new ConcreteCommand( ReceiverManager::NORMAL);
cmd->setArgs(" this is concretecommand");
Invoker*in=new Invoker();
in->cmd = cmd;
cmd->excute();
return0;
}