Factory Pattern with an easy example

Anthony Rodriguez
6 min readJul 7, 2021

Design patterns are existing solutions created over time by encountering the same kind of problems in different scenarios. By applying design patterns, you leverage the knowledge of past mistakes and apply standard solutions, making code easier to comprehend and change if necessary.

The factory pattern’s main concern is to solve the problem of creating a new object where the actual creation is relayed/taken care of by another class. This other class decides what object will be created and when during the entire runtime.

A basic introduction would be as follows: there are multiple products you’d like to create as objects. Every product does a similar task or has shared properties, like a car and a motorcycle, both can be driven and both have a license plate (as an example). Since those products have similar characteristics, you could abstract those into a base class. To create those objects, you’ll need what will be named as a “factory” class. This “factory” class will construct that object by calling the specific object constructor you’d like to create inside one of its methods. These can be considered the “product” factories, since they create a specific object. To tie them all together in one coherent base, you’d also need a “generic” factory, this way you can be certain that those “product” factories will contain the same methods to create the objects.

The benefits from using factory pattern include not showing/exposing how the object creating occurs and being able to add as many classes as possible without altering how the base code works. Also, since the classes are independent of each other, testing each individual class, and therefore, object creation is a relatively simple task. Added to that, it’s easier to make changes to any particular class if the requirements need it.

The downsides are mostly related to having more overall code. Since the object creation is hidden behind classes (abstracting the creation of a class object behind another class), it makes the tracking of the code harder to read. Also, since there needs to be a “product” factory for every object you’d like to create, the amount of classes increases. Also, if a change is to be done to the base class of either the objects created or the factories that create those objects, all the classes that extend those bases must change as well.

If applied correctly, you can capitalize on the factory pattern benefits and help in situations where you don’t know the specific product being created or there will be a lot of new classes each constantly.
Now that the basics are understood, let’s dive into a more concrete example.

Example:

Your uncle is traveling around the world and you asked him to send you a bill of any place he visited. His tour covered the countries of Japan, India, England and Mexico. Wanting to prepare in advance for the bill he will send back, you decide to make a system to start your numismatic collection in digital form. You start by asking the country, the denomination (or value) and the year it was emitted:

Start

But then, you come to the conclusion that the bill hasn’t arrived yet. You don’t know exactly what bill will arrive. How could you build a program that determines at runtime any bill that arrives?
Remembering something a teacher once said, you decide to research again what the factory pattern is and decide to apply it.

First of all, you would need to create a base for all bills. This will contain all the attributes you want each bill to have, which will be the name, value and year. Since this will only be a base class used to abstract the similar attributes, it will be an abstract class with abstract properties

Base Abstract Bill class

Now that the base is finalized, comes the actual creation of the “products”, in this case, the specialized bills.

Yen bill

In the example shown above, we use the Yen bill from Japan, but you’ll need to create one for the Mexican Peso, the English Pound and the Indian Rupee. Since the products are basically the same, you’d only need to change the class name and the name of the bill used in the constructor.

Now that all the products have their own class to be created, the class that will actually create them needs to exist. And again, since the creation process is the one we want to streamline through the factory pattern, there needs to be a base factory. This base factory will contain all methods every “product” factory, which in this particular example will only display the name, value and year of the bill. Following the logic that this class will only be used as a template from which all “product” factories will derive, we’ll use an abstract class.

Base factory class

With the base/generic factory class in place, now we can create the “product” factories that will create a specific product.

“Product” factory for creating yen bills

As shown in the example above, the “YenFactory” class created Yen bills. This is done through the “GetBillInformation” method, which was derived from the BillFactory base factory. Since the same method will be used for each “product” factory, this takes the responsibility of creation away from the main code, and by only determining the factory, we are guaranteed that a specific class object will be returned.

With all that work done, we’re ready to take advantage of the factory pattern and create any bill that your uncle will send you. But first of all, we’ll need to create an empty factory variable. This will be used to store the factory that was selected and later on use the method to create the desired object.

Same starting code but with the factory variable

With that factory variable, all that is needed is to decide which factory will be used. Since the bills are tied to a specific country, depending on the country the bill came from, we will call a specific factory to create the bill from that country.

Deciding which factory to use

Once the factory is chosen, it’s as simple as creating a new bill by calling the method shared by all “product” factories through the base factory and then doing as necessary. For our example, we’ll only print the information of the bill

Creating the bill and printing the information

And with that, the easy example to explain the factory pattern is completed. As promised, your uncle did deliver a bill, it being a 1000 yen from 1990. Since now you know, you put it through the program you just made and this should be the result:

Console output

To access the code, you can download it here (https://github.com/AnthonyWRodriguez/FactoryPattern)

--

--