Creational Design Patterns

Learn via video courses

Overview

Creational design patterns are design patterns which focus on creating objects effectively. They bring flexibility to the software design and help in the reusability of existing code. There are mainly 6 types of creational design patterns: Factory Method, Abstract Factory, Singleton, Prototype, Builder and Object Pool pattern.

Scope

  • This article talks about creational design patterns and its significance.
  • We also cover the 6 different types of creational design patterns.

What are Creational Design Patterns?

A design pattern refers to a generalized, reusable solution to commonly occurring scenarios in software design. Creational design patterns are design patterns that focus on creating objects effectively. In the simplest terms, an object is created by instantiating its corresponding class. But there are some scenarios where the nature of the object changes according to the nature of the problem at hand.

This is where creational design patterns come to the rescue. Creational design patterns provide object creation mechanisms that reduce the level of complexity in our design. They help in creating objects in a more generalized way that increases flexibility and enables the reuse of existing code.

Types of Creational Design Patterns

CREATIONAL DESIGN

There are 6 types of creational design patterns: Factory Method, Abstract Factory, Singleton, Prototype, Builder, and Object Pool pattern. Let us learn about these in detail in the further subsections.

Factory Method Pattern

Factory Method creates an instance of several derived classes belonging to a single family of objects. It helps to create objects in a superclass but also allows the subclasses to modify the type of objects that are created. We can hide the existence of classes from the end user using the Factory Method pattern. Usually, a software design starts with a Factory Method which is comparatively simpler and more customizable.

FACTORY METHOD

Suppose we go to an ice cream factory and order a chocolate ice cream. When we place an order, the shopkeeper sends the order to the kitchen and we get it in a few minutes when the ice cream is ready. We are not concerned with what happens inside the kitchen and what ingredients are used to make the ice cream. The internal call by the shopkeeper to create a vanilla, chocolate, or strawberry ice cream is hidden from the user.

Abstract Factory Pattern

Abstract Factory pattern creates an object of several families of classes. It lets us produce families of related objects without specifying their concrete classes. While a Factory Method can produce only one type of object (only icecreams), an Abstract Factory can produce a family of different kinds of objects (multi-course and multi-cuisine meals). One generalized factory can consist of multiple specialized factories, each producing a different kind of object.

ABSTRACT FACTORY

Let’s take an example of a multi-cuisine restaurant that serves Chinese, Indian, and Continental food and you opt for Chinese. There might be different specialized chefs for the different items and for different cuisines but you don’t need to get into the details. In fact, you would not even know which kitchen or chef is involved in preparing your food.

Singleton Pattern

Singleton is one of the simplest design patterns. It means that only one instance can be instantiated from a given class. It ensures that a class has only one instance and provides a global access point to this particular instance. Singleton pattern helps in saving memory since the object is not created at every request. It either creates a new object or returns an existing object which has already been created.

SINGLETON PATTERN

You can think of a singleton object as the President or Prime Minister of a country. There can be only one of these in an entire nation and hence, such an object should only be instantiated once. Singleton pattern should be used when a given class can have only one instance is available to all clients. For example, a single database object which is shared by all the different parts of a program is an example of a singleton class.

Prototype Pattern

Prototype pattern creates a fully initialized instance that can be cloned. It lets us copy already existing objects without making the code dependent on other classes. A prototype pattern builds a product by copying the initial state of a prototype object.

PATTERN PROTOTYPE

We can take a board game like chess as an example. The initial setup of a chess board is initialized once with the king, rooks, bishops, and other pieces in their designated places. Every time a new game starts, we call this object with the initial setup is already done, clone it and begin the game. There’s no need to create a new setup every single time the game is played.

Builder Pattern

Builder pattern separates the complex object construction from its representation. It hides the internal structure from the end user and provides an easy interface to the outside world. Builder pattern focuses on constructing a complex product incrementally. It is designed as a composition of one or more similar objects having similar functionality. It allows us to produce different types of an object using the same code.

BUILDER PATTERN

Imagine you and your friend go to Subway and order your favorite subs. Your choice of bread, salad, and toppings may be very different but the process of making the sub is similar. You may choose the same bread but a different topping - the final object (sub) is different but the process (code) to make the sub is the same.

Object Pool Pattern

Object Pool pattern helps in the reuse of objects that are expensive to create by recycling objects which are no longer in use. It is most effective in situations where the rate and cost of instantiating a class is high. A common use case of the Object Pool pattern is when there are several clients who need the same resource at different times. For example, if we need to open many connections to a database, it takes longer to create a new instance and the database server will be overloaded.

object pool

An object pool helps to manage available resources in a better way. It is like a container having a number of instantiated objects. Objects in the pool have a defined lifecycle: Create, Validate and Destroy. When an object is taken from the pool, it is not available in the pool until it is put back. Once it is put back, it can be reused by another client which needs that particular object.

Problems Solved by Creational Design Patterns

Creational design patterns separate a system from how its objects are created and represented. They increase the system's flexibility and help in creating objects effectively. They reduce the complexity in our design and enable the reuse of existing code.

Creational design patterns can be complementary in nature, i.e. they may consist of more than one type of design pattern. Abstract Factory might consist of a set of Prototypes from which to clone. Similarly, the Abstract Factory, Builder and Prototype patterns can use Singleton in their implementation.

Conclusion

  • A design pattern refers to a generalized, reusable solution to commonly occurring scenarios in software design.
  • Creational design patterns are design patterns that separate a system from how its objects are created and represented.
  • They bring flexibility to the software design and help in reusability of existing code.
  • There are 6 types of creational design patterns: Factory Method, Abstract Factory, Singleton, Prototype, Builder, and Object Pool pattern.
  • Creational design patterns can be complementary in nature, i.e. they may consist of more than one type of design pattern.