1. Abstract Factory
- Produce families of related objects without specifying their concrete classes.
- Client works with factories and products using abstract interfaces. It makes the same client code working with many product variants, depending on the type of factory object.
- You can extract the product creation code into one place, making the code easier to support.
- You can introduce new variants of products without breaking existing client code.
- The code may become more complicated because a lot of new interfaces and classes are introduced.
- Abstract Factory defines an interface for creating buttons and checkboxes. There are two concrete factories, which return both products in a single variant.
- In this example, buttons and checkboxes will act as products. They have two variants: macOS and Windows.
- Construct various representations of the product involves similar steps that differ only in the details.
- The base Builder interface defines all possible construction steps, and concrete builders implement these steps to construct particular representations of the product.
- Meanwhile, the Director class defines the order of construction.
- You can swap or defer execution of some steps without breaking the final product.
- Builder doesn’t expose the final product until construction is complete.
- You gain full control of construction — how to implement & order of steps to build products.
- The overall complexity of the code increases since the pattern requires creating multiple new classes.
- Keyword — clone, copy.
- Delegates the cloning process to the actual objects that are being cloned. The pattern declares a common interface for all objects that support cloning.
- Instead of instantiating a subclass that matches some configuration, The Prototype pattern lets you simply look for an appropriate prototype and clone it.
- Cloning objects that have circular references might be very challenging.
- Keyword — object pool, cache.
- Ensure a class has only one instance, and provide a global point of access.
- Unlike global variables, Singleton make sure that there’s just one instance of a class. Only the Singleton itself can replace the cached instance.
- Singleton limit resource to instantiate object and limit how to access.
- Singleton requires more concern in a multi-threaded environment so that Singleton objects won’t be created several times in each thread.
Implementation of Singleton have these two steps in common:
- Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
- Create a static creation method that acts as a constructor. This method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object.
Design Patterns and Refactoring
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms…