The Wayback Machine - https://web.archive.org/web/20240709001230/https://www.geeksforgeeks.org/creational-design-pattern/
Open In App

Creational Design Patterns

Last Updated : 02 Jul, 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.
Creational-Design-Patterns

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.


Previous Article
Next Article

Similar Reads

Difference Between Architectural Style, Architectural Patterns and Design Patterns
Many software professionals think that architectural styles and patterns are the same. Sadly, some of the software developers don’t understand the difference between architectural patterns and design patterns. In this article, we're going to summarize the differences between them. According to MSDN, architectural styles and patterns are the same th
7 min read
Connectivity and Composition Patterns | Design Patterns for Cloud Native Applications
In cloud-native applications, the architectural design becomes a critical aspect, defining the success of the entire solution. Connectivity and composition patterns are fundamental design principles that dictate how different components within a cloud-native application communicate and collaborate. Let's delve deeper into the significance of these
9 min read
Composite Design Pattern | JavaScript Design Patterns
The Composite Design pattern is a way of organizing objects. It helps us handle different objects in a similar way when they are put together to create a structure with parts and wholes. These parts and wholes are like building blocks that can be split into smaller pieces and then put together to make a tree-like structure. The composite design pat
6 min read
Strategy Method Design Pattern | C++ Design Patterns
Strategy Pattern is a behavioral design pattern that defines a family of interchangeable algorithms and allows them to be used interchangeably within a context. This pattern enables the algorithm to be selected at runtime, providing flexibility and promoting code reusability. Important Topics for the Strategy Method in C++ Design Patterns Example o
4 min read
Memento Design Pattern | C++ Design Patterns
Memento Design Pattern is a behavioral design pattern that provides a mechanism for capturing an object's internal state and restoring it to that state at a later time. This pattern is useful when we need to implement features like undo/redo functionality or when we want to save and restore an object's state for various reasons. Important Topics fo
7 min read
What is the Correlation Between System Design and Design Patterns?
System design and design patterns are closely related concepts in software engineering, with design patterns serving as reusable solutions to common design problems encountered during system design. System design and design patterns are interrelated concepts that work together to create robust and well-structured software systems. Important Topics
11 min read
Design Patterns Cheat Sheet - When to Use Which Design Pattern?
In system design, selecting the right design pattern is related to choosing the right tool for the job. It's essential for crafting scalable, maintainable, and efficient systems. Yet, among a lot of options, the decision can be difficult. This Design Patterns Cheat Sheet serves as a guide, helping you on the path toward optimal design pattern selec
7 min read
State Method Design Pattern | C++ Design Patterns
In software design, managing the behavior of an object according to its internal state is a common issue. The state pattern addresses this issue by allowing an object to alter its behavior every time its internal state changes. This pattern encapsulates each state in a separate class, which makes it easier to add new states and modify existing stat
7 min read
Template Method Design Pattern | C++ Design Patterns
Template Method Pattern introduces a template in a superclass that defines the steps of an algorithm. These steps may include both common tasks shared among subclasses and specific tasks that need customization. Subclasses then implement or override these steps to modify the algorithm according to their specific needs. Important Topics for Template
7 min read
Design Patterns: Understand The Importance With Real Life Examples
Software Design Patterns... Software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. This is the definition written on Wikipedia for software design patterns...The above definition makes sense to you if you are an experienced developer and spent some time writing the code for
8 min read
Article Tags :