1 / 16

The Factory Method Design Pattern

The Factory Method Design Pattern. Motivation: Class / Type separation Abstract class serves as type definition and concrete class provides implementation

lupita
Télécharger la présentation

The Factory Method Design Pattern

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

  2. Factory Method Motivation

  3. Factory Method patternMotivation Document Application docs Open() Document* doc = CreateDocument(); CreateDocument() docs.Add(doc); Save() NewDocument() doc->Open(); Close() OpenDocument() Revert() MyApplication MyDocument return new MyDocument CreateDocument() creates

  4. Factory Method Structure Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

  5. Factory Method Consequences

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

  7. Connecting parallel hierarchies Manipulator Figure Client DownClick() CreateManipulator() Drag() LineManipulator TextManipulator LineFigure TextFigure DownClick() DownClick() CreateManipulator() CreateManipulator() Drag() Drag() creates creates Localizes knowledge of which classes belongs together

  8. Factory Method patternStructure Creator ... product= FactoryMethod() FactoryMethod() Product ... AnOperation() ConcreteCreator ConcreteProduct return new ConcreteProduct FactoryMethod() creates

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

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

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

  12. Connecting parallel hierarchies Manipulator Figure Client DownClick() CreateManipulator() Drag() LineManipulator TextManipulator LineFigure TextFigure DownClick() DownClick() CreateManipulator() CreateManipulator() Drag() Drag() creates creates Localizes knowledge of which classes belongs together

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

  14. Parameterized factory methods class Creator { public: virtual Product* Create(ProductId); }; Product* Creator::Create(ProductId id) { if (id == MINE) returnnew MyProduct; if (id == YOURS) returnnew YourProduct; return 0; } Product* MyCreator::Create(ProductId id) { if (id == THEIRS) returnnew TheirProduct; return Creator::Create(id); }

  15. 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; }

  16. 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(){ returnnew TheProduct; }

More Related