C++ features by examples
patterns.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
66template <typename ValueType>
69 virtual void set(ValueType i) = 0;
70 virtual ~Setter_interface() = default;
71};
72
73template <typename ValueType>
76 virtual ValueType get() const = 0;
77 virtual ~Getter_interface() = default;
78};
79
80template <typename ValueType>
83 virtual void change(ValueType c) = 0;
84 virtual ~Change_interface() = default;
85};
86
87template <typename ValueType>
94 : public Setter_interface<ValueType>, public Getter_interface<ValueType>, public Change_interface<ValueType>
95{
96 void set(ValueType i) override {
97 scoped_lock writer_lock(mtx);
98 value = i;
99 }
100
101 ValueType get() const override {
102 shared_lock reader_lock(mtx);
103 return value;
104 }
105
106 void change(ValueType c) override {
107 scoped_lock writer_lock(mtx);
108 value += c;
109 }
110
111 mutable shared_mutex mtx;
112 ValueType value;
113};
114
121{
122 auto client = [] (Setter_interface<int>& s, Getter_interface<int>& g) {
123 s.set(1);
124 assert(g.get() == 1);
125 };
129 client(s, g);
130
131 auto client2 = [] (Setter_interface<string>& s, Getter_interface<string>& g) {
132 s.set("abc");
133 assert(g.get() == "abc");
134 };
139 client2(s2, g2);
140 c2.change("de");
141 assert(g2.get() == "abcde");
142}
143
145
167{
168 virtual int method() = 0;
169 virtual ~Interface() = default;
170};
171
186#define SINGLETON(Singleton) \
187public: \
188 /* Meyers Singleton realization */ \
189 static Singleton& instance() { \
190 static Singleton me; \
191 return me; \
192 } \
193 Singleton(const Singleton&) = delete; \
194 Singleton& operator=(const Singleton&) = delete; \
195 Singleton(Singleton&&) = delete; \
196 Singleton& operator=(Singleton&&) = delete; \
197private: \
198 Singleton()
199
201{
203};
204
206{
207 virtual unique_ptr<Interface> factory_method() = 0;
208
209 int client() {
210 auto p(factory_method());
211 return p->method();
212 };
213};
214
216 : Interface
217{
218 int data;
219 int method() override { return data; }
220 Sample_product(int d = 0) : data(d) {}
221};
222
225{
226 unique_ptr<Interface> factory_method() override {
227 return make_unique<Sample_product>(123);
228 }
229};
230
232{
233 virtual unique_ptr<Interface> create() = 0;
234};
235
238{
239 virtual unique_ptr<Interface> create() {
240 return make_unique<Sample_product>();
241 }
242};
243
247{
248
249 int method() override { return 1; }
250 unique_ptr<Interface> create() override {
251 auto clone = new Prototype(*this);
252 return unique_ptr<Interface>(clone);
253 }
254};
255
257{
258 int data = 0;
259 Builder& add(int i) {
260 data += i;
261 return *this;
262 }
263
264 Builder& operator <<(int i) { return add(i); }
265
267 {
268 return *new Sample_product(data);
269 }
270};
271
273{
274 Singleton_demo& singe = Singleton_demo::instance();
275
276 unique_ptr<Abstract_factory> factory(new Sample_factory());
277
278 auto product = factory->create();
279
280 Prototype p1;
281 auto p2 = p1.create();
282
284 assert(C.client() == 123);
285
286 Interface& p = (Builder().add(1).add(2) << 3 << 4).create();
287 assert(p.method() == 10);
288 // strstream looks like string builder
289 delete &p;
290}
291
293