Creational Design Patterns Benchmark

Kerem TAN
5 min readAug 25, 2023

--

Hello dear readers,
I would like to share with you a simple research and study I did when I was a student at university and the results of this study in this article.

The creational design patterns in this article are written with Java Programming language. Therefore, performance values of this article may vary with different approaches in different programming languages.

For example, the objects to be wanted to produce with these creational design patterns can be created using the CRTP(Curiously Recurring Template Pattern) idiom in the C++ Programming language.

Simply aim of this article is to compare the object production costs of creational design patterns.

First of all, I would like to remind you that our main priority when using Design Patterns is to write sustainable and clean code instead of gaining performance.

Our main aim of this article is to try to look at creational design patterns from a different perspective. Moreover, it is to try to understand how the performance of our project can affected, as well as disrupt concepts such as sustainability, clean coding, and similar concepts with the wrong use of creational design patterns in this study.

Our main function in App.java at the project measures the total cost of objects produced with each creational design pattern.

In order to make these observations, 3 concrete classes has been written that can be produced in different ways. These classes are inside the Customer package.

There are a few examples of the graphics we get when we run our App.java class below.

Output 1
Output 2
Output 3
Output 4
Output 5

Summary of Comparison Results

In the tests we conducted at this stage, although there were exceptional results, as a result of our observations, the creational design pattern with the highest production cost among the creational design patterns was the Builder Design Pattern.

The creational design pattern with the second highest production cost is the Abstract Factory Design Pattern, and the third highest creational design pattern is the Simple Factory Design Pattern.

According to our observations, the creational design pattern with the lowest production cost was the Prototype Design Pattern.

Predictions About The Comparison Results

~ Builder Design Pattern;
As it is known, the builder design pattern allows us to produce objects that are different and with the flexibility we want.

The builder design pattern first creates its own object in order to provide us with the flexibility of production, and we can set any field with an eligible value we want with this created builder object. After setting the fields we want, we produce the object we want to produce and we set all fields of the builder object to the relevant fields of the object we want to produce. Finally, we perform the build operation, and the object we want to produce is ready.

In order to produce our objects with the flexibility we want with the builder design pattern, we need to produce 2 objects at a time and adjust the all fields of these objects to each other.

These reasons may have increased more our production cost than other creational design patterns.

~ Abstract Factory Design Pattern;
As it is known, an abstract factory design pattern is used when we need to produce a large number of different or complex objects.

For each object we want to produce in an abstract factory design pattern, we write a class that is responsible for producing that object.
Thus, we can add or remove a new class to our system very quickly and flexibly. In addition, we do not need to make any fixes on our system for these changes.

However, every time we produce an object with an abstract factory design pattern, we produce 2 objects. Therefore, the cost of producing objects with abstract factory design patterns may have been the second highest cost.

The reason why the production cost of the abstract factory design pattern is lower than the builder design pattern may be that we do not have to set the fields one by one each time in the abstract factory design pattern.

~ Simple Factory Design Pattern;
As it is known, a simple factory design pattern is used when we need to produce a large number of different objects.

Unlike the abstract factory design pattern, a single object is responsible for the production of objects in the simple factory design pattern instead of producing different objects for the production of objects. The responsible object created for production produces the necessary objects through its own decision mechanism.

The reason why the simple factory design pattern is lower-cost to produce objects than previous creational design patterns may be that it produces and uses an object responsible for production only once instead of producing another object each time to produce an object.

However, the following details should not be overlooked. If the number of decision mechanisms was increased in the simple factory design pattern, each decision structure would be controlled. Therefore, there may increase in the total production cost of objects for the simple factory design more than the previous creational design patterns. In addition, each time a new class is added or a class is removed, the class responsible for generating the objects also needs to be corrected.

~ Prototype Design Pattern;
As it is known, the prototype design pattern allows us to clone an object that we have produced before and use it by changing only the necessary fields instead of producing a new object if the objects we will produce are based on a certain template.

The reason why the prototype design pattern has the lowest object production cost may be that, unlike other creational design patterns, it produces new objects by cloning an existing object instead of producing an object from scratch.

Thank you for reading this far.

If you wish to measure the instantaneous cost of each object produced with each creational design pattern, you can download the project and run our main function in App2.java at the project.

You can reach the codes for this article in here:

You can reach the Turkish version of this article in here:

--

--