Creational Design Patterns

Shadman Jamil
3 min readFeb 24, 2022

--

These patterns provide a way to create objects while hiding the creation logic rather than instantiating objects. Example: encapsulation.

In simple words, these patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.

  • Singleton Pattern [A class of which only a single instance can exist]

This design pattern lets you ensure that a class has only one instance while providing a global access point to this instance. It solves two problems at a time, violating the Single Responsibility Principle.

Singleton’s implementation has two things in common:

  • Make the default constructor private, to prevent other objects from using the new operator with the Singleton class.
  • Create a static creation method that acts as a constructor. That method will call the private constructor to create an object and save it in a static field. As a result, all following calls to this method return the cached object.
  • Prototype Pattern [A fully initialized instance to be copied or cloned]

This pattern lets you copy existing objects without making your code dependent on their classes.

The Prototype pattern delegates the cloning process to the actual objects that are being cloned. The pattern declares a common interface for all objects that support cloning. This interface lets you clone an object without coupling your code to the class of that object.

The implementation of the clone the method is very similar in all classes. The method creates an object of the current class and carries over all the field values of the old object into the new one. You can even copy private fields because most programming languages let objects access the private fields of other objects that belong to the same class.

This pattern is used when the creation of an object directly is costly. Ex: An object is to be created after a costly database operation. We can cache the object, return its clone on the next request, and update the database as and when needed, thus reducing database calls.

  • Builder Pattern [Separates object construction from its representation]

This pattern lets you construct complex objects step by step. This pattern allows you to produce different types and representations of an object using the same construction code.

The Builder pattern suggests that you extract the object construction code out of its class and move it to separate objects called builders.

  • Factory Method Pattern [Creates an instance of several derived classes]

This pattern deals with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method.

It provides an interface for creating objects in a base class but allows subclasses to alter the type of objects that will be created.

Either specified in an interface and implemented by subclasses or implemented in a base class and optionally overridden by derived classes, rather than by calling a constructor.

  • Abstract Factory Pattern [Creates an instance of several families of classes]

This pattern lets you produce families of related objects without specifying their concrete classes.

It works around a super-factory which creates other factories. This factory is also called a factory of factories.

Cheers !

--

--

Shadman Jamil

Tech Savvy, Software Architect, Mentor, and Entrepreneur