Extending our story of Jay the Carpenter, O! you missed the beginning ? Check my previous post.
Last we heard from Jay, he was selling furniture happily and was able to define a strict process for franchises to follow and he also gave them freedom to make furniture as they like.
He basically controlled the post making process, where the customers would get a well painted, polished and packed furniture.
Of course the franchises were not very happy as they were not able to churn out more money after this was outlined. They thought of doing other nasty things. They figured out their own ways to earn more money, they started using cheaper quality of materials and hence compromising on the quality of furniture.
- The Delhi business owner asked his setup to use wood from older furniture for making newer ones.
- The Mumbai one started using cheaper glue.
- So did the others..
Somehow, Jay got to know about this and now he was more worried because this was a bigger concern compared to the first one where they were just not polishing or packaging the furniture. This time its a question of quality of material and if this is not checked he might face a big blow on his business.
So, he hired a consultant to understand how he can put a check on the franchises to use the prescribed raw material only. After a lot of thoughts and refinement, they came up with a great plan.
Jay decided to ship the required material to the franchises, instead of they procuring it themselves from some local vendor. This was a good idea and would definitely put a check on the malpractices. Hence he defined a more refined framework, which suggests the following:
- Set up a factory which produces a set of raw materials which is sufficient to make one furniture.
- Now there could be a possibility that people from different cities like furniture made of different raw material.
- For e.g.: In Delhi they get Teak Wood and that is the trend there, In Mumbai, they get Mahogany and so on.
- Similarly, people in Chennai love copper nails whereas people in Kolkata like Iron nails.
And satisfying people at different locations with their choices meant creating different factories where each factory creates one set of raw material.
So, now Jay’s framework has one Abstract Material Factory which is composed of lot of factory methods such that all these methods together produce a family of raw materials. He manages shipping material from specific factory to the location specific workshops where the franchises have their labor force which makes furniture using the materials from Jay’s location specific factories.
Here is how Jay’s new framework look like. Please do not treat this as a class diagram, I am just showing the framework here.
Here Jay has Material Factories which will produce materials based on location and ship it to the respective cities so that the franchises do not have scope to use cheap materials.
This is the abstract factory which is used to create families of the raw materials. Each of the method in the abstract factory is a factory method itself. The material factory for Delhi creates the family of raw material for Delhi and the material factory for Mumbai creates the family of raw materials for Mumbai.
So, how do we now plugin all together, how do we hook in the furniture creation and the client code (which is the shop) to actually make the whole machinery work?
The rest of the piece is adapted from the Factory Method Pattern where we had an AbstractProducer which was implemented by the respective Franchises. Here is another image for the same.
And now the complete system in one diagram. The shop (clients) which we claimed to be loosely coupled. The Factory Method and the Abstract Factory Pattern. See how they all fit in the bigger picture.
- The furniture shop is a client say the shop in Delhi, which knows about the workshop in Delhi.
- The workshop knows about the material factory which can be used for the furniture delivered in Delhi
- Hence, the workshop obtains the material from the Delhi factory and creates the required furniture from the material
Check the complete code in github.
Highlights of Abstract Factory Pattern
The abstract factory pattern creates objects using composition. It provides an abstract type to create families of products. In our case the Abstract Factory provided the factory methods to create families of object. For e.g.: The methods getGlue(), getNail() and getWood() are factory methods and they help create multiple families of products and each family contains concrete instances of a glue, a nail and a wood.
So, we see that the factory is based on composition of objects. It gives us much more flexibility as we can create a family of products and not just one product as it was the case with the Factory method pattern.
Similar to the Factory Method, the client code here is also independent of the concrete objects. It only depends on their abstract definition.
Limitations of Abstract Factory Method
The abstract factory method interface will change whenever we plan to add one more product in the family. This is a rare scenario but still this is a limitation. But consider how many times do we need a new type of raw material for an already established process. So, we can live with it.
As it happens with all design patterns, they add an extra layer of abstraction. Which means many extra classes, which you might have already noticed till now. So, in practical scenarios you might need to add a lot of POJOs just to get the abstract factory pattern working.
Abstract Factory Pattern of course provides a better level of abstraction and granular flexibility. But a piece of advice would be to use it when absolutely necessary and you really need a family of objects to be created. Also note that most of the times, we only need Factory Method Pattern to create objects.
We need to definitely talk about the static factory methods which is just a programming idiom and does not comply to the Factory Method Pattern or Abstract Factory Pattern in the GoF. Let us do it in the next post.
Let me know in comments if you couldn’t understand few parts of the post. This was a real life imaginary scenario but if you think that you want an answer for your particular case, just add a comment and I will explain how to use these particular pattern for your use case.
Stay connected and stay subscribed.