Misconception about Factory Method Design pattern

Introduction

This post is intended mostly for beginners who want to understand the Factory Method Design Pattern. Yes! the name is Factory Method Design Pattern and not Factory Pattern.

It is very important for me to start with the misconception about Factory Method Design pattern, this is a starter post and you might not learn anything about the Factory Method design pattern here. But yes! I will tell you what is not a factory method design pattern.

The story

Once upon a time there lived a Carpenter Jay in a village. He had this nice way of making beautiful wooden furniture and he used to sell them in his nice little home shop. Everyone used to love his furniture and he got famous in no time. The only furniture he used to make is CHAIR and TABLE.

It was a complicated task and he use to follow all the steps below rigorously:

Making the Furniture based on type(CHAIR, TABLE)

  • Slice the wood as per dimensions
  • Fix the legs
  • Glue the Furniture

Post Making

  • Paint the Furniture
  • Sand the Furniture for smoothing effect
  • Box the Furniture
  • Deliver it

In terms of programming he had a FurnitureShop and it had a method orderFurniture which accepted an argument to identify the type of furniture to make. Every thing used to happen in the same method. For e.g.: Find what to make, make the furniture, paint it, sand it, box it and deliver it.

Check the full code at github

The word spread and he was famous in neighboring villages as well, now people use to come from other villages for buying furniture. He thought of an idea for expanding and he hired two more carpenters, trained them with his unique technique of making chairs and tables and opened up shops in two of the neighboring villages.

Shop in Village 1

Shop in Village 2

 

Time for Upgrade

Things were going well and he gained a lot of popularity. He thought of trying some new stuff and started making furniture of type BED as well. It became a hit and he started earning more money.

Upgraded Furniture Shop

 

Unfortunately his other two shops in neighboring villages were still selling only chairs and tables because they didn’t recognize the furniture type BED.

So, here was a change in the menu which Jay offered and his original store recognized newer furniture types but the other two shops never knew of the new offering which is BED because their logic of identifying this type of furniture was not updated as it happened in the original shop.

If a customer came with a request to make a bed, they simply used to refuse them. Jay got worried and traveled to one village at a time and trained his carpenters on how to make the bed. After a few weeks, the other two shops started selling beds as well.

The Real Problem

But Jay was still concerned about something, he thought that next time if he adds some new furniture in his offerings he will again face the same problem and would need to travel long distances, train carpenters for a week and then may be they can sell newer stuff.

Problem: Every time there is a change/update the offerings, we need to change/update it at multiple locations.

In terms of programming if there is an addition and a new product is to be created, the client code required an update.

The Big Idea

An idea struck him and he thought of pulling out the process of furniture making from all his shops and established a big workshop.

Now there is one big workshop, all the carpenters sit there, training is done at one place. The shops now just accept orders and pass the order to the workshop. The workshop recognizes it and makes the furniture and sends it to the respective shop. The shop does the post making work like painting, smoothing, packing and delivery as this was trivial work.

Misconception about Factory Method Design pattern

The process was all good, everything was controlled centrally and Jay added another furniture type (SOFA) and the business is on boom now.

In terms of programming the CreateFurniture behavior was pulled out of the client code and put in a separate class which you can call a FurnitureFactory. All the clients now hold a reference to the FurnitureFactory and invoke the createFurniture method passing  the required type and get their respective furniture made. 

The other two classes now look simpler and do not contain the logic of making furniture as shown below:

 

Now because there is only one factory and all shops know the address, hence there we can add improvements as follow:

Change our CreateFurniture method in FurnitureFactory to a static method. Every client access the Factory in a static way and there is absolutely no problem here.

Important Note: In this scenario the workshop works as a factory for producing furniture and all the shops work as the clients to the factory. Please do not mistake it for factory method pattern. I repeat, this is NOT Factory Method Design Pattern. Its just happens that it looks like factory but we will learn about Factory Method Pattern in upcoming post.