Factory Method Design Pattern

1.  Classification
2.  Intent
3.  Motivation
4.  Structure
5.  Consequences
6.  Sample Code
Classification   top
Factory Method is a creational pattern. Also known as Virtual Constructor.
 
Intent  top
The intent of this design pattern is to define an interface for creating an object, but to let the subclasses decide which class to instantiate. It allows classes to defer instantiation to subclasses. It is called a Factory Method because it's responsible for "manufacturing" an object.
 
Motivation   top
One of the goals of object-oriented design is to distribute responsibilities between different objects. Sometimes, applications during their execution cannot anticipate the class of object it has to create. This application may know that it has to instantiate a class, but it may only know about abstract classes, which it cannot instantiate. An application may only know when it has to create a new object of a certain class, not what kind of subclass to create. Classes may want it's subclasses to specify the objects to be created. Classes may want to distribute the responsibility to one of several helper subclasses so that knowledge can be localized to specific helper subclasses. With Factory Method all methods are contained within the maker class and any variations an application may require are done by overriding the methods of the make class by methods in the help subclass. Creator relies on its subclasses
 
Structure   top
 
Consequences   top
There are four major consequences.
  1. Factory Method eliminates the need to bind application-specific classes into your code. The code only deals with the product interface; therefore it can work with any user-defined concrete product classes.
  2. Clients have to subclass the Creator class to create Concrete Product. That's a disadvantage of this pattern.
  3. Factory Method provides hooks for subclasses. It allows subclasses to provide an extended version of an object. Sometimes, the creator class is not abstract and provides a reasonable default implementation.
  4. Factory Method connects parallel class hierarchies. Parallel class hierarchies result when a class delegates some of its responsibilities to a separate class. Factory Method defines the connection between the two class hierarchies. It localizes knowledge of which classes belong together.
 
Sample Code   top
This sample code will show how Factory Method works. It is written in Java. In this example, we will create a Detroit Red Wings hockey jersey. To demonstrate how Factory Method works, we will have an abstract class called "jersey", that defines an abstract function "print". Along with that, we have an abstract creator called "jerseycreator", which has a method of creating a jersey. Both of these classes are abstract and define just a jersey. To create a concrete hockey jersey, like Red Wings hockey jersey, we will need a subclass called "redwingsjersey" which extends the "jersey" class. Class "redwingsjersey" is a concrete class, that contains methods that modify the methods defined in an abstract class "jersey". Just like with "jersey", we must have a creator for "redwingsjersey", we will call it "redwingsjerseycreator". This creator class extends "jerseycreator" class, and will create an actual Detroit Red Wings jersey. Finally, we have an application, that uses class jersey to create a specific jersey. This application will make "redwingsjersey" class to override the methods of "jersey" class in order to create the Red Wings jersey.

jersey.java

   public abstract class jersey
   {    
      public void print () {
          System.out.println("This is a jersey.");
        }
   } 

jerseycreator.java
public abstract class jerseycreator
{
    public abstract jersey createjersey();
}


redwingsjsersey.java
   public class redwingsjersey extends jersey
   {   
      public void print() {
         System.out.println("This is a Red Wings Jersey");
      }
   }


redwingsjerseycreator.java
public class redwingsjerseycreator extends jerseycreator
{
    public jersey createjersey() {
    redwingsjersey myjersey = new redwingsjersey();
    return myjersey;
    }
}


makejersey.java (Application)
   public class makejersey {
      public static void main(String[] args) {
         int i=0;
         System.out.println("1. Red Wings");
         System.out.println("2. exit");
         i=Keyboard.readInt();
         while (i!=2) {
            if (i==1) {
                    jerseycreator ctr=new redwingsjerseycreator();
                    jersey newjersey = ctr.createjersey();
                    newjersey.print();
                } else {
               System.out.println("Wrong Choice");
            }
            System.out.println("1. Red Wings");
            System.out.println("2. exit");
            i=Keyboard.readInt();
         }
         System.out.println("Goodbye");
      }
   }