CHAPTER 05
Intermediate
Creational Design Patterns Overview
Updated: May 16, 2026
20 min read
# CHAPTER 5
Creational Design Patterns Overview
1. Introduction
In standard programming, when you need an object, you simply use thenew keyword (e.g., new DatabaseConnection()). However, hardcoding the new keyword scatters concrete dependencies throughout your entire application. If the initialization logic for that database connection changes, or if you suddenly need to switch to a different database, you have to hunt down and modify hundreds of new statements across your codebase. This is inflexible and fragile. To solve this, the Gang of Four defined Creational Design Patterns. In this chapter, we will provide a high-level overview of the Creational category. We will explore the systemic problems of hardcoded object creation and understand how these patterns abstract the instantiation process, making systems independent of how their objects are created, composed, and represented.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define the purpose and scope of Creational Design Patterns.
-
Understand the architectural dangers of hardcoding the
newkeyword.
- Differentiate between class-creation patterns and object-creation patterns.
- Identify the five primary Creational Patterns defined by the Gang of Four.
- Understand the concept of "Dependency Management" through object creation.
3. The Problem with the "new" Keyword
Thenew keyword is the glue that tightly couples your code.
-
The Anti-Pattern: Imagine a
ReportGeneratorclass that says$db = new MySQLDatabase(). TheReportGeneratoris now permanently glued to MySQL. It cannot be tested independently, and it cannot be reused with PostgreSQL.
-
The Creation Logic: Objects often require complex setup. If a
Carobject requires you to first instantiate anEngine, fourTires, and aChassis, forcing the client code to write 10 lines of setup every time they want aCarduplicates logic and causes errors.
-
The Creational Goal: Hide the complexity of creation. The client code should just ask for a
Car, and the Creational Pattern handles the messy assembly behind the scenes.
4. The Gang of Four Categories Overview
The 23 classic GoF design patterns are divided into three main categories based on their purpose:- 1. Creational Patterns: Deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. (Our current focus).
- 2. Structural Patterns: Deal with object composition, identifying simple ways to realize relationships between different entities (e.g., Adapter, Facade).
- 3. Behavioral Patterns: Deal with communication and the assignment of responsibilities between objects (e.g., Observer, Strategy).
5. The Five Creational Patterns
Creational patterns abstract the instantiation process. Here is the roster:- 1. Singleton: Ensures a class only has one instance, and provides a global point of access to it. (e.g., A shared configuration manager).
-
2.
Factory Method: Defines an interface for creating an object, but lets subclasses decide which class to instantiate. (e.g., A Logistics app deciding between creating a
Truckor aShip).
- 3. Abstract Factory: Provides an interface for creating *families* of related or dependent objects without specifying their concrete classes. (e.g., Creating Mac UI buttons vs. Windows UI buttons).
- 4. Builder: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations. (e.g., Building a complex SQL query step-by-step).
- 5. Prototype: Specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype. (e.g., Cloning an enemy character in a video game to save memory).
6. Dependency Management
Creational patterns are heavily intertwined with the SOLID Dependency Inversion Principle (DIP). By moving thenew keyword out of your business logic and into dedicated Factory or Builder classes, your high-level modules only depend on abstract Interfaces. The Factory handles the messy concrete details, injecting the finished objects into your clean architecture.
7. Diagrams/Visual Suggestions
*Pattern Comparison: Creational Intent*
text
8. Best Practices
-
Hide the Constructor: When implementing patterns like the Factory or Singleton, a standard practice is to make the actual class constructor
privateorprotected. This physically forces other developers to use your Factory method to get the object, preventing them from accidentally using thenewkeyword and bypassing your architectural rules.
9. Common Mistakes
-
Over-Engineering Creation: A developer writes a massive
StringFactoryjust to create basic string variables. *The Failure:* This is absurd over-engineering. Creational patterns should only be used when the object creation process is complex, involves heavy configuration, or requires dynamic polymorphism. For simple Data Transfer Objects (DTOs), thenewkeyword is perfectly fine.
10. Mini Project: Identify the Pattern
Look at these scenarios and identify which creational pattern is needed.- 1. *Scenario:* We need a single, shared connection to the Redis cache that every file in our app can access without opening a new connection. -> Singleton
- 2. *Scenario:* We are building a maze game. We need a way to construct complex rooms with varying amounts of doors, monsters, and treasure chests step-by-step. -> Builder
-
3.
*Scenario:* A user clicks "Export". Based on their settings, we either need to generate a
PDFDocumentor anExcelDocument. -> Factory Method
11. Practice Exercises
-
1.
Explain why relying heavily on the
newkeyword throughout a codebase creates tightly coupled architecture.
- 2. List the three main categories of the Gang of Four (GoF) design patterns, and define the primary focus of the Creational category.
12. MCQs with Answers
Question 1
Which of the following is NOT a Creational Design Pattern?
Question 2
What is the primary architectural goal of utilizing Creational Design Patterns?
13. Interview Questions
- Q: Explain the concept of tightly coupled code in relation to object instantiation. How do Creational patterns resolve this coupling?
- Q: Walk me through the high-level differences between a Factory Method, a Builder, and a Prototype pattern. What specific object creation problem does each solve?
-
Q: Why might an architect choose to make a class constructor
private? How does an external client instantiate the class if the constructor is locked?
14. FAQs
Q: Should I memorize all 23 GoF patterns? A: No. In modern software engineering, some patterns (like Singleton, Factory, Strategy, and Observer) are used constantly, while others (like Flyweight or Memento) are extremely rare. Focus on understanding the core problems the popular patterns solve.15. Summary
In Chapter 5, we explored the philosophy of object genesis. We identified that the innocentnew keyword is actually the root cause of tightly coupled, rigid architectures. We surveyed the Gang of Four's Creational category, establishing a toolkit designed to hide the messy complexity of object construction. Whether we need a single shared instance, a dynamic subclass generator, or a step-by-step assembly line, creational patterns ensure our business logic remains clean, abstract, and oblivious to the messy details of instantiation.