Skip to content

Commit 2037a8c

Browse files
committed
complete: creational patterns
1 parent 3cd556e commit 2037a8c

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Creational Patterns/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Creational Design Patterns 🏗️
2+
3+
<p align="center">
4+
<img src="https://f.hubspotusercontent40.net/hubfs/5621549/imagenes-blog_creational.png" alt="Creational Design Patterns Illustration" width="600">
5+
</p>
6+
7+
Creational design patterns focus on efficient and controlled object creation. They provide flexible ways to create objects while abstracting the instantiation process. Imagine different ways of building houses, each catering to unique requirements.
8+
9+
## 🔧 Types of Creational Patterns
10+
11+
1. **Singleton:** Ensures only one instance of a class exists throughout the application. Useful for scenarios where a single point of control is needed.
12+
13+
2. **Factory Method:** Defines an interface for creating objects but lets subclasses decide which class to instantiate. Useful for creating objects without specifying the exact class.
14+
15+
3. **Abstract Factory:** Provides an interface for creating families of related or dependent objects. Useful for ensuring compatibility among objects.
16+
17+
4. **Builder:** Separates the construction of a complex object from its representation, allowing step-by-step creation. Useful for creating complex objects with various configurations.
18+
19+
5. **Prototype:** Allows copying existing objects to create new instances. Useful for efficiently creating objects with similar properties.
20+
21+
## 🚀 Why Use Creational Patterns?
22+
23+
**Efficient Object Creation:** Creational patterns offer more controlled and optimized ways to create objects, reducing redundant code and improving performance.
24+
25+
**Flexibility:** Creational patterns enable dynamic object creation with varying configurations, making your codebase adaptable to changing requirements.
26+
27+
**Abstraction:** These patterns abstract the instantiation process, making your code more maintainable and loosely coupled.
28+
29+
## 💡 When to Use Each Creational Pattern
30+
31+
**Singleton:**
32+
- Use when you need a single point of control for an instance.
33+
- Examples: Database connections, Logger instances.
34+
35+
**Factory Method:**
36+
- Use when subclasses need flexibility in creating objects.
37+
- Examples: Document processing with different formats, UI element creation.
38+
39+
**Abstract Factory:**
40+
- Use when you want to ensure object compatibility within families.
41+
- Examples: GUI toolkits with different themes, Database access with different implementations.
42+
43+
**Builder:**
44+
- Use when creating complex objects step by step with varied configurations.
45+
- Examples: Building houses with custom features, Creating complex meal orders.
46+
47+
**Prototype:**
48+
- Use when creating similar objects with customized properties.
49+
- Examples: Graphic design app with shape variations, Game with character customization.
50+
51+
## 🎮 How Each Creational Pattern Works
52+
53+
**Singleton:**
54+
- Restricts constructor access to control instantiation.
55+
- Provides a static method to access the single instance.
56+
- Guarantees only one instance exists throughout the application's lifecycle.
57+
58+
**Factory Method:**
59+
- Declares an interface for creating objects.
60+
- Subclasses implement the factory method to create instances of specific classes.
61+
- Allows adding new product classes without modifying existing code.
62+
63+
**Abstract Factory:**
64+
- Declares interfaces for creating families of related objects.
65+
- Concrete factory classes implement these interfaces to create product objects.
66+
- Ensures that products created by one factory are compatible with products created by another.
67+
68+
**Builder:**
69+
- Separates complex object construction from its representation.
70+
- A director class guides the building process using a builder interface.
71+
- Builders implement the interface to construct parts and provide a method to retrieve the complete object.
72+
73+
**Prototype:**
74+
- Defines a clone method to copy existing objects.
75+
- Concrete prototype classes implement this method to create copies.
76+
- Allows efficient object creation with similar properties by copying prototypes.
77+
78+
## 🧠 Trick to Remember Creational Patterns
79+
80+
Remember the funny mnemonic: **S**neaky **F**oxes **A**te **B**anana **P**ancakes to recall the order of Singleton, Factory Method, Abstract Factory, Builder, and Prototype design patterns!
81+
82+
## 🌟 Pros of Creational Patterns
83+
84+
🛠️ **Customization:** Patterns offer ways to customize object creation according to specific requirements.
85+
86+
💡 **Optimization:** Creational patterns optimize object creation and reduce resource usage.
87+
88+
🔧 **Abstraction:** Patterns abstract the creation process, enhancing code readability and maintainability.
89+
90+
## 🛑 Cons of Creational Patterns
91+
92+
🔌 **Complexity:** Some patterns introduce additional classes, potentially increasing code complexity.
93+
94+
🗂️ **Overhead:** In certain cases, patterns might add overhead compared to straightforward object creation.
95+
96+
## 🎮 Real-World Analogies
97+
98+
Consider a carpenter crafting furniture. Creational patterns represent different woodworking techniques to efficiently create chairs, tables, and shelves.
99+
100+
## 💼 Real-World Example
101+
102+
Imagine an e-commerce platform managing product creation. Creational patterns help create different product types like electronics, clothing, and books.
103+
104+
## 🤔 Interview Tips
105+
106+
1. **Understand Purpose:** Know when and why to use each creational pattern based on its strengths.
107+
108+
2. **Working Mechanisms:** Grasp how each pattern works, including its participants and flow.
109+
110+
3. **Remember SFABP:** Recall the five creational patterns using the mnemonic and associate each letter with its pattern.
111+
112+
## 🤓 Interview Questions
113+
114+
1. **Why use creational patterns?**
115+
Creational patterns offer efficient, flexible, and abstracted ways to create objects.
116+
117+
2. **How does the Singleton pattern ensure only one instance exists?**
118+
It provides a single point of access to an instance and restricts the constructor.
119+
120+
3. **What's the main difference between Factory Method and Abstract Factory?**
121+
Factory Method creates objects in subclasses, while Abstract Factory creates families of related objects.
122+
123+
4. **When would you use the Builder pattern?**
124+
The Builder pattern is useful for creating complex objects with various configurations.
125+
126+
5. **What problem does the Prototype pattern solve?**
127+
It efficiently creates objects with similar properties by copying existing instances.
128+
129+
## 🚀 Conclusion
130+
131+
Creational design patterns provide structured and efficient ways to create objects in your application. Each pattern addresses specific needs, from managing singleton instances to creating complex objects step by step. By leveraging these patterns, you enhance code flexibility, reusability, and maintainability, leading to a more robust and adaptable software architecture.

0 commit comments

Comments
 (0)