-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandPattern.h
More file actions
55 lines (52 loc) · 1006 Bytes
/
CommandPattern.h
File metadata and controls
55 lines (52 loc) · 1006 Bytes
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
#ifndef COMMANDPATTERN_H_INCLUDED
#define COMMANDPATTERN_H_INCLUDED
#include <iostream>
#include <vector>
//Command class
class Command {
protected:
public:
virtual void Execute() = 0;
};
//Receiver class
class Receiver {
protected:
public:
void Buy() {std::cout<<"Buy stocks\n";}
void Sell() {std::cout<<"Sell stocks\n";}
};
//Concrete class 1
class Buy : public Command {
protected:
Receiver *receiver;
public:
Buy(Receiver *receiver) {
this->receiver = receiver;
}
void Execute() {
receiver->Buy();
}
};
//Concrete class 2
class Sell : public Command {
protected:
Receiver *receiver;
public:
Sell(Receiver *receiver) {
this->receiver = receiver;
}
void Execute() {
receiver->Sell();
}
};
//Invoker class
class Invoker {
protected:
std::vector<Command*> command_list;
public:
void Order(Command *cmd) {
command_list.push_back(cmd);
cmd->Execute();
}
};
#endif // COMMANDPATTERN_H_INCLUDED