-
Notifications
You must be signed in to change notification settings - Fork 11
/
GoF.h
101 lines (85 loc) · 1.86 KB
/
GoF.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include <vector>
namespace CommandPattern::GoF
{
class Command {
public:
virtual ~Command() {};
virtual void Execute() = 0;
protected:
Command() {};
};
class Document;
class Application {
public:
void Add(Document*) {}
};
class OpenCommand : public Command {
public:
OpenCommand(Application*);
virtual void Execute();
protected:
virtual const char* AskUser() { return nullptr; };
private:
Application* _application;
char* _response;
};
OpenCommand::OpenCommand(Application* a) {
_application = a;
}
class Document {
public:
Document(const char* name) {}
void Open() {}
void Paste() {}
};
void OpenCommand::Execute() {
const char* name = AskUser();
if (name != 0) {
Document* document = new Document(name);
_application->Add(document);
document->Open();
}
}
class PasteCommand : public Command {
public:
PasteCommand(Document*);
virtual void Execute();
private:
Document* _document;
};
PasteCommand::PasteCommand(Document* doc) { _document = doc; }
void PasteCommand::Execute() { _document->Paste(); }
class Receiver;
class SimpleCommand : public Command {
public:
typedef void (Receiver::* Action)();
SimpleCommand(Receiver* r, Action a) :
_receiver(r), _action(a) { }
virtual void Execute();
private:
Action _action;
Receiver* _receiver;
};
void SimpleCommand::Execute() { (_receiver->*_action)(); }
class MacroCommand : public Command {
public:
MacroCommand() {};
virtual ~MacroCommand() {};
virtual void Add(Command*);
virtual void Remove(Command*);
virtual void Execute();
private:
std::vector<Command*>* _cmds;
};
void MacroCommand::Execute() {
auto i(_cmds->begin());
for (; i != _cmds->end(); ++i) {
(*i)->Execute();
}
}
void MacroCommand::Add(Command* c) { _cmds->push_back(c); }
void MacroCommand::Remove(Command* c) {
//_cmds->erase(c);
}
}