Photo by Randy Fath on Unsplash

Creational Patterns — overview

Daniel Jankowski
Netcompany
Published in
5 min readDec 12, 2019

--

These articles are a part of the design pattern series I’m writing to teach developers how to write clean and readable code that scale.

The main overview page can be found here.

The kind of pattern is associated with the process of creating objects. It introduces more control over the process. The goal is to separate the process of creating and representing the object.

There are five well-know design pattern that can be implemented:

  • Singelton
  • Abstract Factory
  • Prototype
  • Factory Method
  • Builder

Singelton

Singelton is useful when we want to share functionality with other classes through one instance. The goal is to limit the possibility of creating multiple objects of a class to one instance only.

Singelton is implemented by creating a class that has a static method. It first checks for an instance of this class by creating it if necessary. Then the instance is returned by reference. I usually use it for caches and thread pools.

Below I present simple cache and its uses:

Here’s the implementation part from the code above showing how caching works.

Abstract Factory

This type of design pattern is one of the best ways to create an object.
The abstract factory pattern is responsible for creating the factory of related objects without explicitly specifying their classes.
The factory allows creating sets of objects for to specific tasks. Each of the specific factory implements a different set of classes, but they always have a certain set of methods defined.

I prepared an example about storing things.

The ‘StorageFactory’ class describes the interface for storing liquids in things — there are two functions: ‘CreateThing’ and ‘CreateLiquid’.
The ‘InventoryFactory’ class is responsible for store oil in barrel.
The ‘TankFactory’ class is responsible for store water in tank.

The last class — ‘Storage’ — has reference to ‘StorageFactory’ and has one method ‘FillThingWithLiquid’. Please look at the example below:

Result:

Barrel was filled by Oil
Tank was filled by Water

The same code can be prepared using interfaces. Example below.

Example how you can run you it.

Builder

Usually this pattern consists of two basic elements: ‘Builder’ and ‘ConcreteBuilder’. The first of them(‘Builder’) purposes to provide an interface for creating objects called products. The second element proposes to create specific product representation using the implemented ‘Builder’ interface. The last part of this schema is the object named ‘Director’. This is the supervisor. It is responsibilities for correct order of build objects.

Builder schema

I prepared an example about this.

The Product is a small class which has Name and Price. The class ‘ProductInformation’ describe what we want to get. The interface ‘IProductsInformationBuilder’ has four methods which have to be run in valid order. The class ‘ProductsInformationBuilder’ implements interface and it purposes to prepare everything for Director. The ‘ProductsInformationDirector’ has only one method which invokes methods from Builder in the right order.

Example how you can run it.

Result:

This is simple report of products — 6.
Total price for all products is 24.5.

Prototype

If you need to reproduce multiple instances of objects, you should use this pattern. The prototype design pattern describes how to copy an object. The class itself is responsible for this. .NET has a dedicated interface called: ‘ICloneable’.

Below is simple schema for it:

I prepared some simple example. There are three classes ‘GeometricalFigure’ (base class), Rectangle and Square.

Example how you can run you it.

Factory Method

This type of design pattern is a single method that creates the same type of object. You can delegate creating objects to other classes. Usually system has the business class with method responsible for creating. This is to create a separate class to implement the factory method. It defines the interface to create the object, but instructs its subclass to decide which object to create.

‘ApplicationRentBike’ creates the Bike class, the next class ‘ApplicationRentCar’ creates object of the Car class.

What is the difference between Factory Method and Abstract Factory?
Abstract Factory is responsible for creating objects belonging to the same group. Usually, separate classes are created to implement this pattern.

Summary

Creational patterns help projects grow in a flexible way. They are more useful than hard-coding behaviors. This allows objects to be created in a controlled manner, which reduces complexity and increase stability.

I invite you to read the other articles in this series:

You can follow/contact me through this link: Daniel Jankowski.

Netcompany provides IT solutions for both large enterprises and governments

--

--