Creational Design Patterns intent is to create object at runtime. Learn different aspects of creational design patterns like intent, focus usability etc.
1. Overview of Creational Design Patterns
We use the creation of an object via a design pattern is known as a creational pattern. Here, creational means there is a different way of creating an object with different intent to solve the contextual problem. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve the contextual problem of the creation of an object by somehow controlling this object creation.
Creational Design Patterns is one of the types of GoF (Gang of Four) design patterns.
- ued to create objects for a suitable class that serves as a solution for a problem.
- particularly useful when you are taking advantage of polymorphism, choose between different classes at runtime rather than compile time.
- Allow objects to be created in a system without having to identify a specific class type in the code
1. key design patterns under creational
In this section you can learn key creational design patterns from GoF patterns.
- Factory Method: creates an instance of product via several derived classes.
- Abstract Factory: creates instance of product which belongs to families of classes or group of class.
- Singleton: ensure existence of a single instance of a class per JVM.
- Builder: Separates object construction from its representation.
- Prototype: A fully initialized instance to be copied or cloned.
2. Key Consideration Points
Following points can be considered while designing of a component in software engineering or reverse software engineering.
- The intent of Factory is to allow a class to defer instantiation to its sub-classes.
- The intent of Abstract Factory is to create families of related object without having to depends on their concrete class.
- Factory method handles object creation and encapsulates it in a sub-class.
- Abstract Factory, Builder, and Prototype define a factory object that’s responsible for knowing and creating the class of product objects.
- Abstract Factory has the factory object producing objects of several classes.
- Builder has the factory object building a complex product incrementally using a correspondingly complex protocol.
- Prototype has the prototype/factory object which build a product by copying/cloning a prototype object.
- Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype depending upon how much heavy is the instantiation process and how frequently instantiating abstract factory.
- Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
- Builder focuses on constructing a complex object step by step while Abstract Factory emphasizes a family of product objects
- Builder returns the product in final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
- Builder often builds a Composite. Builder is to creation as Strategy is to algorithm.
- Factory Methods are usually called within Template methods , if step of algorithm is complex.
- Factory Method creation through inheritance while Prototype creation through delegation.
- Prototype doesn’t require sub-classing, but it does require an Initialize operation while Factory Method requires sub-classing, but doesn’t require Initialize.
- Designs that make heavy use of the Composite and Decorator patterns, often can benefit from Prototype as well.
- There are different way of implementation of Singleton: lazy initialization, eager initialization, dual checking, thread safe , and using enum.
- Dependency Inversion Principle: Depends upon abstraction, Do not depend upon concrete classes. In another word, low level component depends on high level abstraction.
- No variable should hold a reference to concrete class it means, if you are using new Clazz() , use factory to get object .
- S.O.L.I.D Design Principle
I hope you enjoyed this post of behavioral design patterns, and you can visit design patterns tutorial for more details
Please write your comment in the comment section for further improvement. Happy Learning 🙂