forked from mrchuanxu/RegularNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandMode.cpp
More file actions
85 lines (74 loc) · 1.99 KB
/
commandMode.cpp
File metadata and controls
85 lines (74 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/***
* 命令模式
* 将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化
* 对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能
* ***/
// 先抽象一个group
class Group{
public:
virtual void findGroup() = 0;
virtual void addFeature() = 0;
virtual void deleteFeature()= 0;
virtual void changeFeature() = 0;
virtual void plan() = 0;
};
class RequirementGroup:public Group{
public:
virtual void findGroup();
virtual void addFeature();
virtual void deleteFeature();
virtual void changeFeature();
virtual void plan();
};
class PageGroup:public Group{
public:
virtual void findGroup();
virtual void addFeature();
virtual void deleteFeature();
virtual void changeFeature();
virtual void plan();
};
class CodeGroup:public Group{
public:
virtual void findGroup();
virtual void addFeature();
virtual void deleteFeature();
virtual void changeFeature();
virtual void plan();
};
// 这时我需要抽象一个命令类,拿上面的下来用
class Command{
protected:
RequirementGroup *rg = new RequirementGroup();
PageGroup *pg = new PageGroup();
CodeGroup *cg = new CodeGroup();
public:
virtual void execute();
};
// 继承命令
class AddRequirementCommand:public Command{
public:
virtual void execute() override{ // 执行命令
Command::cg->findGroup();
Command::rg->findGroup();
}
};
// 负责人invoker
class Invoker{
private:
Command *command;
public:
void setCommand(Command *refcommand){
command = refcommand;
}
void action(){
command->execute();
}
};
int main(){
Invoker *trans = new Invoker();
Command *command=new AddRequirementCommand();
trans->setCommand(command);
trans->action();
return 0;
}