5/180: Creational Design Patterns

Builder, factory, abstract factory, singleton & prototype are all creational design patterns. Builder pattern I have already discussed in my previous blog.

Factory Design Pattern

Just life factories where products are manufactured in factory design pattern objects are created. We create factories which produces objects. What was the need of factory design pattern: When object creation method becomes too convoluted. Constructor is not descriptive i.e. Name mandated by name of containing type, cannot overload with same sets of arguments with different names, can turn into overloading hell.

A factory component is solely responsible for creation of object.

Abstract Factory

Though it’s rarely used somewhere, but we should be aware of its use case. When there are hierarchies of same type of product. Lets say I want a Hot Drink which can be tea or coffee so tea and coffee are subtypes, I can create abstract factory Hot Drink which can further create tea and coffee objects.

AbstractFactory.java (github.com)

A factory method is a static method that creates objects

A factory takes care of object creation.

A factory can be external or reside inside the object as an inner class in case you want to call the objects only via factory you can declare constructor private.

Hierarchies of factories can be used to create related objects.

Prototype Design Pattern

When it’s easier to copy an existing object to fully initialize a new one. The motivation behind this pattern was that complicated products (eg. cars) are not designed from scratch. They reiterate existing designs. An existing (partially or fully) constructed design is a prototype. We make a copy and customize the prototype. It required deep copy support. By deep copy it refers when copying object and its references.

So a prototype is a partially or fully initialized object that you copy and make use of.

We can create a prototype pattern using 3 ways:

  1. Cloneable
  2. Copy Constructor
  3. Serializable Interface

Prototype By Copy Constructor

Prototype by cloneable

PrototypePattern.java (github.com)

Prototype By Serialization

PrototypeSerialization.java (github.com)

To implement a prototype, partially construct an object and store it somewhere. Clone the prototype by implementing your own deep copy functionality. By serializing and deserialize. Customize the resulting instance.

Singleton Design Pattern

Singleton is one of the most common design pattern and is the last design pattern I am going to discuss in creational design patterns.

Why was the need of singleton pattern:

For some components it only makes sense to have one in the system like database repository, Object factory. Sometimes constructor calls are expensive we restrict it to once. We provide everyone with the same instance. Or want to prevent anyone creating additional copies.

A component which is instantiated only once, or object which is instantiated only once is a singleton pattern. We can achieve singleton class by declaring constructor private so that no outer class should be able to call the constructor and create a new instance. Other ways to create a singleton class:

  1. Static Block Singleton: In this instance is created inside static block
  2. Laziness & Thread safety: By laziness it refers create the instance only when it is required, but in case multiple threads are using the particular function it is possible that multiple instance gets created so to remove that problem we can add the synchronized variable
  3. Enum Based Singleton: Singleton can be achieved by enum also, in this case we don’t have to declare constructor private because by default enum have private constructor.
  4. Inner Static Singleton: It is when you create the instance inside the inner class. And you can create the instance inside inner class because we can access private variable/constructors inside inner class.
  5. Mono State: This way lets you create multiple instance but every instance as the same value because all the data members in the particular class are static. This is not recommended though.
  6. Muliton: This refers to when you are restricting the amount of objects you want to create. And for that you can use factory pattern also. creating private constructor and in the inner class create the object you want to.

SingletonPattern.java (github.com)

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.