Skip to content

Commit 937de4f

Browse files
authored
Merge pull request #24 from urboob21/dev_branch
id 1764048339
2 parents 20dfc1b + 05030a5 commit 937de4f

7 files changed

Lines changed: 410 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ set(APP_SOURCES
109109
"src/patterns/behavioral/State.cpp"
110110
"src/patterns/behavioral/Observer.cpp"
111111
"src/patterns/creational/Singleton.cpp"
112+
"src/patterns/creational/FactoryMethod.cpp"
112113
)
113114

114115
# Test files

docs/uml/patterns_creational_factorymethod.drawio.svg

Lines changed: 4 additions & 0 deletions
Loading

src/patterns/behavioral/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Behavioral Design Patterns
2+
3+
Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
4+
5+
---
6+
7+
## Chain of Responsibility
8+
9+
**Chain of Responsibility**
10+
Lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.
11+
12+
**Real-time example:**
13+
Customer support tickets in a company. Simple requests go to the first-level support, more complex issues go to specialists, and only very complex issues reach managers.
14+
15+
---
16+
17+
## Command
18+
19+
**Command**
20+
Turns a request into a stand-alone object that contains all information about the request. This lets you pass requests as arguments, delay or queue execution, and support undo operations.
21+
22+
**Real-time example:**
23+
Using a remote control for smart home devices. Each button press represents a command: turn on lights, open curtains, or play music. You can even undo the last command.
24+
25+
---
26+
27+
## Iterator
28+
29+
**Iterator**
30+
Lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
31+
32+
**Real-time example:**
33+
Browsing a photo gallery app. You can swipe left or right to view photos without knowing how the photos are stored internally.
34+
35+
---
36+
37+
## Mediator
38+
39+
**Mediator**
40+
Reduces chaotic dependencies between objects by forcing them to communicate through a mediator.
41+
42+
**Real-time example:**
43+
An air traffic control tower. Planes don’t communicate directly with each other; the tower coordinates landings and takeoffs.
44+
45+
---
46+
47+
## Memento
48+
49+
**Memento**
50+
Lets you save and restore the previous state of an object without revealing its implementation details.
51+
52+
**Real-time example:**
53+
The “Undo” feature in a text editor. You can revert to a previous version of your document without knowing the details of how the editor stores text internally.
54+
55+
---
56+
57+
## Observer
58+
59+
**Observer**
60+
Defines a subscription mechanism to notify multiple objects about events happening to the object they’re observing.
61+
62+
**Real-time example:**
63+
Social media notifications. When someone posts a new photo, all their followers are notified immediately.
64+
65+
---
66+
67+
## State
68+
69+
**State**
70+
Lets an object alter its behavior when its internal state changes, making it appear as if the object changed its class.
71+
72+
**Real-time example:**
73+
A traffic light. Its behavior (red, yellow, green) changes automatically depending on its current state.
74+
75+
---
76+
77+
## Strategy
78+
79+
**Strategy**
80+
Defines a family of algorithms, puts each into a separate class, and makes them interchangeable.
81+
82+
**Real-time example:**
83+
A navigation app lets you choose between driving, walking, or cycling routes. Each strategy calculates a different route but uses the same interface.
84+
85+
---
86+
87+
## Template Method
88+
89+
**Template Method**
90+
Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps without changing its structure.
91+
92+
**Real-time example:**
93+
Making coffee or tea in a café. The steps (boil water, pour, serve) are the same, but each drink has slightly different preparation steps.
94+
95+
---
96+
97+
## Visitor
98+
99+
**Visitor**
100+
Lets you separate algorithms from the objects on which they operate.
101+
102+
**Real-time example:**
103+
A tax calculator that processes different types of items (books, electronics, groceries) without changing the item classes. The calculator “visits” each item to compute tax.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// cppcheck-suppress-file [functionStatic]
2+
3+
// Factory Method is a creational design pattern that provides an interface for creating objects in a superclass,
4+
// but allows subclasses to alter the type of objects that will be created.
5+
// Appicability:
6+
// (*) when you don’t know beforehand the exact types and dependencies of the objects your code should work with.
7+
// (**) when you want to provide users of your library or framework with a way to extend its internal components.
8+
// (***)when you want to save system resources by reusing existing objects instead of rebuilding them each time.
9+
10+
// UML: docs/uml/patterns_creational_factorymethod.drawio.svg
11+
12+
#include <iostream>
13+
#include <string>
14+
namespace
15+
{
16+
namespace FactoryMethod
17+
{
18+
/**
19+
* The Product interface declares the operations that all concrete products must
20+
* implement.
21+
*/
22+
class IGdbProduct
23+
{
24+
public:
25+
virtual ~IGdbProduct() = default;
26+
virtual void launch() const = 0;
27+
};
28+
29+
/**
30+
* Concrete Products provide various implementations of the Product interface.
31+
*/
32+
class LinuxGdbProduct : public IGdbProduct
33+
{
34+
public:
35+
void launch() const override
36+
{
37+
std::cout
38+
<< "\tsudo apt update && sudo apt install -y gdb && gdb --version\n";
39+
}
40+
};
41+
42+
class WindowsGdbProduct : public IGdbProduct
43+
{
44+
public:
45+
void launch() const override
46+
{
47+
std::cout << "\tpacman -Syu mingw-w64-x86_64-gdb && gdb --version\n";
48+
}
49+
};
50+
51+
class MacOsGdbProduct : public IGdbProduct
52+
{
53+
public:
54+
void launch() const override
55+
{
56+
std::cout << "\tbrew install gdb && gdb --version\n";
57+
}
58+
};
59+
60+
/**
61+
* The Creator class declares the factory method that is supposed to return an
62+
* object of a Product class. The Creator's subclasses usually provide the
63+
* implementation of this method.
64+
*/
65+
class IGdbCreator
66+
{
67+
public:
68+
virtual ~IGdbCreator() = default;
69+
virtual IGdbProduct *factoryMethod() = 0;
70+
virtual void launchGdb() = 0;
71+
};
72+
73+
class AbstractGdbCreater : public IGdbCreator
74+
{
75+
public:
76+
// Call the factory method to create a Product object.
77+
78+
void launchGdb() override final
79+
{
80+
IGdbProduct *gdb = this->factoryMethod();
81+
gdb->launch();
82+
delete gdb;
83+
}
84+
};
85+
86+
/**
87+
* Concrete Creators override the factory method in order to change the
88+
* resulting product's type.
89+
*/
90+
class WindowsGdbCreator : public AbstractGdbCreater
91+
{
92+
public:
93+
IGdbProduct *factoryMethod() override
94+
{
95+
return new WindowsGdbProduct();
96+
}
97+
};
98+
99+
class LinuxGdbCreator : public AbstractGdbCreater
100+
{
101+
public:
102+
IGdbProduct *factoryMethod() override
103+
{
104+
return new LinuxGdbProduct();
105+
}
106+
};
107+
108+
class MacOsGdbCreator : public AbstractGdbCreater
109+
{
110+
public:
111+
IGdbProduct *factoryMethod() override
112+
{
113+
return new MacOsGdbProduct();
114+
}
115+
};
116+
117+
/**
118+
* The client code works with an instance of a concrete creator, albeit through its base interface.
119+
* As long as the client keeps working with the creator via the base interface, you can pass it any creator's subclass.
120+
*/
121+
namespace ClientCode
122+
{
123+
void clientCode(IGdbCreator *gdb)
124+
{
125+
if (gdb != nullptr)
126+
gdb->launchGdb();
127+
}
128+
}
129+
130+
class GdbCreatorFactory
131+
{
132+
public:
133+
static IGdbCreator *createGdbCreator(const std::string &os)
134+
{
135+
if (os == "linux")
136+
{
137+
return new LinuxGdbCreator();
138+
}
139+
else if (os == "windows")
140+
{
141+
return new WindowsGdbCreator();
142+
}
143+
else if (os == "macos")
144+
{
145+
return new MacOsGdbCreator();
146+
}
147+
else
148+
{
149+
std::cout << "OS not support yet - " << os << "\n";
150+
return nullptr;
151+
}
152+
}
153+
};
154+
155+
void run()
156+
{
157+
std::string os = "linux";
158+
IGdbCreator *gdb = GdbCreatorFactory::createGdbCreator(os);
159+
ClientCode::clientCode(gdb);
160+
delete gdb;
161+
}
162+
}
163+
}
164+
165+
struct FactoryMethodAutoRunner
166+
{
167+
FactoryMethodAutoRunner()
168+
{
169+
std::cout << "\n--- FactoryMethod Pattern Example ---\n";
170+
FactoryMethod::run();
171+
}
172+
};
173+
174+
static FactoryMethodAutoRunner instance;

src/patterns/creational/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Creational Design Patterns
2+
3+
Creational design patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
4+
5+
---
6+
7+
## Factory Method
8+
9+
**Factory Method**
10+
Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
11+
12+
**Real-time example:**
13+
A ride-hailing app can create different types of rides: economy, premium, or SUV. The app defines a general interface for booking a ride, and the specific ride type is decided at runtime.
14+
15+
---
16+
17+
## Abstract Factory
18+
19+
**Abstract Factory**
20+
Lets you produce families of related objects without specifying their concrete classes.
21+
22+
**Real-time example:**
23+
A furniture store app that sells sets of furniture. If a customer chooses a "Modern" style, the app provides a matching chair, table, and sofa. If "Victorian" style is chosen, it provides a different matching set. The app doesn’t need to know the concrete classes of each furniture piece.
24+
25+
---
26+
27+
## Builder
28+
29+
**Builder**
30+
Lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
31+
32+
**Real-time example:**
33+
Ordering a custom pizza online. You can choose the dough, sauce, toppings, and size step by step, resulting in different pizzas, all using the same ordering process.
34+
35+
---
36+
37+
## Prototype
38+
39+
**Prototype**
40+
Lets you copy existing objects without making your code dependent on their classes.
41+
42+
**Real-time example:**
43+
Copying a template for a new document in Google Docs. Instead of creating a document from scratch, you duplicate an existing one and modify it.
44+
45+
---
46+
47+
## Singleton
48+
49+
**Singleton**
50+
Lets you ensure that a class has only one instance, while providing a global access point to this instance.
51+
52+
**Real-time example:**
53+
A system configuration manager in an app. All parts of the app access the same configuration instance, so settings are consistent across the application.

src/patterns/creational/Singleton.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// (*) when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
77
// (**) when you need stricter control over global variables.
88

9+
// UML: docs/uml/patterns_creational_singleton.drawio.svg
10+
911
#include <iostream>
1012

1113
namespace

0 commit comments

Comments
 (0)