Describe, compare, and contrast the following four creational patterns:
1) Factory Method pattern, 2) Abstract Factory, 3) Builder, 4) Prototype. 5) Singleton
- Factory Method Pattern: The Factory Method pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. The Factory Method is used when there is uncertainty about the exact types and dependencies of the objects your code should work with. Instead of calling a constructor to create an object directly, the creation is delegated to a factory method, providing a layer of flexibility and scalability to your code.
- Abstract Factory Pattern: The Abstract Factory Pattern, another creational design pattern, extends the concept of the Factory Method pattern. It involves an interface to create families of related or dependent objects without specifying their concrete classes. Think of it as a factory of factories. The idea is to abstract the creation of objects depending on business logic, platform choice, or other considerations. Abstract Factory allows high-level abstraction and loose coupling, thereby promoting code reusability and separation of responsibilities.
- Builder Pattern: The Builder pattern is employed when there is a need to construct complex objects step by step. It separates the construction of an object from its representation so that the same construction process can create different types of objects. It encapsulates the construction logic in different builder classes, each defining how to combine different parts of the product, while the director class guides the builder class on the order of the execution. The Builder pattern is excellent when the product involves a lot of steps or components, providing flexibility in the composition and representation of the product.
- Prototype Pattern: The Prototype pattern is used when creating a new object instance is more complex or costly than copying an existing instance. The original object acts as a prototype and is cloned when a new instance is needed. This pattern hides the complexities of making new instances from the client. The copying process can be customized to handle the copying of complex object structures or cross-referencing of other objects within the clone.
- Singleton Pattern: The Singleton pattern restricts instantiation of a class and ensures that only one instance of the class exists in the system. It provides a global point of access to the instance, ensuring that no other part of the code can instantiate its own copy of the object. Singleton is used when a single shared value of a particular class needs to be used by other objects.
While all these patterns deal with object creation, they serve different purposes.
- Factory Method and Abstract Factory patterns both deal with object creation but at different levels of complexity: Factory Method is about creating an instance of several derived classes, while Abstract Factory is about creating families of related or dependent objects.
- The Builder pattern, on the other hand, isn't about choosing the class of the product but about constructing complex objects step by step. Unlike the other patterns, the Prototype pattern leverages prototypical instances and creates new objects by copying these prototypes.
- Finally, the Singleton pattern is unique in its purpose: it's designed to ensure there's only one instance of a class in the system and provides a global point of access to it.
Each pattern offers unique advantages based on the situation at hand, but all serve the same general purpose: to make the code more flexible, maintainable, and organized by abstracting the process of object creation.
There is no particular limit to the number of creational patterns, any more than there is a finite number of possible blueprints for a house,
there are five which are particularly well-known.
- Factory Method Pattern
- Abstract Factory
We have already looked at the Singleton pattern.
Later in this module, you will have a chance to explore the Factory Method pattern
The other three common creational patterns are described in this SlideShow:
Creational: This deals with the concept of how an object can be created.
This often involves isolating the details of object creation so your code is not dependent on what types of objects there are and thus does not have to be changed when you add a new type of object. The aforementioned Singleton is classified as a creational pattern, and later in this course you will see examples of the Factory Method and Prototype.