两种语言实现设计模式(C++和Java)(十三:命令模式)

命令模式将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行储存、传递、调用、增加与管理。

实际上,命令模式就是实现请求和执行者之间的解耦。图中Invoker实现接受和管理各种命令,然后统一向Receiver发起请求,所有类型的请求封装在Command中。例如在餐厅点菜,只需要和Waiter说,而不用和做菜的人说。

 

C++ 实现:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 class Cooker{
 7 public:
 8     void cookBeef(){
 9         cout << "Cooker cook beef." << endl;
10     }
11     void cookChicken(){
12         cout << "Cooker cook chicken" << endl;
13     }
14 };
15 
16 class Command{
17 protected:
18     Cooker* cooker;
19 public:
20     Command(Cooker* _cooker){
21         cooker = _cooker;
22     }
23     virtual void executeCmd() = 0;
24 };
25 
26 class CommandBeef:public Command{
27 public:
28     CommandBeef(Cooker* _cooker):Command(_cooker){}
29     void executeCmd(){
30         cooker->cookBeef();
31     }
32 };
33 
34 class CommandChicken:public Command{
35 public:
36     CommandChicken(Cooker* _cooker):Command(_cooker){}
37     void executeCmd(){
38         cooker->cookChicken();
39     }
40 };
41 
42 class Waiter{
43 private:
44     vector<Command*> vec_cmd;
45 public:
46     ~Waiter(){
47         for(auto it=vec_cmd.begin(); it != vec_cmd.end(); it++){
48             delete (*it);
49         }
50         vec_cmd.clear();
51     }
52     void add(Command* command){
53         vec_cmd.push_back(command);
54     }
55     void remove(Command* command){
56         auto it = find(vec_cmd.begin(), vec_cmd.end(), command);
57         if (it != vec_cmd.end()){
58             vec_cmd.erase(it);
59         }
60     }
61     void subCmd(){
62         for (auto it=vec_cmd.begin(); it != vec_cmd.end(); it++){
63             (*it)->executeCmd();
64         }
65     }
66 };
67 
68 int main()
69 {
70     Waiter *waiter = new Waiter();
71     Cooker *cooker = new Cooker();
72     waiter->add(new CommandBeef(cooker));
73     waiter->add(new CommandChicken(cooker));
74     waiter->subCmd();
75     delete waiter;
76     delete cooker;
77     return 0;
78 }

Java实现:

 1 public class Cooker {
 2 
 3     public void cookBeef(){
 4         System.out.println("Cooker cook beef");
 5     }
 6 
 7     public void cookChicken(){
 8         System.out.println("Cooker cook chicken");
 9     }
10 }
11 
12 public interface Command {
13 
14     public abstract void executeCmd();
15 }
16 
17 public class CommandBeef implements Command {
18 
19     private Cooker cooker;
20 
21     public CommandBeef(Cooker cooker){
22         this.cooker = cooker;
23     }
24 
25     public void executeCmd() {
26         cooker.cookBeef();
27     }
28 }
29 
30 public class CommandChicken implements Command{
31 
32     private Cooker cooker;
33 
34     public CommandChicken(Cooker cooker){
35         this.cooker = cooker;
36     }
37 
38     public void executeCmd(){
39         cooker.cookChicken();
40     }
41 }
42 
43 public class Waiter {
44 
45     private List<Command> commandList;
46 
47     public Waiter(){
48         commandList = new ArrayList<Command>();
49     }
50 
51     public void add(Command command){
52         this.commandList.add(command);
53     }
54 
55     public void subCommand(){
56         for (Command command:
57              commandList) {
58             command.executeCmd();
59         }
60     }
61 }
62 
63 public class Main {
64 
65     public static void main(String[] args) {
66         Cooker cooker = new Cooker();
67         Command command1 = new CommandBeef(cooker);
68         Command command2 = new CommandChicken(cooker);
69         Waiter waiter = new Waiter();
70         waiter.add(command1);
71         waiter.add(command2);
72         waiter.subCommand();
73     }
74 
75 }

 

posted @ 2019-07-29 21:47  Asp1rant  阅读(203)  评论(0编辑  收藏  举报