The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as...
-
Upload
augustine-knight -
Category
Documents
-
view
212 -
download
0
Transcript of The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as...
![Page 1: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/1.jpg)
The Factory Method Design PatternThe Factory Method Design Pattern• Motivation: Class / Type separation
– Abstract class serves as type definition and concrete class provides implementation
– In a language which distinguishes between class and type, there must be a mechanism for creation of an object which would reveal its type, but not its class.
– Unfortunately, in Java, the best known language in which such a separation exists, there is no such mechanism. Abstract Methods deal exactly with this problem. The Abstract Factory Generalizes.
• Pattern Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate.
• Lets a class defer instantiation to subclasses
![Page 2: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/2.jpg)
Factory Method Motivation
Open()Close()Save()Revert()
Document*
CreateDocument()NewDocument()OpenDocument()
Application*
MyDocument
CreateDocument()
MyApplication
Document* doc = CreateDocument(); docs.Add(doc); doc ->Open();
return new MyDocument
docs
![Page 3: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/3.jpg)
Factory Method patternFactory Method patternMotivationMotivation
Document* doc = CreateDocument();docs.Add(doc);doc->Open();
ApplicationCreateDocument()NewDocument()OpenDocument()
DocumentOpen()Save()Close()Revert()
docs
MyDocumentMyApplicationCreateDocument()
createsreturn new MyDocument
![Page 4: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/4.jpg)
Factory Method Structure
FactoryMethod() AnOperation()
Creator*
FactoryMethod()
ConcreteCreator
...product = FactoryMethod()
...
return new ConcreteProduct
Product*
ConcreteProduct*
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
![Page 5: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/5.jpg)
Factory Method Consequences
createManipulator() ...
Figure*
createManipulator() ...
LineFigure
createManipulator() ...
TextFigure
Client
DownClick() Drag() Upclick()
Manipulator*
DownClick() Drag() Upclick()
TextManipulator
DownClick() Drag() Upclick()
LineManipulator
![Page 6: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/6.jpg)
Factory Method pattern Motivation (cont.)
• Application class is responsible for creation (and management) of Documents
• Problem:– Application class knows: WHEN a new document should be
created– Application class doesn’t know: WHAT KIND of document to
create• Solution:
– Application subclasses redefine abstract CreateDocument() method to return an appropriate Document subclass instance
![Page 7: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/7.jpg)
Connecting parallel hierarchiesConnecting parallel hierarchies
LineFigure
CreateManipulator()TextFigure
CreateManipulator()
LineManipulator
DownClick()Drag()
TextManipulator
DownClick()Drag()
Manipulator
DownClick()Drag()
ClientFigure
CreateManipulator()
creates
creates
Localizes knowledge of which classes belongs together
![Page 8: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/8.jpg)
Factory Method patternFactory Method patternStructureStructure
Product
ConcreteProduct
CreatorFactoryMethod()AnOperation()
ConcreteCreatorFactoryMethod()
creates
...product= FactoryMethod()...
return new ConcreteProduct
![Page 9: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/9.jpg)
Factory Method patternApplicability
• Use the Factory Method pattern when– a class can’t anticipate the class of objects it must create– a class wants its subclasses to specify the object it creates– classes delegate responsibility to one of several helper subclasses,
and you want to localize the knowledge of which helper subclass is the delegate
![Page 10: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/10.jpg)
Factory Method patternParticipants
• Product (Document)– defines the interface of objects the factory method creates
• ConcreteProduct (MyDocument)– implements the Product interface
• Creator (Application)– declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
– may call the factory method to create a Product object• ConcreteCreator (MyApplication)
– overrides the factory method to return an instance of a ConcreteProduct
![Page 11: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/11.jpg)
Factory Method - Consequences• Advantage– eliminates the need to bind application specific classes into
your code; your code deals with Product interface implemented by ConcreteProduct subclasses
• Potential disadvantage– clients might have to subclass the Creator class just to
create a particular (I.e., 1) ConcreteProduct object• Provides hooks for subclasses
– Factory Method gives subclasses a hook for providing an extended version of an object
• Connects parallel class hierarchies– see next slide for example
![Page 12: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/12.jpg)
Connecting parallel hierarchiesConnecting parallel hierarchies
LineFigureCreateManipulator()
TextFigureCreateManipulator()
LineManipulatorDownClick()Drag()
TextManipulatorDownClick()Drag()
ManipulatorDownClick()Drag()
ClientFigureCreateManipulator()
creates
creates
Localizes knowledge of which classes belongs together
![Page 13: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/13.jpg)
Factory Method - Implementation• Two major varieties
– Creator declares ABSTRACT factory method, ConcreteCreator implements it
– Creator defines a default implementation for factory method• Parameterized factory methods
– lets the factory method to create multiple kinds of objects– factory methods takes a parameter: a kind of object to create– all products have to share a Product interface
![Page 14: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/14.jpg)
Parameterized factory methodsclass Creator {public:
virtual Product* Create(ProductId);};Product* Creator::Create(ProductId id) {
if (id == MINE) return new MyProduct;if (id == YOURS) return new YourProduct;return 0;
}Product* MyCreator::Create(ProductId id) {
if (id == THEIRS) return new TheirProduct;return Creator::Create(id);
}
![Page 15: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/15.jpg)
Language-specific variantsC++
• “Lazy evaluation”class Creator {public:
Product* GetProduct();protected:
virtual Product* CreateProduct();private:
Product* _product;};Product* Creator::GetProduct() {
if (_product == 0)_product = CreateProduct();
return _product;}
![Page 16: The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f395503460f94c55ba3/html5/thumbnails/16.jpg)
Using templates to avoid subclassing
class Creator {public:
virtual Product* CreateProduct() = 0;};template <class TheProduct>class StandartCreator : public Creator {public:
virtual Product* CreateProduct();};template <class TheProduct>Product*StandartCreatot<TheProduct>::CreateProduct(){
return new TheProduct;}