SlideServe
Menu
Préférences
© SlideServe · v2026

Design Patterns: Factory Method

kermit-slater avatar
12 diapositives · 516 Views · Oct 29, 2014

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.

Design Patterns: Factory Method - slide 1
01 / 12Design Patterns: Factory Method
1 / 17

Télécharger la présentation

Design Patterns: Factory Method
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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