Open In App

Creational Design Patterns

Last Updated : 04 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented. A class creational pattern uses inheritance to vary the class that’s instantiated, whereas an object creational pattern will delegate instantiation to another object.

Creational patterns give a lot of flexibility in what gets created, who creates it, how it gets created, and, when.

There are two recurring themes in these patterns:

  • They all encapsulate knowledge about which concrete class the system uses.
  • They hide how instances of these classes are created and put together.

Example of Creational Design Patterns

Building a maze for a computer game, the maze and the game will vary slightly from pattern to pattern. We ignore what can be in a maze and whether a maze game has single or multiple players. Instead, we just focus on how the maze is created. We define a maze as a set of rooms, a room knows its neighbors; possible neighbors are another room, a wall, or a door to another room.

Sometimes the game will be simply to find your way out of a maze; in that case player will probably only have a local view of the maze. Sometimes the maze contains problems to solve and dangers to overcome and these games may provide a map of the part of the maze that has been explored.

Types of Creational Design Patterns

  • Factory Method Design Patterns
  • Abstract Factory Method Design Patterns
  • Singleton Method Design Pattern
  • Prototype Method Design Patterns
  • Builder Method Design Patterns

Type-of-Creational-Design-Pattern

Sometimes creational patterns are competitors. For Example: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complimentary: Builder can use one of the other patterns to implement which components get built. The Prototype can use Singleton in its implementation.

Factory Method Design Pattern

Factory Method or Factory Design Patterns also known as virtual constructor, it define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

When to use Factory Method

  • A class can’t anticipate the class of objects it must create.
  • A class wants its subclass to specify the objects it creates.
  • Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.

Abstract Factory Method Design Pattern

Abstract Factory method or Abstract Factory Design Patterns provides for creating families of related or dependent objects without specifying their concreate class. It is also knows as Kit.

When to use Abstract Factory Method:

  • A system should be independent of how its products are created, composed, and represented.
  • A system should be configured with one of multiple families of products.
  • A family of related product objects is designed to be used together, and you need to enforce this constraint.
  • You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Singleton Method Design Pattern

Singleton Method or Singleton Design Patterns ensure a class only has one instance and provide glocal point of access to it.

When to use Singleton Method:

  • There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
  • When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Prototype Method Design Pattern

Prototype method or Prototype design patterns it specify the kinds of objects to create using a prototypical instance and create new objects by copying this prototype.

When to use Prototype Method:

Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and

  • When the classes to instantiate are specified at run-time, for example, by dynamic loading.
  • To avoid building a class hierarchy offactories that parallels the class hierarchy of products.
  • When instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

Builder Method Design Pattern

Builder Method or Builder Method Design Patterns it separate the construction of a complex object from its representation so that the same construction process can create different representations.

When to use Builder Method:

  • The algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled.
  • The construction process must allow different representations for the object that’s constructed.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads