In software engineering, a Design Pattern describes an established solution to the most commonly encountered problems in software design. It represents the best practices evolved over a long period -through trial and error- by experienced software developers.

In this article we will talk about the Creational Design Patterns.

Creational Patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

The most common Creational Patterns are:

Factory Method Abstract Factory Builder Prototype Singleton

Factory Method Pattern

The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. Wait, What does this mean?

Let’s pretend that we want to be able to create soldiers. These soldiers can be from the Targaryen’s house or from the Lannister’s house.

According to the “Factory Method” pattern, we will need:

* An interface class to define our soldiers

* A class for each type of soldier to extend the abilities of each house

* A class to request the creation of a new soldier regardless of which house he comes from

Example of Factory Method Pattern

When to use the Factory Method Pattern?

When the implementation of an interface or an abstract class is expected to change frequently

When the current implementation cannot comfortably accommodate new change

When the initialization process is relatively simple, and the constructor only requires a handful of parameters

Abstract Factory Pattern

D isclaim: The Abstract Factory Pattern is strongly based on objects. Applying this pattern to a functional language (no javascript is not a pure object-oriented language, please do not start with flame) is not so trivial, this is why javascript implementation is different.

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In a normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn’t know (or care) which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products.

Let’s pretend that for our game we want to manage the houses individually, each house must be able to manage an heir to the throne and his subjects.

According to the “Factory Method” pattern, we will need:

* An interface class to define our heir to the throne

* A class for each heir to the throne

* An interface class to define our subject

* A class for each subjects

* An interface class to define our houses

* A class for each houses that will return an instance of her HeirToTheThrone and Subject

Example of Abstract Factory Pattern

When to use the Builder Design Pattern?

The client is independent on how we create and compose the objects in the system

The system consists of multiple families of objects, and these families are designed to be used together

We need a run-time value to construct a particular dependency

Builder Pattern

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. When the complexity of creating an object increases, the Builder pattern can separate out the instantiation process by using another object (a builder) to construct the object.

Let’s pretend that for our game we want to create a navy for each house. Each family will have a certain number of ships and warriors available. To facilitate the creation we want to be able to invoke only the makeNavy method that will automatically create everything you need through a configuration.

According to the “Builder” pattern, we will need:

* A class to start the creation of the Navy

* A Builder interface class to define our builder

* A ConcreteBuilder class that will be responsible for creating all the objects in our army

* The Soldier and Ship class that will will be responsible for creating the specific soldiers or ships

Example of Builder Design Pattern

When to use the Builder Design Pattern?

When the process involved in creating an object is extremely complex, with lots of mandatory and optional parameters

When an increase in the number of constructor parameters leads to a large list of constructors

When the client expects different representations for the object that’s constructed

Prototype Pattern

The Prototype Pattern allows you to specify the kinds of objects to create using a prototypical instance, and create new objects from the ‘skeleton’ of an existing object, thus boosting performance and keeping memory footprints to a minimum.

Let’s pretend that for our game we want to create a white walkers’ army we don’t have great pretensions for this army, we need only that they be really many, that they all have the same characteristics and that they don’t occupy us too much memory in our application.

According to the “Prototype” pattern, we will need:

* A class to store the information about the White Walker

* A clone method that returns another instance clone of the instance

Example of Prototype Design Pattern

The Pros and Cons of the Prototype Pattern

PROS

The Prototype Pattern helps reduce costs, time and contributes to increasing the performance of the program by not using the new operator to create new objects.

by not using the new operator to create new objects. It helps reduce complexity for object initialization : because each class only implements its own way of cloning.

: because each class only implements its own way of cloning. We can also avoid classifying and creating multiple sub-classes to initialize objects such as the Abstract Factory Pattern.

to initialize objects such as the Abstract Factory Pattern. It helps you increase the flexibility of the system because it allows creating new objects by changing some properties of the original object that it copied.

CONS

Cloning complex objects that have circular references might be very tricky

Singleton Pattern

The Singleton Design Pattern aims to keep a check on initialization of objects of a particular class by ensuring that only one instance of the object exists throughout the Program. This is useful when exactly one object is needed to coordinate actions across the system.

Let’s pretend that for our game we want to be sure that nobody can be the Mother of Dragons except the first one who will become it.

According to the “Singleton Method” pattern, we will need:

* A class to store the information of the Mother of Dragons

* An instance of the Mother of Dragon saved

Example of Singleton Design Pattern

When to use the Singleton Design Pattern?

For resources that are expensive to create (like database connection objects)

It’s good practice to keep all loggers as Singletons which increases performance

Classes which provide access to configuration settings for the application

Classes that contain resources that are accessed in shared mode