Factory Method Pattern

It is most likely that you read my previous post on the Misconceptions . If you missed that, then I would advice you to please read that, it is a small post.

So, in this post we will learn about few things:

  1. What exactly is Factory Method Pattern ?
  2. What purpose does it solve?
  3. Where in JDK is Factory Method Design Pattern used?
  4. Key points of Factory Method Design Pattern.

What exactly is Factory Method Pattern

Continuing our story from the previous post, where Jay is now happy with his shops and the simple factory setup. As a business grows people see it and try to earn profit from the same.

Small aspiring businessmen from different cities approached Jay for franchising his furniture making business. They asked him for using his name and giving him a share of the profit as it happens in all the franchises.

Jay happily agreed to it and now new furniture shops started in the cities (Delhi, Bombay, Chennai & Kolkata). Every business owner has literally replicated Jay’s setup and they are making furniture as they want and selling it using Jay’s name. In return Jay is getting his share of the profit.

After couple of months Jay realized that the businesses are actually taking advantage of him by employing the below practices:

  1. Some of them are not packaging the furniture.
  2. Few do not even polish it.
  3. One forgot painting and so on.

Jay got concerned and thought that this might ruin his name and that would incur a loss to him. Hence, he plans to implement a strict and stringent process.

Here are the steps taken by Jay:

  1. He said that he will define an abstract framework where he will define all the process and everyone has to comply to it.
  2. The only freedom the franchise will have is to employ their labors and make the furniture.
  3. He will create one example of the abstract framework and show it to all business owners to setup it in their own cities.

Eventually everyone agreed to this proposal because Jay owns the business and they will have to comply. Hence, Jay started setting up one implementation of the Abstract Framework, which he called the AbstractProducer.

The framework outlined all the steps as Jay wanted in such a way that there is no scope of altering any of the step and it combined all these steps as one and named it orderFurniture. But as Jay promised that the franchises will have the liberty of employing their labor force and make their own furniture so the framework allowed that and hence, the createFurniture step inside the orderFuniture was left abstract (unimplemented).

Jay ordered his manager to implement the createFurniture process in his workshop and use the rest of the setup from the orderFurniture step as it is already implemented there.

Seeing this default implementation, all the franchises had setup similar workshops now. Everyone just implemented the createFurniture step as they wanted because the rest is almost fixed now.

This is called Factory Method Pattern, where the createFurniture is the factory method.`

And here is Jay’s default implementation

Here is Jay’s shop using the default implementation to deliver the furniture to his original customers

Get the complete code on github

Note: It is absolutely possible that all the implementations do not use the same concrete Furniture classes. In which case they can have their own Furniture implementations. For e.g: Let us say that the Delhi based shop wants to specifically make and sell fancy chairs instead of the typical chair which Jay sells. They can implement a FancyChair class and use it in their factory implementation.

What purpose does Factory Method Design Pattern Solve

If we consider our simple factory in the previous post, it used to create objects and it was kind of fixed. Here, we have a flexibility of adding a different implementation and the implementation is free to create products the way it wants.

It solves the problem where we have  a template for product creation but the producer has no idea about the concrete object being created and it mostly works with the abstract product. Only the concrete implementations of the producer knows how the product will be created. This provides a better loosely coupled code.

The Factory Method Design Pattern is about creating products, and as we read above that the AbstractProducer depends on abstract products which means that all products must be derived from an abstract product. In our case a Furniture interface.

Hence, there will be a hierarchy of products inheriting/implementing the super class / interface Furniture. Also, there will be a hierarchy of producers implementing the AbstractProducer and they can freely implement the createFurniture method.

Where in JDK is Factory Method Design Pattern used?

java.net.URLStreamHandlerFactory  

This interface defines a factory for URL stream protocol handlers. It is used by the URL class to create a URLStreamHandler for a specific protocol. The factory method in this class is createURLStreamHandler( String protocol)

java.text.NumberFormat

This is the abstract class for all number formats. It provides the interface for formatting and parsing numbers. It also provides methods for  determining which locales have number formats, and what their names are. Below are the factory methods:

  • format(double number, StringBuffer toAppendTo, FieldPosition pos)
  • format(double number, StringBuffer toAppendTo, FieldPosition pos)

There are many other usages, you can find this in the Stackoverflow answer. In this answer you may be a mention of static factory method, which again does not comply to the Factory Method Design pattern.

Key points of Factory Method Design Pattern

  • The key feature of the Factory Method Design Pattern is that it is implemented using inheritance.
  • Object creation is deferred to the sub classes which enables the sub classes to define the creation procedure for a concrete object.
  • All code inside the AbstractProducer depends on an abstract product because it doesn’t know what will be produced.
  • Sometimes the factory method does not accept parameter and each implementation just create one object.
  • It is a loosely coupled design because the client only depends on the interfaces and abstract classes and the concrete object creation is encapsulated in the inherited sub classes of the producer.

 

Whats next?

Ah! so now we know the real factory method design pattern. I will also like to explain about the static factory idiom which is not exactly a design pattern but it is used all over JDK.

Understanding that is important because we must be able to clearly distinguish between all these terms which sounds similar. From an interview perspective, we must be able to answer it correctly. We will also learn about the Abstract Factory Pattern, which uses composition instead of Inheritance for object creation.

 

Stay Connected and Stay Subscribed.