Factory Pattern

In this article we are going to understand Factory pattern. As name of this pattern is Factory, you can very well relate it to real world manufacturing factories where items like Mobile phones, Television sets, Computers are constructed. Whenever we come across any thing new, our mind always have questions like what’s the purpose of using it? What are concept and its implementation? , What are benefits of using it? You will get answers to these questions as you read a further.

Motivation


In this section, we will see what is motivation behind using this pattern. Let consider an example of mobile applications, which we are installing on our mobile phones. Before concept of Play Store and App Store, every mobile application company used to create application and put it on their website from where user will download it and install it on their mobile phones. Let consider 2 applications development companies build their apps i.e. FooMail and FooBook, also mobile client Mobile as shown below.

If you have observed, Mobile client class needs to have knowledge of all application classes which client wants to install. In our case FooMail and FooBook. Hence client class will be always dependent on application classes. Here both client class and application classes are tightly coupled.This section must have answered, “What’s the purpose of using it?” question.

Concept


Factory is pattern is one of the Creational patterns. It’s used to construct objects such that they can be decoupled from the implementing system.

The definition of Factory Pattern given by Gang of Four book

Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.

 

Implementation


We will continue with same example. Now lets introduce a common interface AndroidApplication  that will be overridden by all applications.

FooBook and FooMail applications are implementing AndroidApplication interface

Now lets introduce a factory class AndroidApplicationFactory, which has a factory method getApplication, which will return AndroidApplication based on application requested by client.

Now lets see how the client class will use this factory class

If you observed above implementation using Factory, client doesn’t require having knowledge of all application classes and it relies on abstraction i.e. AndroidApplication interface. Now client class is independent of application classes. Here both client class and application classes are loosely coupled.This section must have answered, “What are concept and its implementation?” question.

Benefits


  • It hides the implementation of class from client.
  • It allows the sub-classes to choose the type of objects to create.
  • It promotes the loose coupling by eliminating the need to bind application-specific classes into the code.
  • It manages creational part in lifecycle.

Pitfalls


  • Makes code more difficult to read as all of your code is behind an abstraction that may in turn hide abstractions.
  • It can be classed as an anti-pattern when it is incorrectly used.

Few classes in JDK which uses this pattern


Happy learning !

Leave a Reply

Your email address will not be published. Required fields are marked *