zoukankan      html  css  js  c++  java
  • 命令模式——示例——实现

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

    1
    // Client.cpp 2 3 #include <iostream>
     4 #include <string.h> 5 6 #include <FBSettingWindow.h> 7 #include <FunctionButton.h> 8 #include <Command.h> 9 10 int main(void) 11 { 12 //面板 13 FBSettingWindow* fbsw = new FBSettingWindow("function key set panel"); 14 15 //键号 16 FunctionButton* fb1 = new FunctionButton("KEY 1"); 17 FunctionButton* fb2 = new FunctionButton("KEY 2"); 18 19 //功能 20 Command* command1 = new HelpCommand; 21 Command* command2 = new MinimizeCommand; 22 23 //键号--功能 24 fb1->setCommand(command1); 25 fb2->setCommand(command2); 26 27 //面把上添加功能键 28 fbsw->addFunctionButton(fb1); 29 fbsw->addFunctionButton(fb2); 30 31 //调用功能键的业务方法 32 fb1->onClick(); 33 fb2->onClick(); 34 35 delete command2; 36 delete command1; 37 delete fb2; 38 delete fb1; 39 delete fbsw; 40 return 0; 41 }
     1 // Command.cpp
     2 
     3 #include <iostream>
     4 #include <string.h>
     5 
     6 #include <Command.h>
     7 using namespace std;
     8 
     9 MinimizeCommand::MinimizeCommand(void): whObj(NULL)
    10 {
    11     whObj = new WindowHandler;
    12 }
    13 
    14 MinimizeCommand::~MinimizeCommand(void)
    15 {
    16     if(whObj)
    17         delete whObj;
    18 }
    19 
    20 //命令执行方法:将调用请求接收者的业务方法
    21 void MinimizeCommand::execute(void)
    22 {
    23     whObj->minimize();
    24 }
    25 
    26 HelpCommand::HelpCommand(void): hhObj(NULL)
    27 {
    28     hhObj = new HelpHandler;
    29 }
    30 
    31 //命令执行方法:将调用请求接收者的业务方法
    32 void HelpCommand::execute(void)
    33 {
    34     hhObj->display();
    35 }
    36 
    37 HelpCommand::~HelpCommand(void)
    38 {
    39     if(hhObj)
    40         delete hhObj;
    41 }                
     1 // FBSettingWindow.cpp
     2 
     3 #include <iostream>
     4 #include <string.h>
     5 
     6 #include <FBSettingWindow.h>
     7 using namespace std;
     8 //功能设置窗口类
     9 FBSettingWindow::FBSettingWindow(void):title(NULL), functionButtons({NULL}) {}
    10                                                                                                                    
    11 FBSettingWindow::~FBSettingWindow(void)
    12 {
    13     if(title)
    14         delete[]title;
    15 }
    16 
    17 FBSettingWindow::FBSettingWindow(const char* str): title(NULL), functionButtons({NULL})
    18 {
    19     title = new char[strlen(str) + 1];
    20     strcpy(title, str);
    21 }
    22 
    23 void FBSettingWindow::setTitle(const char *str)
    24 {
    25     if(str != NULL)
    26         delete[] str;
    27     title = new char[strlen(str) + 1];
    28     strcpy(title, str);
    29 }
    30 
    31 const char* FBSettingWindow::getTitle(void)const
    32 {
    33     return title;
    34 }
    35 
    36 void FBSettingWindow::addFunctionButton(FunctionButton* fb)
    37 {
    38     int size = sizeof(functionButtons) / sizeof(functionButtons[0]);
    39     int i = 0;
    40     while(i < size) 
    41     {
    42         if(functionButtons[i] == NULL)
    43             break;
    44         i++;
    45     }
    46 
    47     if(i < size)
    48         functionButtons[i] = fb;
    49 }
    50 
    51 void FBSettingWindow::removeFunctionButton(FunctionButton* fb)
    52 {
    53     int size = sizeof(functionButtons) / sizeof(functionButtons[0]);
    54     int i = 0;
    55     while(i < size)
    56     {
    57         if(functionButtons[i] == fb)
    58             break;
    59         i++;
    60     }
    61 
    62     if(i < size)
    63         functionButtons[i] = NULL;
    64 
    65 }
    66 
    67 //显示窗口及功能键
    68 void FBSettingWindow::display(void)
    69 {
    70     cout << "show window " << title << endl;
    71     cout << "show function keys" << endl;
    72     int size = sizeof(functionButtons) / sizeof(functionButtons[0]);
    73     int i = 0;
    74     while(i < size)
    75     {
    76         if(functionButtons[i] != NULL)
    77         {
    78             cout << functionButtons[i]->getName() << endl;
    79         }
    80         i++;
    81     }
    82 }
     1 // FunctionButton.cpp
     2 
     3 #include <iostream>
     4 #include <string.h>
     5 #include <FunctionButton.h>
     6 
     7 using namespace std;
     8 //功能键类:请求发送者
     9 FunctionButton::FunctionButton(const char* str):name(NULL), command(NULL)
    10 {
    11     name = new char[strlen(str) + 1];
    12     strcpy(name, str);
    13 }
    14                                                                                                                    
    15 FunctionButton::~FunctionButton(void)
    16 {
    17     if(name)
    18         delete[]name;
    19 }
    20 
    21 const char* FunctionButton::getName(void)const
    22 {
    23     return name;
    24 }
    25 
    26 //为功能键注入命令
    27 void FunctionButton::setCommand(Command* cmd)
    28 {
    29     command = cmd;
    30 }
    31     
    32 //发送请求的方法
    33 void FunctionButton::onClick(void)
    34 {
    35     cout << "click function key" << endl;
    36     command->execute();
    37 }
     1 // HelpHandler.cpp
     2 
     3 #include <iostream>                                                                                                
     4 #include <string.h>
     5 #include <HelpHandler.h>
     6 
     7 using namespace std;
     8 //帮助文档处理类:请求接收者
     9 void HelpHandler::display(void)
    10 {
    11     cout << "show help document" << endl;
    12 }
     1 // WindowHandler.cpp
     2 
     3 #include <iostream>                                                                                                
     4 #include <string.h>
     5 
     6 #include <WindowHandler.h>
     7 using namespace std;
     8 
     9 //窗口处理类:请求接收者
    10 void WindowHandler::minimize(void)
    11 {
    12     cout << "minimize window size" << endl;
    13 }
  • 相关阅读:
    Python 基础知识----数据类型
    drf 之序列化组件
    Django Rest framework 框架之解析器
    css选择器
    Python 爬虫 解析库的使用 --- Beautiful Soup
    Python 爬虫 解析库的使用 --- XPath
    动态渲染页面爬取(Python 网络爬虫) ---Selenium的使用
    HDU 1014(互质数 **)
    HDU 6432(不连续环排列 ~)
    HDU 6433(2的n次方 **)
  • 原文地址:https://www.cnblogs.com/openix/p/3132019.html
Copyright © 2011-2022 走看看