Lesson 1
Creational Design Patterns
This module explores
creational patterns, which represent patterns that are often used in place of direct instantiation with constructors.
Creational patterns make the creation process more adaptable and dynamic. In particular, they can provide a great deal of flexibility about which objects are created, how those objects are created, and how they are initialized. In this module, you will learn:
- How creational patterns help programmers
- Five well-known creational patterns
- The elements of the Factory Method pattern
- How to incorporate the Factory Method pattern into your course project
How do creational design patterns deal with object creation mechanisms?
In the realm of software architecture, creational design patterns are akin to the master builders of object creation. They are expert architects, using their wisdom and experience to devise mechanisms for the construction of objects, dealing with the complex nature of object creation and initialization in the most efficient and versatile ways possible.
Creational patterns deal with object creation mechanisms in a way that separates the system's specifics of how objects are created, composed, and represented from its general architecture. By doing so, they bring flexibility and reusability to a system's architecture, making it adaptable to evolving requirements and situations. Here are some of the creational design patterns and how they handle object creation:
- Singleton Pattern: Imagine a kingdom where only one king rules. The Singleton pattern does precisely that. It restricts an application from creating more than one instance of a class, ensuring that just one object of a particular class exists in the system. Singleton handles object creation by first checking if an instance of the class already exists. If it does, it returns a reference to that instance. If not, it creates one.
- Factory Method Pattern: The Factory Method pattern works like a skilled craftsman who creates objects. Instead of calling a constructor directly to create an object, an application calls a factory method. The beauty of this pattern lies in its ability to determine the class of the object to be created at runtime, based on the parameters passed to the factory method.
- Abstract Factory Pattern: This pattern acts as a super-factory or a factory of factories, creating other factories. It provides an interface for creating families of related or dependent objects without specifying their concrete classes, thus increasing abstraction and reducing dependency.
- Builder Pattern: Just as a skilled builder meticulously constructs a complex structure piece by piece, the Builder pattern allows a step-by-step creation of complex objects. This pattern separates the construction of an object from its representation, giving the same construction process the ability to create different types and representations of an object.
- Prototype Pattern: The Prototype pattern, much like a sculptor who creates copies of a masterpiece, permits the creation of objects by cloning an existing object, the prototype. This is particularly useful when creating a new instance is heavier or more complex than copying an existing one.
By utilizing these patterns, developers can effectively control and manage the object creation process, promoting code scalability and robustness, and making the code easier to maintain and modify. Creational design patterns, therefore, serve as the robust pillars in the foundation of object-oriented software architecture, helping to construct efficient, versatile, and robust systems.
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created,composed, and represented. A class creational pattern uses inheritance to vary the class that's instantiated, whereas an
object creational pattern will delegate instantiation to another object. Creational patterns become important as systems evolve to depend more on object composition than class inheritance. As that happens, emphasis shifts away from hard-coding a fixed set of behaviors toward defining a smaller set of fundamental behaviors that can be composed into any number of more complex ones. Thus creating objects with particular behaviors requires more than simply instantiating a class. There are two recurring themes in these patterns.
- First, they all encapsulate knowledge about which concrete classes the system uses.
- Second, they hide how instances of these classes are created and put together.
The only thing the system at large knows about are the objects and their interfaces as defined by abstract classes.
Consequently, the creational patterns give you a lot of flexibility in what gets created, who creates it, how it gets created, and when. They let you configure a system with "product" objects that vary widely in structure and functionality. Configuration can be static (that is, specified at compile-time) or dynamically. Sometimes creational patterns are competitors. For example, there are cases when either Prototype or Abstract Factory could be used to your advantage. At other times they are complementary: Builder can use one of the other patterns to implement which components get built.
Ad Microservices Patterns