GofPatterns Gofpatterns 

Creational Patterns  «Prev  Next»

Factory Method Pattern

In a factory pattern, a concrete class with static methods is used to create instances of objects that implement an interface.
The Factory Method pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate.
The Factory method lets a class defer instantiation to subclasses, which is useful for constructing individual objects for a specific purpose without the requestor knowing the specific class being instantiated. This allows you to introduce new classes without modifying the code because the new class implements only the interface so it can be used by the client. You create a new factory class to create the new class and the factory class implements the factory interface.
Factory Method
  1. Factory Pattern
  2. Factory Method Pattern Java Code

The following lists the benefits of using the Factory Method pattern.
  1. Eliminates the need to bind application classes into your code. The code deals only with the interface, so you can work with any classes that implement that interface.
  2. Enables the subclasses to provide an extended version of an object, because creating an object inside a class is more flexible than creating the object directly in the client.
When To Use:
You should use the Factory method pattern when:
  1. A class cannot anticipate the class of objects it must create.
  2. A class wants its subclasses to specify the objects it creates.
  3. Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
The factory method pattern is an object-oriented creational design pattern to implement the concept of factories and deals with the problem of creating objects (products) without specifying the exact class of object that will be created.
The essence of this pattern is to
Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.

Creating an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object's concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.
Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object. Outside the scope of design patterns, the term factory method can also refer to a method of a factory whose main purpose is creation of objects. The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects.
The Factory Method is related to the idea on which libraries work because library uses abstract classes for defining and maintaining relations between objects. One type of responsibility is creating such objects. The library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library.
The Factory method works the same way because it defines an interface for creating an object, but leaves the choice of its type to the subclasses. A simple real life example of the Factory Method is the hotel. When staying in a hotel you first have to check in. The person working at the front desk will give you a key to your room after you have paid for the room you want.
This room can be conceived as a room factory. While staying at the hotel, you might need to make a phone call, so you call the front desk and the person there will connect you with the number you need, becoming a phone-call factory, because he controls the access to calls.