230 likes | 477 Views
Design Patterns: Factory Method. The factory method defines an interface for creating objects but lets subclasses decide which classes instantiate. Factory Method. Factory Method is a creational pattern. Known as ‘Factory’ Method because it serves to create objects.
E N D
Design Patterns: Factory Method The factory method defines an interface for creating objects but lets subclasses decide which classes instantiate. Uttara Paingankar
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. Uttara Paingankar
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. Uttara Paingankar
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. Uttara Paingankar
Factory Method – Structure Creator Factory Method() AnOperation() product = FactoryMethod() Product ConcereteProduct ConcreteCreator Factory Method() return new ConcreteProduct Uttara Paingankar
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. Uttara Paingankar
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"); } } Factory Method – Example Uttara Paingankar
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(); } } Factory Method – Example cont’d Uttara Paingankar
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"); } } Uttara Paingankar
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. Uttara Paingankar
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 Uttara Paingankar
Thank You!! Uttara Paingankar