CSC/ECE 517 Fall 2009/wiki3 16 AD: Difference between revisions
(36 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
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. | 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 == | == Overview == | ||
Line 14: | Line 12: | ||
=== Factory Patterns Insight === | === Factory Patterns Insight === | ||
The essence of a pattern is a reusable solution for a recurring problem.The type of object needed depends on the contents of the external data or event. It defines 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. | |||
[[Image: | [[Image:facuml.jpg]] | ||
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 === | === Applicants of Factory Pattern === | ||
=== | Use the Factory Method pattern in any of the following situations: | ||
<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>Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclasses is the delegate. | |||
Sometimes, you need to create an object to represent external data or process an external event. The type of object needed depends on the contents of the external data or event. You don’t want the source of the data or event nor object’s clients to need to be aware of the actual type of object created. You accomplish that by encapsulating the decision of the class of object to create in its own class.This is when factory method design can be put in to to best use. | |||
=== Application 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 other Creational Patterns == | ||
Line 37: | Line 93: | ||
[[Image:Builder2.png]] | [[Image:Builder2.png]] | ||
=== Comparison with Prototype 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. | |||
[[Image:prototype.jpg]] | [[Image:prototype.jpg]] | ||
=== Comparison with Abstract Factory Pattern === | === 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. | |||
[[Image:abstractfactory.gif]] | |||
== External Links and References == | == External Links and References == | ||
<ol> | |||
<li> Factory Design Pattern [http://en.wikipedia.org/wiki/Factory_method_pattern] </li> | |||
<li> Abstract Factory Method[http://www.apwebco.com/gofpatterns/creational/AbstractFactory.html] </li> | |||
<li> Factory v/s Abstract Factory Pattern[http://www.coderanch.com/t/99780/OO-Patterns-UML-Refactoring/Factory-Vs-Abstract-Factory-Pattern] </li> | |||
<li> Builder Design Pattern v/s Factory Design Pattern[http://stackoverflow.com/questions/757743/what-is-the-difference-between-builder-design-pattern-and-factory-design-pattern]</li> | |||
<li> Abstract Factory Method [http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf]</li> | |||
<li> UML Diagram of Builder Pattern [http://en.wikipedia.org/wiki/Builder_pattern]</li> | |||
<li> UML Diagram of Prototype Design Pattern [http://www.apwebco.com/gofpatterns/creational/Prototype.html] </li> | |||
<li> UML Diagram of Abstract Factory Design Pattern [http://www.apwebco.com/gofpatterns/creational/AbstractFactory.html] </li> | |||
<li> Factory Design Pattern Usage [http://www.developer.com/java/other/article.php/618421/Pattern-Summaries-Factory-Method.htm] </li> | |||
<li> Examples of Factory Design Pattern [http://www.hiteshagrawal.com/java/factory-design-pattern-in-java] </li> |
Latest revision as of 23:50, 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
The essence of a pattern is a reusable solution for a recurring problem.The type of object needed depends on the contents of the external data or event. It defines 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.
Sometimes, you need to create an object to represent external data or process an external event. The type of object needed depends on the contents of the external data or event. You don’t want the source of the data or event nor object’s clients to need to be aware of the actual type of object created. You accomplish that by encapsulating the decision of the class of object to create in its own class.This is when factory method design can be put in to to best use.
Application 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
- Factory Design Pattern [1]
- Abstract Factory Method[2]
- Factory v/s Abstract Factory Pattern[3]
- Builder Design Pattern v/s Factory Design Pattern[4]
- Abstract Factory Method [5]
- UML Diagram of Builder Pattern [6]
- UML Diagram of Prototype Design Pattern [7]
- UML Diagram of Abstract Factory Design Pattern [8]
- Factory Design Pattern Usage [9]
- Examples of Factory Design Pattern [10]