C++ features by examples

Creational patterns More...

Collaboration diagram for Creational:

Classes

struct  Singleton_demo
 
struct  Factory_method_demo
 
struct  Sample_product
 
struct  Sample_factory_method_demo
 
struct  Abstract_factory
 
struct  Sample_factory
 
struct  Prototype
 is the factory of himself More...
 
struct  Builder
 

Macros

#define SINGLETON(Singleton)
 

Functions

void creational_patterns_demo ()
 

Detailed Description

Creational patterns

https://refactoring.guru/design-patterns/creational-patterns

Macro Definition Documentation

◆ SINGLETON

#define SINGLETON (   Singleton)
Value:
public: \
/* Meyers Singleton realization */ \
static Singleton& instance() { \
static Singleton me; \
return me; \
} \
Singleton(const Singleton&) = delete; \
Singleton& operator=(const Singleton&) = delete; \
Singleton(Singleton&&) = delete; \
Singleton& operator=(Singleton&&) = delete; \
private: \
Singleton()

The singleton will be automatically safely instantiated on the first call.

Define constructor body after the define

Definition at line 186 of file patterns.cpp.

Function Documentation

◆ creational_patterns_demo()

void creational_patterns_demo ( )

Definition at line 272 of file patterns.cpp.

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}
Builder & add(int i)
Definition: patterns.cpp:259
is a common pure virtual interface
Definition: patterns.cpp:167
virtual int method()=0
is the factory of himself
Definition: patterns.cpp:247
unique_ptr< Interface > create() override
Definition: patterns.cpp:250
Here is the call graph for this function:
Here is the caller graph for this function: