Creational Design Patterns are ones that create objects, rather than having to directly instantiate objects. It gives the program more flexibility in deciding which objects need to be created for a given case.
Creational Design Patterns are divided into following categories.
- Abstract Factory: Allows the creation of objects without specifying their concrete type.
- Builder: Uses to create complex objects.
- Factory Method: Creates objects without specifying the exact class to create.
- Prototype: Creates a new object from an existing object.
- Singleton: Ensures only one instance of an object is created.
Abstract Factory Pattern
Abstract Factory pattern is almost similar to Factory Pattern is considered as another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories.
Abstract factory pattern implementation provides us a framework that allows us to create objects that follow a general pattern. So at runtime, the abstract factory is coupled with any desired concrete factory which can create objects of the desired type.
What are the advantages of using the Advantages of Abstract Factory?
This pattern is particularly useful when the client doesn’t know exactly what type to create.
- Isolation of concrete classes
- Exchanging Product Families easily
- Promoting consistency among products
Disadvantages of Abstract Factory
- Difficult to support new kind of products
Builder Pattern
Builder is a Creational Design Patterns that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
This pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders.
The Builder pattern lets you build objects step by step, using only those steps that you really need. After implementing the pattern, you don’t have to cram dozens of parameters into your constructors anymore.
What are the advantages of using the Advantages of Builder Pattern?
- Single Responsibility Principle. You can isolate complex construction code from the business logic of the product.
- You can reuse the same construction code when building various representations of products.
- The overall complexity of the code increases since the pattern requires creating multiple new classes.
Disadvantages of Builder Pattern
- You can construct objects step-by-step, defer construction steps or run steps recursively.
Factory Method Design Pattern
Factory Method is responsible to creates objects without specifying the exact class to create.
The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special factory method. Don’t worry: the objects are still created via the new operator, but it’s being called from within the factory method. Objects returned by a factory method are often referred to as products.
Where is factory design pattern used?
Factory classes are useful
- When you need a complicated process for constructing the object.
- When the construction needs a dependency that you do not want for the actual class.
- When you need to construct different objects etc.
- Factory methods should be considered as an alternative to constructors – mostly when constructors aren’t expressive enough.
What are the advantages of using the Advantage of Factory Method
- You avoid tight coupling between the creator and the concrete products.
- Single Responsibility Principle. You can move the product creation code into one place in the program, making the code easier to support.
- Open/Closed Principle. You can introduce new types of products into the program without breaking existing client code.
Disadvantage of Factory Method
- The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern. The best case scenario is when you’re introducing the pattern into an existing hierarchy of creator classes.
Prototype Design Pattern
Creates a new object from an existing object. The Prototype pattern is generally used when we have an instance of the class (prototype) and we’d like to create new objects by just copying the prototype.
What are the advantages of using the prototype design pattern?
- This pattern should be used, if the cost of creating a new object is expensive and resource intensive.
- It reduces the need of sub-classing.
- It hides complexities of creating objects.
- The clients can get new objects without knowing which type of object it will be.
- It lets you add or remove objects at runtime.
Disadvantage of Prototype Design Pattern
Cloning complex objects that have circular references might be very tricky.
Singleton Design Pattern
Ensures only one instance of an object is created. The Singleton Design Pattern aims to keep a check on initialization of objects of a particular class by ensuring that only one instance of the object exists throughout the Application memory.
A Singleton class also provides one unique global access point to the object so that each subsequent call to the access point returns only that particular object.
What are the advantages of using the Singleton Design Pattern?
- You can be sure that a class has only a single instance.
- You gain a global access point to that instance.
- The singleton object is initialized only when it’s requested for the first time.
Disadvantage of Singleton Design Pattern
- Violates the Single Responsibility Principle. The pattern solves two problems at the time.
- The Singleton pattern can mask bad design, for instance, when the components of the program know too much about each other.
- The pattern requires special treatment in a multithreaded environment so that multiple threads won’t create a singleton object several times.
- It may be difficult to unit test the client code of the Singleton because many test frameworks rely on inheritance when producing mock objects. Since the constructor of the singleton class is private and overriding static methods is impossible in most languages, you will need to think of a creative way to mock the singleton. Or just don’t write the tests. Or don’t use the Singleton pattern.
What are the 23 design patterns?