CSC/ECE 517 Fall 2009/wiki3 16 AD: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 25: Line 25:
<br>A class can't anticipate the class of objects it must create
<br>A class can't anticipate the class of objects it must create
<br>A class wants its subclasses to specify the objects it creates
<br>A class wants its subclasses to specify the objects it creates
<br>Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclasses is the delegate.


=== Examples of Factory Pattern ===
=== Examples of Factory Pattern ===

Revision as of 23:20, 18 November 2009

Definition

Factory Method is a creational pattern. This pattern helps to model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate.It is called as Factory Pattern as it is responsible for "Manufacturing" an Object. It helps instantiate the appropriate Subclass by creating the right Object from a group of related classes. The Factory Pattern encourages loose coupling by eliminating the need to bind application-specific classes into the code.It Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Overview

This topic deals with the Factory method design pattern.It explains Factory design pattern and compares with other creational pattern to highlight the uniqueness and distinguishing features of the pattern.It also gives examples of where factory design pattern is used and scenarios wherein its more suitable than other creational patterns.


In Depth Analysis

Factory Patterns Insight

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.It means that Creator class is written without knowing what actual ConcreteProduct class will be instantiated. The ConcreteProduct class which is instantiated is determined solely by which ConcreteCreator subclass is instantiated and used by the application.

Benefits Code is made more flexible and reusable by the elimination of instantiation of application-specific classes,Code deals only with the interface of the Factory class and can work with any sub class that supports this interface

Applicants of Factory Pattern

Use the Factory Method pattern in any of the following situations:


A class can't anticipate the class of objects it must create
A class wants its subclasses to specify the objects it creates
Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclasses is the delegate.

Examples of Factory Pattern

Factory design pattern can be explained by the following example. This example involves factory class called Mammals and it involves calling objects cat and dog with out actually creating an object for cat and dog class.


public class MammalsFactory {
   public static Mammals getMammalObject(String name) {
      if (name.equalsIgnoreCase("Cat")){
         return new Cat();
      } else {
         return new Dog();
      }
   }
}

Here we will be passing an argument to the getMammalObject function based on the argument passed we return the Mammals object of the class.

Abstract class is created so that dog ans cat can implement this class

public abstract class Mammals {
      public abstract String doWalking();
}

The cat and dog class can be created to implement the abstract class as follows

public class Cat extends Mammals {
public String doWalking() {
return "Cats has been Informed to perform Walk Operation";
}
}

Dog class

public class Dog extends Mammals {
public String doWalking() {
return "Dogs has been Informed to perform Walk Operation";
}
}

Now without creating object for dog and cat but we are instantiating the object for Mammals Factory class.

public class FactoryClient {
  public static void main(String args[]) {
      MammalsFactory mf = new MammalsFactory();
      System.out.println(mf.getMammalObject("Dog").doWalking());
  }
}

Comparison with other Creational Patterns

Comparison with Builder Pattern

In Factory pattern, the factory is in charge of creating various subtypes of an object depending on the needs.

Where as in Builder pattern, the composition of the objects might differ within the same subclass.

An example of a Factory method CarCreate might return a Audi or a Toyota where as in the same scenario if we are using a Builder pattern, it allows us to create objects with finer granularity like specifying different engine specifications (a 4 cylinder engine or a 6 cylinder engine)for the objects. As seen from the above example the Factory pattern is a simpler version of the Builder Pattern.

UML diagram of Builder Pattern


Comparison with Prototype Pattern

The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it is dealing with as long as the concrete prototype extends or implements the prototype interface/class. The concrete prototype object is responsible for cloning itself and returning the cloned object. The pattern enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at runtime as long as they conform to the abstract prototype.

Comparison with Abstract Factory Pattern

Abstract pattern is one level of abstraction higher than the factory pattern. A Factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of data.

There are several similar variations on the factory pattern:

 1. The base class is abstract and the pattern must return a complete working class. 
 2. The base class contains default methods and is only sub-classed for cases where the default methods are insufficient.
 3. Parameters are passed to the factory telling it which of several class types to return. In this case the classes may share
    the same method names but may do something quite different.

External Links and References

  1. Abstract Factory Method[1]
  2. Factory v/s Abstract Factory Pattern[2]
  3. Builder Design Pattern v/s Factory Design Pattern[3]
  4. Abstract Factory Method [4]
  5. UML Diagram of Builder Pattern [5]