12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
12/6/20042 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference
12/6/20043 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 許書豪
12/6/20044 Motivation ( 1/4 ) Useful to Frameworks for applications Consider a framework for applications that can present multiple documents Two key abstractions Application managing Documents : create Open or New from a menu Document application-specific implementations To subclass and to implement
12/6/20045 Motivation ( 2/4 )
12/6/20046 Motivation ( 3/4 ) A drawing application Class DrawingApplication extends Application Class DrawingDocument extends Document A imgzip application Class ImgzipApplication extends Application Class ImgzipDocument extends Document particular Document subclass
12/6/20047 Motivation ( 4/4 ) Particular Document subclass to instantiate is application-specific when and what kind questions to the Application class
12/6/20048 Problem The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate
12/6/20049 In Real World ( 1/2 ) A Bakery
12/6/ In Real World ( 2/2 ) A Fruit Gardener
12/6/ Solution ( 1/3 ) The Factory Method pattern encapsulates the knowledge of which Document subclass to create and moves this knowledge out of the framework Lets a class ( abstractions ) defer instantiation to subclasses
12/6/ Solution ( 2/3 ) Product & Creator
12/6/ Solution ( 3/3 ) Example
12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 吳佩達
12/6/ Intent Intent by the book “ Design Patterns ” Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Also known as Virtual Constructor
12/6/ Example ( 1/2 ) Class Diagram in the book “ Design Patterns ” Factory Method
12/6/ Example ( 2/2 ) Application subclasses redefine an abstract CreateDocument operation on Application to return the appropriate Document subclass We call CreateDocument a factory method because it's responsible for "manufacturing" an application-specific object
12/6/ Applicability 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 objects 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 ( Composite )
12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 張峻銘
12/6/ Participants ( 1/2 ) Abstract Creator 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 ( see Consequences 1 )
12/6/ Participants ( 2/2 ) Abstract Product defines the interface of objects the factory method creates ConcreteCreator overrides the factory method to return an instance of a ConcreteProduct ConcreteProduct implements the Product interface
12/6/ Structure Class Diagram form “ Design Patterns ” To instantiate a Creator-specific Product instance
12/6/ Collaborations Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct
12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 林俊成
12/6/ Consequences ( 1/3 ) Factory methods eliminate the need to bind application-specific classes into your code. A potential disadvantage of factory methods is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object.
12/6/ Consequences ( 2/3 ) Two additional consequences of the Factory Method pattern Provides hooks for subclasses Connects parallel class hierarchies
12/6/ Consequences ( 3/3 ) Connects parallel class hierarchies
12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 鄭榮智
12/6/ Implementation ( 1/5 ) Two major varieties Creator class is an abstract class and does not provide an implementation for the factory method it declares Creator class is a concrete class and provides a default implementation for the factory method
12/6/ Implementation ( 2.1/5 ) Parameterized factory methods Another variation lets the factory method create multiple kinds of Products Takes a parameter that identifies the kind of object to create All objects the factory method creates will share the Product interface Application might support different kinds of Documents
12/6/ Implementation ( 2.2/5 ) class Creator { public: virtual Product* Create( ProductId ); }; Product* Creator::Create ( ProductId id ) { if ( id == MINE ) return new MyProduct; if ( id == YOURS ) return new YourProduct; // repeat for remaining products... return 0; }
12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Presenter 王世賀
12/6/ Implementation ( 3.1/5 ) Language-specific variants and issues Factory methods in C++ are always virtual functions and are often pure virtual. Just be careful not to call factory methods in the Creator's constructor — the factory method in the ConcreteCreator won't be available yet
12/6/ Implementation ( 3.2/5 ) Lazy initialization Instead of creating the concrete product in the constructor, the constructor merely initializes it to 0. The accessor returns the product. But first it checks to make sure the product exists, and if it doesn't, the accessor creates it
12/6/ Implementation ( 3.3/5 )
12/6/ Implementation ( 4.1/5 ) Using templates to avoid subclassing Another potential problem with factory methods is that they might force you to subclass just to create the appropriate Product objects
12/6/ Implementation ( 4.2/5 )
12/6/ Implementation ( 4.3/5 )
12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 陳祐毓
12/6/ Implementation ( 5/5 ) Naming conventions make it clear you're using factory methods For example, the MacApp Macintosh application framework ( declares the abstract operation that defines the factory method as Class* DoMakeClass(), where Class is the Product class )
12/6/ Related Patterns Abstract Factory Factory method: other example
12/6/ Reference 閻宏, Java 與樣式理論, 碁峰資訊股份有 限公司, ISBN , 結城 浩, Design Patterns 於 Java 語言上的 實習應用, 博碩文化股份有限公司, ISBN ,
12/6/ The End