-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample.cpp
More file actions
112 lines (95 loc) · 3.69 KB
/
Copy pathConceptualExample.cpp
File metadata and controls
112 lines (95 loc) · 3.69 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ===========================================================================
// ConceptualExample.cpp // Facade
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
namespace ConceptualExample {
/**
* The Subsystem can accept requests either from the facade or client directly.
* In any case, to the Subsystem, the Facade is yet another client.
*/
class Subsystem1 {
public:
Subsystem1() { std::cout << "Subsystem1: Created." << std::endl; }
~Subsystem1() { std::cout << "Subsystem1: Released." << std::endl; }
std::string complexOperation1() const {
return "Subsystem1: Initialized!\n";
}
std::string complexOperation2() const {
return "Subsystem1: Works!\n";
}
};
/**
* Some facades can work with multiple subsystems at the same time.
*/
class Subsystem2 {
public:
Subsystem2() { std::cout << "Subsystem2: Created." << std::endl; }
~Subsystem2() { std::cout << "Subsystem2: Released." << std::endl; }
std::string complexOperation1() const {
return "Subsystem2: Initialized!\n";
}
std::string complexOperation2() const {
return "Subsystem2: Works too!\n";
}
};
/**
* The Facade class provides a simple interface to the complex logic of one or
* several subsystems. The Facade delegates the client requests to the
* appropriate objects within the subsystem. The Facade is also responsible for
* managing their lifecycle. All of this shields the client from the undesired
* complexity of the subsystem.
*/
class Facade {
private:
std::shared_ptr<Subsystem1> m_subsystem1;
std::shared_ptr<Subsystem2> m_subsystem2;
/**
* Depending on your application's needs, you can provide the Facade with
* existing subsystem objects or force the Facade to create them on its own.
*/
public:
/**
* In this case we will delegate the memory ownership to Facade Class
*/
Facade() {
m_subsystem1 = std::make_shared<Subsystem1>();
m_subsystem2 = std::make_shared<Subsystem2>();
}
~Facade() {}
/**
* The Facade's methods are convenient shortcuts to the sophisticated
* functionality of the subsystems. However, clients get only to a fraction of
* a subsystem's capabilities.
*/
std::string simpleOperation() const {
std::string result = "Facade initializes subsystems:\n";
result += m_subsystem1->complexOperation1();
result += m_subsystem2->complexOperation1();
result += "Facade orders subsystems to perform actions:\n";
result += m_subsystem1->complexOperation2();
result += m_subsystem2->complexOperation2();
return result;
}
};
/**
* The client code works with complex subsystems through a simple interface
* provided by the Facade. When a facade manages the lifecycle of the subsystem,
* the client might not even know about the existence of the subsystem. This
* approach lets you keep the complexity under control.
*/
static void clientCode(const Facade& facade) {
// ...
std::cout << facade.simpleOperation();
// ...
}
}
void test_conceptual_example () {
using namespace ConceptualExample;
Facade facade;
clientCode(facade);
}
// ===========================================================================
// End-of-File
// ===========================================================================