creational patterns

Didem Yanıktepe
3 min readMar 9, 2022

--

Creation design patterns guide us to create objects that fit the situation. We’ll look at how we can more effectively create objects based on states.

Builder Pattern

when piecewise object construction is complicated, provide an API for doing it succinctly

Lets build a hamburger with together.

HamburgerBuilder

The builder pattern allows you to build complex objects step by step. The Builder pattern allows us to create objects with the fields we need. We didn’t need meat when building hamburger, instead of defining multiple constructors, we created it easily with the builder pattern.

Factory Method

A component responsibly solely for the wholesale (not piecewise) creation of objects.

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory model, you always use parameters to specify which instance you want to get

Dialog Factory
Products
Factory
  • Use factory pattern when a class have no idea which sub-class should create the instance

Abstract Factory Pattern

Define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes.

These are our interfaces to build our kingdom

With Kingdom factory, we can make orc kingdoms, not just elves.

Difference between factory vs. abstract factory

  • The Factory Method aims to produce more than one relational object with a request to be made through a single class through a common interface and to reduce the dependency of the client on the produced object to zero at the time of object production.
  • Abstract Factory, on the other hand, provides the production of more than one relational object, not by a single interface, but by defining a different interface for each product family.

Singleton Pattern

The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.

two form of singleton

— early ,instantiation: creation of instance at load time
— lazy instantiation: creation of instance when required

Singleton Pattern Checklist

— private constructor, static member, static factory method

Prototype Pattern

Identifying objects to be created using a prototype object and creating new objects by copying the prototype.

USE IT! WHEN copying an object can be more efficient than creating a new object.

In summary, the ProtoType Design Pattern is a design pattern that allows us to copy repetitive objects without recreating them.

--

--