1 / 12

Design Patterns: Factory Method

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.

Download Presentation

Design Patterns: Factory Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns: Factory Method The factory method defines an interface for creating objects but lets subclasses decide which classes instantiate. Uttara Paingankar

  2. 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

  3. 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

  4. 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

  5. Factory Method – Structure Creator Factory Method() AnOperation() product = FactoryMethod() Product ConcereteProduct ConcreteCreator Factory Method() return new ConcreteProduct Uttara Paingankar

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. Thank You!! Uttara Paingankar

More Related