You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
Copy file name to clipboardExpand all lines: src/patterns/creational/Singleton.cpp
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@
6
6
// (*) 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.
7
7
// (**) when you need stricter control over global variables.
0 commit comments