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
308{
309 float standalone_method() const {
310 return 1.01;
311 }
312};
313
314struct Bridge
316 : public Interface
317{
318 Bridge(Standalone& s): standalone(s) {
319// trace(typeid(*this).name());
320 }
321 int method() override {
322 return this->standalone.standalone_method();
323 }
324private:
325 Standalone& standalone;
326};
327
328struct Proxy
330 : public Interface
331{
332 Proxy(Interface& o): subject(o) {}
333 int method() override {
334 return this->subject.method();
335 }
336private:
337 Interface& subject;
338};
339
342 : public Interface
343{
345 int method() override {
346 return 100 + this->subject.method();
347 }
348 Interface& subject; // decorated object is public
349};
350
352 : public Interface
353{
354 int method() override {
355 //trace();
356 for (Interface& i : children) i.method();
357 return 0;
358 }
359 forward_list<reference_wrapper<Interface>> children;
360};
361
363{
364 Standalone sa;
365 Bridge br(sa);
366 br.method();
367 Proxy p(br);
368 Decorator dec(br);
369 dec.method();
370 dec.subject.method();
371 p.method();
372 Composite comp;
373 comp.children.push_front(p);
374 comp.method();
375}
376
378
392struct Subject;
393
396{
398 virtual void notify() {};
400 virtual void update(Subject& subject) {};
401 virtual ~Observer() = default;
402};
403
406{
408 for (Observer& o : observers) {
409 o.notify();
410 o.update(*this);
411 }
412 }
413 forward_list<reference_wrapper<Observer>> observers;
414};
415
423{
424 virtual int execute() { return -1; };
425};
426
427
436struct Visitor;
437
440{
441 virtual string component_accept(Visitor&) const = 0;
442 virtual ~Component() = default;
443};
444
445struct Sample_component;
448{
450 virtual string visit(const Sample_component&) const = 0;
451 virtual ~Visitor() = default;
452};
453
455 : public Component
459{
460 string component_accept(Visitor& visitor) const override {
461 return string(__func__) + " > " + visitor.visit(*this);
462 }
464 string component_method() const {
465 return __func__;
466 }
467};
468
469string client_visit(const forward_list<unique_ptr<Component>>& components,
470 const forward_list<unique_ptr<Visitor>>& visitors)
472{
473 string res;
474 for (auto&& comp : components)
475 for (auto&& vis : visitors) {
476 res += string(__func__) + " > " + comp->component_accept(*vis.get());
477 }
478 return res;
479}
480
492{
495 struct Sample_visitor
496 : public Visitor {
498 string visit(const Sample_component& c) const override {
499 return string(__func__) + " > " + c.component_method();
500 }
501 };
502
503 forward_list<unique_ptr<Component>> components;
504 components.emplace_front(new Sample_component);
505 Sample_visitor v;
506 forward_list<unique_ptr<Visitor>> visitors;
507 visitors.emplace_front(new Sample_visitor);
508 assert(client_visit(components, visitors) ==
509 "client_visit > component_accept > visit > component_method");
510}
511
513
516{
518 virtual int handle(Command& cmnd) { return cmnd.execute(); };
519 virtual ~Handler() = default;
520};
521
523 : Handler
531{
532 void register_handler(Handler&& h, bool front = false) {
533 if (front)
534 handlers.push_front(h);
535 else
536 handlers.push_back(h);
537 }
538 int handle(Command& cmnd) override {
539 int rc = -1;
540 for (Handler& h : handlers)
541 if ((rc = h.handle(cmnd)) >= 0)
542 return rc;
543 return rc;
544 }
545private:
546 list<reference_wrapper<Handler>> handlers;
547};
548
549struct Message { };
550
557struct Mediator;
558