Download presentation
Presentation is loading. Please wait.
Published byAngel Parks Modified over 9 years ago
1
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
2
Factory Method: Defined Define an interface for creating an object, but let the subclasses decide which class to instantiate. (Factory Method lets a class defer instantiation to subclasses) (Gamma et al. 107)
3
Factory Method: Defined Creational
4
Factory Method: Motivated Document Application CreateDocument() NewDocument() Open Document() Document* doc=CreateDocument(); docs.Add(doc); doc->Open(); Return new MyDocument MyDocument MyApplication CreateDocument() Open() Close() Save()
5
Factory Method: Motivated
6
Factory Method: Applied When should we use Factory Method? When a class: Can’t predict the class of the objects it needs to create Wants its subclasses to specify the objects that it creates Delegates responsibility to one of multiple helper subclasses, and you need to localize the knowledge of which helper is the delagte
7
Factory Method: The Consequences Advantage: Elminates the need to bind application- specific classes to your code. Code deals only with the Product interface (Document), so it can work with any user-defined ConcreteProduct (MyDocument)
8
Factory Method: The Consequences Potential Disadvantages Clients may have to make a subclass of the Creator, just so they can create a certain ConcreteProduct. This would be acceptable if the client has to subclass the Creator anyway, but if not then the client has to deal with another point of evolution.
9
Factory Method: The Consequences In addition: “Provides hooks for subclasses” “Connects parallel class hierarchies” P.109-110
10
Factory Method: Implemented Two major varieties Parameterized factory methods Language-specific issues Using templates to avoid subclassing Naming conventions
11
Factory Method: Known Uses Frameworks Toolkits
12
Factory Method: Related Patterns Abstract Factory is often implemented with Factory Method. Usually called in Template Methods Prototypes don’t subclass the Creator, but often need to initialize the Product class. Creator would use Initialize on the object, but Factory Method doesn’t need this operation.
13
Sources Gammal et al. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. 1995. Data and Object Factory. Image: Factory.gif Internet. Available by HTTP: http://www.dofactory.com/patterns/PatternFactory.aspx http://www.dofactory.com/patterns/PatternFactory.aspx
14
Factory Method: Notes Text Books pages 107-116 Slides available at www.rpi.edu/~colasc
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.