According to the Gang of Four (GoF) definition, there are 3 types of software design patterns. Those are creational, behavioural, and structural patterns. This article will discuss the creational design pattern.
What are Creational Design Patterns?
As the name implies, this set of design patterns focuses more on object creation process. All the including patterns in the creational design pattern category give solutions for any problematic and complex object creation or class instantiation scenarios. Each pattern will introduce a way to create objects in a manner suitable to the existing situation in the application. Creational design pattern focuses on separating the object creation, composition, and representation from the client application.
There are two main principals when formulating creational design patterns. One is knowledge encapsulation about the concrete classes, which use inside the system and the second is information hiding regarding the creation and combination of object instantiation.
Creational patterns can categorize into two parts. Those are object-creational patterns and class-creational patterns. Object-creational patterns deal especially with object creation while class-creational patterns specialized on class instantiation patterns.
The usage of each creational pattern is different based on the scenario. The environment, super-classes, factories will vary depending on the existing requirement. Let’s find out what each creational pattern is good at and when to apply each pattern.
Basic Principles and Guidelines for Creational Design Patterns
- The system or the client program should be independent of how it’s objects and the projects are created.
- Implementations of class libraries are hidden and only exposed via interfaces
- Sets of related and similar behavioural objects will be used together.
- Class instantiations are specified at run-time
- Single instances can be accessed by clients any-time
- Ability to construct different representations of same independent object
Types of Creational Design Patterns
- Singleton Pattern
- Factory Method Pattern
- Abstract Factory Pattern
- Builder Pattern
- Prototype Pattern
Let’s get an overall basic idea of each design pattern and then compare each of those with one another.
The main principle behind the singleton pattern is keeping a single class instance per a java virtual machine. A class becomes a singleton and it restricts its own instantiation for a one time. That single instance will serve all other classes within the same JVM. To ensure all the clients will reach the singleton, pattern implementation is providing a global access point to that single instance. Main implementation steps include making the constructor private to restrict the instantiation and introducing a static creation method with a way to instantiate the singleton object and keeping a static variable to store the created singleton. Implementation varies depending on the requirements and environment of the problem.
Prototype Design Pattern
The main principle behind the prototype pattern is cloning an existing object. The main reason behind cloning is the cost of object creation with large time and resource consumption. The Implementation uses Java cloning mechanism, which mandates the inheritance of Cloneable marker interface by the original class. The features and behaviors of the copy may not be 100% equal to the original copy. The client needs to modify the copy as per their requirement. You can use deep copy or shallow copy depending on the requirement and problem state.
Factory Method Pattern
Factory method pattern is based on the sub class and super class theory. In this scenario, there are several sub-classes for a one super-class. The appropriate sub-class will decide the type of the object that will cater the requirement. But the type of the object is not specified by the client. A separate class called ‘Factory’ decides it. The factory class takes the responsibility of instantiating the suitable object on behalf of the client. Factory class contains the abstract method, which should be implemented by each concrete class. This is called the ‘factory method’. All products must have a common interface and that interface helps to decide the suitable concrete object at run-time. Because the sub-class factory method contains the customized concrete object construction code, factory method, also known as ‘Virtual Constructor’.
The factory method pattern defines an interface for creating an object and leaves the choice of type to the subclasses. Factory method design pattern makes a design more customizable and only a little complicated.
Abstract Factory Pattern
Abstract factory pattern add another high-level layer to the factory method design pattern. This is used to create individual concrete objects so that they are in match with other related objects of the same family. In general, one factory creates different types of concrete objects. In this pattern, that one factory represents a family of related objects. An application contains several of such factory classes and there is a one abstract factory interface. Abstract factory interface is the base interface for creating all products of a family. A concrete factory class contains the creation of concrete product code. The program creates concrete factory objects at the initialization stage. Then the required object type is determined at run-time depending on the concrete factory class. The client doesn’t know the object creation mechanism or doesn’t need to specify construction logic.
Builder Design Pattern
Builder pattern simplifies the way of creating the complex object with a large number of parameters and states. It introduces a mechanism to create an object step-by-step using the correct sequence of actions. It also enables building different types and representations of an object using the same building process. In this pattern, object construction code is moved to a separate class called ‘Builder’. The ‘Builder’ class declares the steps required to build the complex object. The construction of concrete objects is controlled by a class called ‘Director’ that only requires to know the object type.
Understanding the differences
Singleton vs Prototype
Singleton and Prototype design patterns are based on individual object instances. While the singleton pattern uses one and only one class instance per a java virtual machine, prototype pattern uses an original object instance to create clones when required. We can use both patterns when the object creation mechanism is time-consuming and costly. Anyway, singleton pattern uses two variations of the loading mechanism to retrieve the singleton object and prototype design pattern use two cloning mechanisms to copy the original object as required.
Factory Method Pattern vs Abstract Factory Pattern
Then most people confuse around factory method and abstract factory design patterns. Both use the principle of the factory to achieve the solutions. Both patterns support to create concrete products without putting the object creation burden on the client. Both use an abstract class or base interface to separate the implementation details from the client. Factory method pattern uses an abstract method called ‘factory method’ to encapsulate the common behavior. This ‘factory method’ will then override in sub-classes to achieve the concrete behaviors for each product. The ‘Factory’ super class contains the abstract factory method and sub-classes will override it as requires. In contrast, abstract factory pattern adds another layer to the factory level. It introduces another abstraction when there are several families of factories. Abstract factory composite all those different factory behaviors and provide factory methods for each family. Then, each family should introduce a concrete factory to support creating each concrete product. Each concrete factory will implement the abstract factory interface and then override the appropriate factory method to generate the product. Hence, the client is again not aware how the objects are constructed. Therefore, abstract factory pattern contains several factory methods. In summary, abstract factory design pattern creates the factory and factory method pattern create the concrete product.
Builder Pattern vs Factory Patterns
Builder pattern also behaves bit similar to factory patterns by summing up all complex building steps to one dedicated class. When factory patterns produce several related products via one method call, builder pattern produces different types and representations of the same object using a common building process. In the builder pattern, object construction code is made independent by delegating that responsibility to a separate class called ‘Builder’. The user can create any deviations of the product via the ‘Builder’ class using the same building process. On the other hand, abstract factory pattern, composite and abstract the concrete object creations in factory methods, and return as a fully created object within one method call.