命令模式——示例——数据结构

依 CSDN刘伟技术博客,C++命令模式示例如下:

 1 // include/Command.h
 2 
 3 #ifndef __COMMAND__                                                                                                
 4 #define __COMMAND__
 5 
 6 #include <WindowHandler.h>
 7 #include <HelpHandler.h>
 8 
 9 //抽象命令类
10 class Command 
11 {
12     public: 
13         virtual void execute(void) = 0;
14         virtual ~Command(void){}
15 };
16 
17 
18 //最小化命令类:具体命令类
19 class MinimizeCommand: public Command 
20 {
21     private:
22         WindowHandler* whObj; //维持对请求接收者的指针
23 
24     public: 
25         MinimizeCommand(void);
26 
27 //命令执行方法:将调用请求接收者的业务方法
28         void execute(void);
29 
30         ~MinimizeCommand(void);
31 };
32 
33 //帮助命令类:具体命令类
34 class HelpCommand: public Command 
35 {
36     private:
37         HelpHandler* hhObj; //维持对请求接收者的指针
38 
39     public: 
40         HelpCommand(void);
41 
42 //命令执行方法:将调用请求接收者的业务方法
43         void execute(void);
44 
45         ~HelpCommand(void);
46 };
47 
48 #endif  
 1 // include/FunctionButton.h
 2 
 3 #ifndef __FUCNCTIONBUTTON__                                                                                        
 4 #define __FUCNCTIONBUTTON__
 5 
 6 #include <Command.h>
 7 //功能键类:请求发送者
 8 class FunctionButton
 9 {
10     private:
11         char *name; //功能键名称
12         Command* command;//维持一个抽象命令对象指针
13 
14     public: 
15         FunctionButton(const char* str);
16 
17         const char* getName(void)const;
18 
19 //为功能键注入命令
20         void setCommand(Command* cmd);
21     
22 //发送请求的方法
23         void onClick(void);
24 
25         ~FunctionButton(void);
26 };
27 
28 #endif
 1 // include/FunctionButton.h
 2 
 3 #ifndef __FUCNCTIONBUTTON__                                                                                        
 4 #define __FUCNCTIONBUTTON__
 5 
 6 #include <Command.h>
 7 //功能键类:请求发送者
 8 class FunctionButton
 9 {
10     private:
11         char *name; //功能键名称
12         Command* command;//维持一个抽象命令对象指针
13 
14     public: 
15         FunctionButton(const char* str);
16 
17         const char* getName(void)const;
18 
19 //为功能键注入命令
20         void setCommand(Command* cmd);
21     
22 //发送请求的方法
23         void onClick(void);
24 
25         ~FunctionButton(void);
26 };
27 
28 #endif
 1 // include/FBSettingWindow.h
 2 
 3 #ifndef __FBSETTINGWINDOW__                                                                                        
 4 #define __FBSETTINGWINDOW__
 5 
 6 #include <FunctionButton.h>
 7 //功能设置窗口类
 8 class FBSettingWindow
 9 {
10     private:
11         char* title; //窗口标题
12         //定义一个ArrayList来存储所有功能键
13         FunctionButton* functionButtons[16];
14 
15     public: 
16         FBSettingWindow(void);
17 
18         FBSettingWindow(const char* str);
19 
20         void setTitle(const char *str);
21 
22         const char* getTitle(void)const;
23 
24         void addFunctionButton(FunctionButton* fb);
25 
26         void removeFunctionButton(FunctionButton* fb);
27 
28 //显示窗口及功能键
29         void display(void);
30 
31         ~FBSettingWindow(void);
32 
33 };
34 #endif
 1 // include/HelpHandler.h
 2 
 3 #ifndef __HELPHANDLER__                                                                                            
 4 #define __HELPHANDLER__
 5 
 6 //帮助文档处理类:请求接收者
 7 class HelpHandler
 8 {
 9     public: 
10         void display(void);
11 };
12 
13 #endif
 1 // include/WindowHandler.h
 2 
 3 #ifndef __WINDOWHANDLER__                                                                                          
 4 #define __WINDOWHANDLER__
 5 
 6 
 7 //窗口处理类:请求接收者
 8 class WindowHandler
 9 {
10     public: 
11         void minimize(void);
12 };
13 
14 #endif
原文地址:https://www.cnblogs.com/openix/p/3132009.html