Design Patterns: The Factory Method Pattern

Design Patterns: The Factory Method Pattern

In the previous article, we went through the Simple Factory Pattern. Now in this article we will examine the Factory Method Design pattern. In the case of Simple Factory, it provides an interface to create objects, while the Factory method does the same thing but in addition it allows a subclass to make a decision on which class to instantiate.

For explaining the Simple Factory Method pattern, I used the example of creating an object of a car with various car types, which are Sedan, SUV, etc. I will continue the same example to explain the Factory Method pattern so we will have a better idea and continuity.

The Problem

In a simple factory, we were creating an object of the car class but actually it was creating an object of the car class based on the car type we have passed. This was a good step to start with creating objects for a car with different types.

Earlier our company was limited to selling a car in the USA only, and it has one production center in the USA only. But as time goes on, the company expands and it decides to sell cars in the UK and build a new production center in the UK too.

In both production centers, our old code to build cars works fine, but then to improve customer satisfaction, the company decides on some changes in the car model, but for UK cars only. At this stage, our code to build an object of a car will fail because we need some extra features (attributes) for one specific type of cars only.

We have two options in this case: either modify the current code (multiple if else conditions) to get the desired object of car, or restructure our classes in such a way that does not require dirty if else conditions in your class and in future if you want to add a few more features but for limited types of class only.

The first approach no one would like to go with because it's a kind of patchwork you are doing in your code and not the real implementation. In the next section, we will see how we can implement the second approach by using the Factory Method design pattern.

The Solution

This design pattern falls under the structural pattern category, so it is more concerned with how you structure your classes. So let's get to the solution of our problem by structuring our classes in the proper way.

In a simple factory, we did not use a structure because we needed only one class which was responsible for creating an object of the car. Based on the current problem, we need to create a separate factory class for both production centers, so we want to make sure all our factories are following the same process to create an object of a class. Hence, we will make one interface which both the US and UK factory will implement.

Now our interface for factories is ready, and it's time to create classes of our factories which are the US and UK car factory.

At this stage, we have our factory classes ready, and it's time now to create our concrete class. But as with the factory, we want our concrete classes to implement some required methods which are common to all concrete classes.

We have added very basic methods to our class to get the location and type of car. Now we will implement our concrete classes as below.

We are done with the implementation of our concrete classes, which actually are an object of car based on location and type of car. Here what's left is to use this implementation to see how it solves our problem explained above.

You can say that we have effectively created objects of all possible types of car with the location. Our classes are well structured now, so there won't be much trouble adding a new factory.  

Adding a New Factory Class

Let's imagine a company has decided to start a new production center in Australia.

As our classes are well structured now, there won't be an issue adding a new class now to build a car object for the Australian production center. 

Wrapping Up

So we have seen how we can structure our classes in such a way that they can be extended without making any changes to your base classes and client code. Feel free to add your inputs in the comment section below or tweet me at @XpertDevelopers.

Source: Tuts Plus

About the Author

Comments

  1. … [Trackback]

    […] There you will find 63950 more Infos: designncode.in/design-patterns-the-factory-method-pattern/ […]

  2. … [Trackback]

    […] Read More here: designncode.in/design-patterns-the-factory-method-pattern/ […]

  3. … [Trackback]

    […] Find More Informations here: designncode.in/design-patterns-the-factory-method-pattern/ […]

  4. … [Trackback]

    […] Read More Infos here: designncode.in/design-patterns-the-factory-method-pattern/ […]

  5. … [Trackback]

    […] There you will find 77414 more Infos: designncode.in/design-patterns-the-factory-method-pattern/ […]

  6. … [Trackback]

    […] Read More here: designncode.in/design-patterns-the-factory-method-pattern/ […]

  7. … [Trackback]

    […] Informations on that Topic: designncode.in/design-patterns-the-factory-method-pattern/ […]

  8. mediumchat says:

    … [Trackback]

    […] Read More: designncode.in/design-patterns-the-factory-method-pattern/ […]

  9. … [Trackback]

    […] Read More: designncode.in/design-patterns-the-factory-method-pattern/ […]

  10. explanation says:

    … [Trackback]

    […] Informations on that Topic: designncode.in/design-patterns-the-factory-method-pattern/ […]

  11. … [Trackback]

    […] Read More here: designncode.in/design-patterns-the-factory-method-pattern/ […]

  12. … [Trackback]

    […] Read More: designncode.in/design-patterns-the-factory-method-pattern/ […]

  13. … [Trackback]

    […] Read More here: designncode.in/design-patterns-the-factory-method-pattern/ […]

  14. … [Trackback]

    […] There you will find 28837 more Infos: designncode.in/design-patterns-the-factory-method-pattern/ […]

  15. … [Trackback]

    […] There you will find 34504 more Infos: designncode.in/design-patterns-the-factory-method-pattern/ […]

  16. … [Trackback]

    […] Read More here: designncode.in/design-patterns-the-factory-method-pattern/ […]