-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_pattern.cc
More file actions
61 lines (55 loc) · 1.13 KB
/
command_pattern.cc
File metadata and controls
61 lines (55 loc) · 1.13 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
/*************************************************************************
> File Name: command_pattern.cc
> Author: Yikang Chen
> Mail: 472757599@qq.com
> Created Time: Tue 10 Nov 2015 07:17:24 PM CST
************************************************************************/
#include <iostream>
using namespace std;
#define SAFE_DELETE(p) if(p){delete p; p = nullptr;}
class Receiver{
public:
void Action()
{
cout << "Action" << endl;
}
};
class Command{
public:
virtual void Excute()=0;
virtual ~Command(){}
protected:
Receiver* rp;
};
class ConcreteCommand : public Command{
public:
ConcreteCommand(Receiver* p)// : Command(p){}
{
rp = p;
}
void Excute()
{
rp->Action();
}
};
class Invoker{
public:
Invoker(Command* p) : cmm_ptr_(p) {}
void Invoke()
{
cmm_ptr_->Excute();
}
private:
Command* cmm_ptr_;
};
int main()
{
Receiver* rec_ptr = new Receiver();
Command* com_ptr = new ConcreteCommand(rec_ptr);
Invoker* inv_ptr = new Invoker(com_ptr);
inv_ptr->Invoke();
SAFE_DELETE(inv_ptr);
SAFE_DELETE(com_ptr);
SAFE_DELETE(rec_ptr);
return 0;
}