Download presentation
Presentation is loading. Please wait.
Published byJane Summers Modified over 9 years ago
1
12/6/20041 The Factory Method Pattern Presenters 王世賀 F9006029 陳祐毓 F9006047 張峻銘 F9006065 吳佩達 F9006084 林俊成 F9006097 鄭榮智 F9006100 許書豪 F9006205
2
12/6/20042 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference
3
12/6/20043 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 許書豪
4
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
5
12/6/20045 Motivation ( 2/4 )
6
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
7
12/6/20047 Motivation ( 4/4 ) Particular Document subclass to instantiate is application-specific when and what kind questions to the Application class
8
12/6/20048 Problem The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate
9
12/6/20049 In Real World ( 1/2 ) A Bakery
10
12/6/200410 In Real World ( 2/2 ) A Fruit Gardener
11
12/6/200411 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
12/6/200412 Solution ( 2/3 ) Product & Creator
13
12/6/200413 Solution ( 3/3 ) Example
14
12/6/200414 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 吳佩達
15
12/6/200415 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
16
12/6/200416 Example ( 1/2 ) Class Diagram in the book “ Design Patterns ” Factory Method
17
12/6/200417 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
18
12/6/200418 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 )
19
12/6/200419 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 張峻銘
20
12/6/200420 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 )
21
12/6/200421 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
22
12/6/200422 Structure Class Diagram form “ Design Patterns ” To instantiate a Creator-specific Product instance
23
12/6/200423 Collaborations Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct
24
12/6/200424 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 林俊成
25
12/6/200425 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.
26
12/6/200426 Consequences ( 2/3 ) Two additional consequences of the Factory Method pattern Provides hooks for subclasses Connects parallel class hierarchies
27
12/6/200427 Consequences ( 3/3 ) Connects parallel class hierarchies
28
12/6/200428 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 鄭榮智
29
12/6/200429 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
30
12/6/200430 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
31
12/6/200431 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; }
32
12/6/200432 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Presenter 王世賀
33
12/6/200433 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
34
12/6/200434 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
35
12/6/200435 Implementation ( 3.3/5 )
36
12/6/200436 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
37
12/6/200437 Implementation ( 4.2/5 )
38
12/6/200438 Implementation ( 4.3/5 )
39
12/6/200439 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 陳祐毓
40
12/6/200440 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 )
41
12/6/200441 Related Patterns Abstract Factory Factory method: other example
42
12/6/200442 Reference 閻宏, Java 與樣式理論, 碁峰資訊股份有 限公司, ISBN 9864214179, 2009.9.26 結城 浩, Design Patterns 於 Java 語言上的 實習應用, 博碩文化股份有限公司, ISBN 9575274644, 2002.2.26
43
12/6/200443 The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.