设计模式C++实现(9)——命令模式


一、简介

         Command命令模式为了解决的问题是:当系统请求繁多复杂时,将请求封装到一个对象(Command)中,并将请求的接受者存放到具体的ConcreteCommand类(Receiver)中,从而实现调用操作的对象和操作的具体实现者之间的解耦。

         Command 模式的典型结构图为:

       Command模式结构图中,将请求的接收者(处理者)放到Command的具体子类ConcreteCommand中,当请求到来时(Invoker 发出Invoke消息激活Command对象),ConcreteCommand将处理请求交给Receiver对象进行处理。

二、详解

1、正常对象参数传递调用

(1)代码reveiver.h:

[html]  view plain  copy
  1. #ifndef _RECEIVER_H_  
  2. #define _RECEIVER_H_  
  3.   
  4. class Receiver  
  5. {  
  6.       public:  
  7.           Receiver();  
  8.           ~Receiver();  
  9.           void Action();  
  10.       protected:  
  11.       private:  
  12. };  
  13.   
  14. #endif  

(2)代码reveiver.cpp:

[html]  view plain  copy
  1. #include <iostream>  
  2. #include "receiver.h"  
  3. using namespace std;  
  4.   
  5. Receiver::Receiver()  
  6. {  
  7. }  
  8.   
  9. Receiver::~Receiver()  
  10. {  
  11. }  
  12.   
  13. void Receiver::Action()  
  14. {  
  15.        cout<<"---Receiver::Action---"<<endl;  
  16. }  

(3)代码invoker.h:

[html]  view plain  copy
  1. #ifndef _INVOKER_H_  
  2. #define _INVOKER_H_  
  3.   
  4. class Command;  
  5. class Invoker  
  6. {  
  7.       public:  
  8.           Invoker(Command *cmd);  
  9.           ~Invoker();  
  10.           void InvokeFunc();  
  11.       protected:  
  12.       private:  
  13.           Command *_cmd;  
  14. };  
  15.   
  16. #endif  

(4)代码invoker.cpp:

[html]  view plain  copy
  1. #include <iostream>  
  2. #include "invoker.h"  
  3. #include "command.h"  
  4. using namespace std;  
  5.   
  6. Invoker::Invoker(Command *cmd)  
  7. {  
  8.     _cmd = cmd;  
  9. }  
  10.   
  11. Invoker::~Invoker()  
  12. {  
  13. }  
  14.   
  15. void Invoker::InvokeFunc()  
  16. {  
  17.       cout<<"Invoker::InvokeFunc"<<endl;  
  18.       _cmd->Excute();  
  19. }  
(5)代码command.h:
[html]  view plain  copy
  1. #ifndef _COMMAND_H_  
  2. #define _COMMAND_H_  
  3.   
  4. class Receiver;  
  5. class Command  
  6. {  
  7.       public:  
  8.           virtual ~Command();  
  9.           virtual void Excute() = 0;  
  10.       protected:  
  11.           Command();  
  12.       private:  
  13. };  
  14. class ConcreteCommand : public Command  
  15. {  
  16.       public:  
  17.           ConcreteCommand(Receiver *rev);  
  18.           ~ConcreteCommand();  
  19.           void Excute();  
  20.       protected:  
  21.       private:  
  22.           Receiver *_rev;  
  23. };  
  24.   
  25. #endif  
(6)代码command.cpp:
[html]  view plain  copy
  1. #include <iostream>  
  2. #include "command.h"  
  3. #include "receiver.h"  
  4. using namespace std;  
  5.   
  6. Command::Command()  
  7. {  
  8. }  
  9.   
  10. Command::~Command()  
  11. {  
  12. }  
  13.   
  14. ConcreteCommand::ConcreteCommand(Receiver *rev)  
  15. {  
  16.       _rev = rev;  
  17. }  
  18.   
  19. ConcreteCommand::~ConcreteCommand()  
  20. {  
  21. }  
  22.   
  23. void ConcreteCommand::Excute()  
  24. {  
  25.       cout<<"ConcreteCommand::Excute"<<endl;  
  26.       _rev->Action();  
  27. }  
(7)代码main.cpp:

[html]  view plain  copy
  1. #include <iostream>  
  2. #include "receiver.h"  
  3. #include "invoker.h"  
  4. #include "command.h"  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.       //创建具体命令对象cmd并设定它的接收者rev  
  10.       Receiver *rev = new Receiver();  
  11.       Command *cmd = new ConcreteCommand(rev);  
  12.       //请求绑定命令  
  13.       Invoker *inv = new Invoker(cmd);  
  14.       inv->InvokeFunc();  
  15.   
  16.           delete inv;  
  17.           delete cmd;  
  18.           delete rev;  
  19.       return 0;  
  20. }  
(8)代码makefile:
[html]  view plain  copy
  1. CFLAGS = -g  
  2. DEFINED = #-D _VERSION  
  3. LIBS =   
  4. CC = g++  
  5. INCLUDES = -I./  
  6. OBJSmain.o receiver.o command.o invoker.o  
  7. TARGETmain  
  8. all:$(TARGET)  
  9.   
  10. $(TARGET):$(OBJS)  
  11.     $(CC) $(CFLAGS) -o $@ $(OBJS)  
  12.   
  13. .SUFFIXES:.o .h  
  14. .SUFFIXES:.cpp .o  
  15. .cpp.o:  
  16.     $(CC) $(DEFINED) -c $(CFLAGS) -o $@ $<  
  17.   
  18. ok:  
  19.     ./$(TARGET)  
  20. clean:  
  21.     rm -f $(OBJS) $(TARGET) core *.log  

2、函数地址回调机制调用

(1)代码receiver.h:

[html]  view plain  copy
  1. #ifndef _RECEIVER_H_  
  2. #define _RECEIVER_H_  
  3.   
  4. class Receiver  
  5. {  
  6.       public:  
  7.           Receiver();  
  8.           ~Receiver();  
  9.           void Action();  
  10.       protected:  
  11.       private:  
  12. };  
  13.   
  14. #endif  

(2)代码receiver.cpp:

[html]  view plain  copy
  1. #include <iostream>  
  2. #include "receiver.h"  
  3. using namespace std;  
  4.   
  5. Receiver::Receiver()  
  6. {  
  7. }  
  8.   
  9. Receiver::~Receiver()  
  10. {  
  11. }  
  12.   
  13. void Receiver::Action()  
  14. {  
  15.        cout<<"---Receiver::Action---"<<endl;  
  16. }  

(3)代码command.h:

[html]  view plain  copy
  1. #ifndef _COMMAND_H_  
  2. #define _COMMAND_H_  
  3.   
  4. class Command  
  5. {  
  6.       public:  
  7.           virtual ~Command(){};  
  8.           virtual void Excute() = 0;  
  9.       protected:  
  10.           Command(){};  
  11.       private:  
  12. };  
  13.   
  14. template <class T>  
  15. class SimpleCommand : public Command  
  16. {  
  17.       public:  
  18.           typedef void (T::*ActionFunc)();  
  19.           SimpleCommand(T *rev, ActionFunc act) {  
  20.               _rev = rev;  
  21.               _act = act;  
  22.           }  
  23.           virtual void Excute() {  
  24.               (_rev->*_act)();     //回调调用方式  
  25.           }  
  26.           ~SimpleCommand() {  
  27.               delete _rev;  
  28.           }  
  29.       protected:  
  30.       private:  
  31.           T *_rev;  
  32.           ActionFunc _act;  
  33. };  
  34.   
  35. #endif  
(4)代码main.cpp:
[html]  view plain  copy
  1. #include <iostream>  
  2. #include "command.h"  
  3. #include "receiver.h"  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.       Receiver *rev = new Receiver();  
  9.       Command *cmd = new SimpleCommand<Receiver>(rev, &Receiver::Action);  
  10.       cmd->Excute();  
  11.         
  12.       delete cmd;  
  13.       return 0;  
  14. }  
(4)makefile:
[html]  view plain  copy
  1. CFLAGS = -g  
  2. DEFINED = #-D _VERSION  
  3. LIBS =   
  4. CC = g++  
  5. INCLUDES = -I./  
  6. OBJSmain.o receiver.o  
  7. TARGETmain  
  8. all:$(TARGET)  
  9.   
  10. $(TARGET):$(OBJS)  
  11.     $(CC) $(CFLAGS) -o $@ $(OBJS)  
  12.   
  13. .SUFFIXES:.o .h  
  14. .SUFFIXES:.cpp .o  
  15. .cpp.o:  
  16.     $(CC) $(DEFINED) -c $(CFLAGS) -o $@ $<  
  17.   
  18. ok:  
  19.     ./$(TARGET)  
  20. clean:  
  21.     rm -f $(OBJS) $(TARGET) core *.log  


3、运行结果

(Centos6.3系统中运行结果:)

      Command模式的实现和思想都很简单,其关键就是将一个请求封装到一个类(Command)中,再提供处理对象(Receiver),最后Command命令由Invoker激活。

       在上述2中用到 C++中的类成员函数指针,将请求接收者的处理抽象出来作为参数传给Command对象,实际也就是回调的机制(Callback)来实现这一点,即将处理操作方法地址(在对象内部)通过参数传递给Command对象,Command在适当的时候(Invoke激活的时候)再调用该函数。

三、总结

(1)Command模式关键就是提供一个抽象的Command类,并将执行操作封装到Command类接口中,Command类中一般就是只是一些接口的集合,并不包含任何的数据属性。

(2)Command模式将调用操作的对象和知道如何实现该操作的对象解耦,在上述结构图中Invoker对象根本就不知道具体的是那个对象在处理Excute操作(当然要知道是Command类别的对象,也仅此而已)。
(3)在Command要增加新的处理对象很容易,可以通过创建新的继承自Command的子类来实现这一点。

(4)Command模式可以和Memento模式结合起来,支持取消的操作。

(5)源码已经打包上传到csdn上可登录下载(http://download.csdn.net/detail/taiyang1987912/8437165)。 








本文转自:

http://blog.csdn.net/taiyang1987912/article/details/43567077







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适配器设计模式是一种结构型设计模式,它用于将一个类的接口转换为客户端所期望的另一个接口。在C语言中,可以通过以下步骤来实现适配器设计模式: 1. 首先,定义目标接口(Target Interface),这是客户端期望的接口。它可以是一个抽象基类或者一个纯虚函数。 ```c // 目标接口 typedef struct { void (*request)(void); } TargetInterface; ``` 2. 接下来,实现需要适配的类(Adaptee Class)。这个类拥有与目标接口不同的接口。 ```c // 需要适配的类 typedef struct { void (*specificRequest)(void); } AdapteeClass; void specificRequestImpl(void) { // 执行特定的操作 } ``` 3. 创建适配器类(Adapter Class),该类继承或包含目标接口,并将其方法委托给适配的类。 ```c // 适配器类 typedef struct { TargetInterface targetInterface; AdapteeClass* adaptee;} AdapterClass; void requestImpl(void) { AdapterClass* adapter = (AdapterClass*)this; adapter->adaptee->specificRequest(); } TargetInterface* createAdapter(AdapteeClass* adaptee) { AdapterClass* adapter = (AdapterClass*)malloc(sizeof(AdapterClass)); adapter->targetInterface.request = requestImpl; adapter->adaptee = adaptee; return &(adapter->targetInterface); } ``` 4. 最后,客户端可以通过目标接口来使用适配器。 ```c int main() { AdapteeClass adaptee; adaptee.specificRequest = specificRequestImpl; TargetInterface* adapter = createAdapter(&adaptee); adapter->request(); return 0; } ``` 上述代码演示了适配器设计模式的简单实现。通过适配器类的创建和使用,客户端可以通过目标接口调用适配的类的方法,实现了将两个不兼容接口之间的适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值