Design Patterns: The Simple Factory Pattern

When you think of a factory, what comes to mind? For me, it's a place where things are created - that is, it's a centralized placed where things are produced. Later, the delivery of said products are done by the factory based on an order.

Let's say that you're requesting a car. A factory will create one based on the specifications of the work order and will then deliver it once it's complete.

Just as their real world counterparts, a software factory (that is, software that implements the factory design pattern), is an object that is responsible for creating and delivering other objects based on incoming parameters. 

There are three variations of the factory pattern:

  1. Simple Factory Pattern. This allows interfaces for creating objects without exposing the object creation logic to the client.
  2. Factory Method Pattern. This allows interfaces for creating objects, but allow subclasses to determine which class to instantiate.
  3. Abstract Factory Pattern. Unlike the above two patterns, an abstract factory is an interface to create of related objects without specifying/exposing their classes. We can also say that it provides an object of another factory that is responsible for creating required objects.

The Problem

Let's assume that you have a car class that contains all properties and methods relevant to a automobile. In it's most basic form, you'd create it like this:

Immediately, as time goes there needs to be some change in how the Car object is being created. We need to create class objects that are based on Cart Type instead of just Car. So you need to make changes in all the places where you have created object of this Car class. 

But, as time goes by, there will inevitably need to be changes in how the Car object is created. For example, we need to create classes based on a Car type rather than just Car. As such 

Instead of doing that, it would be a better decision to create a class that implements the Factory pattern. 

The Solution

In the previous section we recognized that we were creating an object of type Car using the new keyword. And later, it is decided to create an object of the Car class, but based on the Car type like Sedan, SUV, etc.

Either we should place the Car type class creation code at all places whenever required or implement Factory to handle it in an effective manner. Please refer below code block which shows the implementation of the Simple Factory Pattern.

In the above class, you can see that we have one static method available which is responsible to create the object based on the type you pass. Now we need concrete classes of different car types like what we have listed below:

At this point, we have our factory and concrete classes ready to use, so let's practice it for creating require car type objects.

Addition of New Class

The addition of a new class is simple: Create one concrete class and you are ready to go. For example:

Conclusion

When it comes to the factory pattern, consider the use of new keyword to create a new object as harmful. The factory allows us to have a centralized location well all of our objects are created.

Tags:

Comments

Related Articles