Factory Method Pattern

Factory Method Pattern

Factory Method(virtual constructor) provides an interface for creating objects in a super-class, but defers instantiation to sub-classes.

Factory Method(virtual constructor) provides an interface for creating objects in a super-class, but defers instantiation to sub-classes.

Motivation

Consider an application which has to access a class, but does not know which class to choose form among a set of sub-classes of the parent class. The application class can’t predict the sub-class to instantiate as the choice might depend on numerous factors, such as the state of the application at run-time or its configuration.

The Factory method pattern solves this problem by encapsulating the functionality of selecting and instantiating the appropriate class inside a designated method, which we refer to as the factory method.

Applicability

The factory method pattern can be used in cases where:

  • A class can not know or predict the object it must create
  • A class wants to transfer the instantiation of objects to its sub-classes

Structure

Factory method class diagram

Participants

  • Product: Interface of objects created by the factory method
  • ConcreteProduct: Implementation of the Product interface
  • Creator: declares the factory method that returns a Product instance.
  • ConcreteCreator: Overrides factory method and returns ConcreteProduct instance

Collaborations

In case the Creator does not have a default implementation of the factory method, it relies on its sub-classes to define the factory method.

Advantages

  • Provides hook for sub-classes, enabling sub-classes to affect their parents’ behavior.
  • Dynamic or concrete types are isolated form the client code, hence promotes loose-coupling

Disadvantages

  • To create even one ConcreteProduct object the creator class must be sub-classed. This might overburden the clients.

Implementation

The implementation of the factory method could have in general three variations:

  1. Creator class is an abstract class and does not have a default implementation for the factory method
  2. Creator class has a default implementation for the factory class, sub-classes override the default implementation if necessary
  3. factory methods are parameterized and can create multiple kinds of products corresponding to the value passed.

Demo

Let us look at how we could apply the Factory Method Pattern to the cafe discussed in the introduction article of the series.

The cafe is growing popular and the owners decided to open a new branch in France. But there is a problem, the menu is in English but the customers expect it to be in French. There is also a confusion in the currency. In France, the currency is Euro but the system displays price in US Dollars.

We can solve this problem by first creating an abstract Menu class, and then creating concrete classes that extend this abstract class to make a french and other language versions of the menu. We then create a GetMenuFactory class to generate object of the different language versions based on the information given.