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

A factory component is solely responsible for creation of object.

Abstract Factory

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

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

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.