cs 590l – distributed component architecture 02/20/2003uttara paingankar1 design patterns: factory...
TRANSCRIPT
02/20/2003 Uttara Paingankar 1
CS 590L – Distributed Component Architecture
Design Patterns: Factory Method
The factory method defines an interface for creating objects but lets subclasses decide
which classes instantiate.
02/20/2003 Uttara Paingankar 2
CS 590L – Distributed Component Architecture
Factory Method
• Factory Method is a creational pattern.
• Known as ‘Factory’ Method because it serves to create objects.
• Factory method is based on the concept of inheritance.
02/20/2003 Uttara Paingankar 3
CS 590L – Distributed Component Architecture
Factory Method – Applicable when…
• An application cannot anticipate at runtime, the class of object that it must create. It may know when to instantiate but not what to instantiate.
• A class might want its subclasses to specify the objects to be created.
• A class might delegate responsibility to one of the several helper subclasses so that knowledge can be localized to specific helper classes.
02/20/2003 Uttara Paingankar 4
CS 590L – Distributed Component Architecture
Factory Method – Participants
• Product – It defines the interface of objects the factory method creates.
• ConcreteProduct – Implements the Product interface.
• Creator – Declares the factory method, which returns an object of type Product. It may also define a default implementation of the factory method that returns a default ConcreteProduct object.
• ConcreteCreator – Overrides the factory method to return an instance of Concrete Product.
02/20/2003 Uttara Paingankar 5
CS 590L – Distributed Component Architecture
Factory Method – Structure
ConcereteProduct
Product
CreatorFactory Method()AnOperation()
ConcreteCreatorFactory Method()
product = FactoryMethod()
return new ConcreteProduct
02/20/2003 Uttara Paingankar 6
CS 590L – Distributed Component Architecture
Factory Method – Implementations
• The creator class can be an abstract method, not providing any implementation for the factory method it defines.
• The creator class can be a concrete class, providing some default implementation. These can be overridden by the subclass implementations.
• We can also have parameterized factory methods in which the factory method takes a parameter which identifies the kind of object to create.
02/20/2003 Uttara Paingankar 7
CS 590L – Distributed Component Architecture
Factory Method – Example
Product – Soup
abstract class Soup {
ArrayList soupIngredients = new
ArrayList(); String soupName; public String getSoupName() { return soupName; } public String toString() {…..}}
ConcreteProduct – Chicken Soup
class ChickenSoup extends Soup{ public ChickenSoup() { soupName = "ChickenSoup"; soupIngredients.add("1 Pound diced
chicken"); soupIngredients.add("1/2 cup rice"); soupIngredients.add("1 cup bullion"); soupIngredients.add("1/16 cup
butter"); soupIngredients.add("1/4 cup diced
carrots"); }}
02/20/2003 Uttara Paingankar 8
CS 590L – Distributed Component Architecture
Factory Method – Example cont’d
Creator – SoupFactoryMethod
class SoupFactoryMethod{ public SoupFactoryMethod() {}
public ChickenSoup makeChickenSoup(){// Returns a default Concrete Product
return new ChickenSoup();}
}
ConcreteCreator – BostonSoupFactoryMethod
class BostonSoupFactoryMethodSubclass extends SoupFactoryMethod
{ //Overrides the factory method
public ChickenSoup makeChickenSoup(){ return new BostonChickenSoup();}
}
02/20/2003 Uttara Paingankar 9
CS 590L – Distributed Component Architecture
Factory Method – Example cont’d
class BostonChickenSoup extends ChickenSoup{ public ChickenSoup () { soupName = "BostonChickenSoup"; soupIngredients.clear(); soupIngredients.add("1 Pound Fresh Chicken"); soupIngredients.add("1 cup corn"); soupIngredients.add("1/2 cup heavy cream"); soupIngredients.add("1/4 cup butter"); soupIngredients.add("1/4 cup carrots"); }}
02/20/2003 Uttara Paingankar 10
CS 590L – Distributed Component Architecture
Factory Method – Consequences
• Pros– Code can be made more flexible and reusable by the
elimination of instantiation of application-specific classes.
– It gives subclasses a hook for providing an extended version of an object.
• Cons– Clients might have to subclass the Creator just to
instantiate a particular Concrete-Product.– The code might become too tedious and complex to
understand if there are too many sub-classes.
02/20/2003 Uttara Paingankar 11
CS 590L – Distributed Component Architecture
Factory Method – References
• Erich Gamma, et.al, Design Patterns – Elements of Re-usable Object Oriented Software.
• Gopalan Suresh Raj, The Factory Method (Creational) Design Pattern.
http://gsraj.tripod.com/design/creational/factory/factory.html
• Best Practices: Seeing Design Patterns: The Factory Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfoxtk00/html/ft00a11.asp
02/20/2003 Uttara Paingankar 12
CS 590L – Distributed Component Architecture
Thank You!!