Factory design Pattern

Table of contents
Reading Time: 2 minutes

Hi guys, I hope you are doing good. In this blog, we will be talking about one of the creational design patterns which is Factory Pattern. The creational design patterns are concerned with the way in which objects are created. In factory design patterns, we basically define an interface or abstract class for creating an object but objects creation decisions like for which class client wants to instantiate, would be handled by subclasses. This design pattern is used by all the wrapper classes like Integer, Boolean in the valueOf() method.

   All design patterns are not inherently language specific, although there can be scenarios that one design pattern is more useful in some languages than others.

There are some advantages which we would be getting from Factory design pattern – 

  • By implementing a factory design pattern we can eliminate the tight coupling from the code by removing the need to bind the application specific classes into the code.
  • The factory design pattern allows sub-classes to choose the type of objects to create.

Let’s see one real world example where we would be implementing the factory design pattern –

  • Create a abstract class Car.
  • Extend the abstract class Car and provide override the abstract method setColor().
  • Create factory class to generate the objects of the child classes based on the input type.
  • Call the Factory class method to generate the objects of child classes based on the input information.

That’s pretty much it from the article. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.


Written by 

Lokesh Aggarwal is a software Consultant trainee with 6 months of experience at Knoldus Inc.