Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.

Similar presentations


Presentation on theme: "CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating."— Presentation transcript:

1 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating objects but lets subclasses decide which classes instantiate.

2 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar2 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.

3 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar3 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.

4 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar4 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.

5 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar5 Factory Method – Structure ConcereteProduct Product Creator Factory Method() AnOperation() ConcreteCreator Factory Method() product = FactoryMethod() return new ConcreteProduct

6 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar6 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.

7 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar7 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"); }

8 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar8 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(); }

9 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar9 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"); }

10 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar10 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.

11 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar11 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

12 CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar12 Thank You!!


Download ppt "CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating."

Similar presentations


Ads by Google