1#include <bits/stdc++.h>
66template <
typename ValueType>
69 virtual void set(ValueType i) = 0;
73template <
typename ValueType>
76 virtual ValueType
get()
const = 0;
80template <
typename ValueType>
83 virtual void change(ValueType c) = 0;
87template <
typename ValueType>
96 void set(ValueType i)
override {
97 scoped_lock writer_lock(mtx);
101 ValueType get()
const override {
102 shared_lock reader_lock(mtx);
106 void change(ValueType c)
override {
107 scoped_lock writer_lock(mtx);
111 mutable shared_mutex mtx;
124 assert(g.get() == 1);
133 assert(g.
get() ==
"abc");
141 assert(g2.
get() ==
"abcde");
186#define SINGLETON(Singleton) \
189 static Singleton& instance() { \
190 static Singleton me; \
193 Singleton(const Singleton&) = delete; \
194 Singleton& operator=(const Singleton&) = delete; \
195 Singleton(Singleton&&) = delete; \
196 Singleton& operator=(Singleton&&) = delete; \
227 return make_unique<Sample_product>(123);
233 virtual unique_ptr<Interface>
create() = 0;
240 return make_unique<Sample_product>();
250 unique_ptr<Interface>
create()
override {
252 return unique_ptr<Interface>(clone);
278 auto product = factory->create();
284 assert(C.
client() == 123);
334 return this->subject.
method();
359 forward_list<reference_wrapper<Interface>>
children;
461 return string(__func__) +
" > " + visitor.
visit(*
this);
469string client_visit(
const forward_list<unique_ptr<Component>>& components,
470 const forward_list<unique_ptr<Visitor>>& visitors)
474 for (
auto&& comp : components)
475 for (
auto&& vis : visitors) {
476 res += string(__func__) +
" > " + comp->component_accept(*vis.get());
495 struct Sample_visitor
503 forward_list<unique_ptr<Component>> components;
506 forward_list<unique_ptr<Visitor>> visitors;
507 visitors.emplace_front(
new Sample_visitor);
509 "client_visit > component_accept > visit > component_method");
534 handlers.push_front(h);
536 handlers.push_back(h);
541 if ((rc = h.handle(cmnd)) >= 0)
546 list<reference_wrapper<Handler>> handlers;
576 forward_list<reference_wrapper<Member>>
members;
637 int rc = cmnd.execute();
713 pub.
publish(
"sample_topic", m);
725template <
typename T,
class Container = std::queue<T>>
731 condition_variable
cv;
734 scoped_lock<mutex>{mtx}, Container::push(v);
738 optional<reference_wrapper<T>>
pull() {
739 unique_lock<mutex> lk(mtx);
740 cv.wait(lk, [&] {
return !this->empty() ||
stop; });
741 optional<reference_wrapper<T>> ret;
745 ret = make_optional(ref(Container::front()));
767 auto cmd =
cmd_q.pull();
768 if (!cmd.has_value())
776 cmd_q.cv.notify_one();
782 future f = p.get_future();
encapsulating class with only public accessor and mutator intrfaces
optional< reference_wrapper< T > > pull()
void architectural_patterns_demo()
void behavioral_patterns_demo()
void concurrency_patterns_demo()
void creational_patterns_demo()
void structural_patterns_demo()
string client_visit(const forward_list< unique_ptr< Component > > &components, const forward_list< unique_ptr< Visitor > > &visitors)
knows only virtual visitor and component
virtual unique_ptr< Interface > create()=0
function< void()> Command
Active_object(Interface &s)
Synchronized_queue< Command > cmd_q
is a wrapper using different from Standalone interface. AKA Adapter
Builder & operator<<(int i)
list based implementation without recursion
int handle(Command &cmnd) override
Specific handler can process a command and return non-negative.
void register_handler(Handler &&h, bool front=false)
virtual ~Change_interface()=default
virtual void change(ValueType c)=0
is a sample of getter abstract interface for Synchronised_encapsulated_value
encapsulates arguments. AKA Intent, operation.
accepts a pure virtual Visitor
virtual string component_accept(Visitor &) const =0
virtual ~Component()=default
forward_list< reference_wrapper< Interface > > children
is part of MVC with Model and View
int command(Command &&cmnd)
int command(Command &cmnd)
is a partial wrapper of an object with same as wrapped object Interface
virtual unique_ptr< Interface > factory_method()=0
virtual ValueType get() const =0
is a sample of getter abstract interface for Synchronised_encapsulated_value
virtual ~Getter_interface()=default
is a virtual command handler of Chain_of_responsibility
virtual int handle(Command &cmnd)
Specific handler can process a command and return non-negative.
virtual ~Handler()=default
is a common pure virtual interface
virtual ~Interface()=default
is part of MVC with View and Controller
void register_observer(Observer &o)
int command(Command &cmnd)
int command(Command &&cmnd)
is virtual observer of a Subject
virtual ~Observer()=default
virtual void notify()
without arguments
virtual void update(Subject &subject)
with the only Subject argument
is the factory of himself
unique_ptr< Interface > create() override
is a opaque wrapper with same as wrapped object Interface
map< string, forward_list< reference_wrapper< Subscriber > > > topic_subscribers
void publish(const string &topic, Message &m)
one of many components is independed from Sample_visitor and implemenation of function visit.
string component_accept(Visitor &visitor) const override
string component_method() const
is not virtual
unique_ptr< Interface > factory_method() override
virtual unique_ptr< Interface > create()
virtual ~Setter_interface()=default
virtual void set(ValueType i)=0
is a sample of setter abstract interface for Synchronised_encapsulated_value
SINGLETON(Singleton_demo)
is wrapped by Bridge. AKA adaptee of Adapter
float standalone_method() const
forward_list< reference_wrapper< Observer > > observers
void notify() override
without arguments
is a pure virtual visitor of Sample_component and other specific components
virtual ~Visitor()=default
virtual string visit(const Sample_component &) const =0
overloaded function for each component