12/6/20041 the factory method pattern presenters 王世賀 f9006029 陳祐毓 f9006047 張峻銘...
TRANSCRIPT
12/6/2004 1
The Factory Method Pattern
Presenters王世賀 F9006029陳祐毓 F9006047張峻銘 F9006065吳佩達 F9006084林俊成 F9006097鄭榮智 F9006100許書豪 F9006205
12/6/2004 2
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
12/6/2004 3
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
Presenter 許書豪
12/6/2004 4
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/2004 6
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/2004 7
Motivation ( 4/4 ) Particular Document subclass to
instantiate is application-specific when and what kind questions to
the Application class
12/6/2004 8
Problem
The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate
12/6/2004 9
In Real World ( 1/2 ) A Bakery
0..1 0..*
0..1
0..*
Bakery
Johnson's Bakery
+ bake Bread () : void
bread
Baked by Johnson
A Factory Method
12/6/2004 10
In Real World ( 2/2 ) A Fruit Gardener
0..1
0..*
Fruit gardener
{abstract}
Apple gardener Strawberry gardener Grape gardener
Fruit
{abstract}
12/6/2004 11
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/2004 12
Solution ( 2/3 ) Product & Creator
Creator
{abstract} Product
{abstract}
ConcreteCreatorContreteProduct
12/6/2004 13
Solution ( 3/3 ) Example
0..10..*Document
{abstract}
Application
{abstract}
+++
CreateDocument ()NewDocument ()OpenDocument ()
: bool: bool: bool
DrawingApplication ImgzipApplication
The Factory Method
12/6/2004 14
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
Presenter 吳佩達
12/6/2004 15
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/2004 17
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/2004 18
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/2004 19
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
Presenter 張峻銘
12/6/2004 20
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/2004 21
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/2004 22
Structure
Class Diagram form “Design Patterns”
To instantiate a Creator-specific Product instance
12/6/2004 23
Collaborations
Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct
12/6/2004 24
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
Presenter 林俊成
12/6/2004 25
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/2004 26
Consequences ( 2/3 ) Two additional consequences of
the Factory Method pattern Provides hooks for subclasses Connects parallel class hierarchies
12/6/2004 28
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
Presenter 鄭榮智
12/6/2004 29
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/2004 30
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/2004 31
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/2004 32
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns
Presenter 王世賀
12/6/2004 33
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/2004 34
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/2004 36
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/2004 39
Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure
Collaborations Consequences Implementation Related
Patterns Reference
Presenter 陳祐毓
12/6/2004 40
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/2004 42
Reference
閻宏 , Java 與樣式理論 , 碁峰資訊股份有限公司 , ISBN 9864214179, 2009.9.26
結城 浩 , Design Patterns 於 Java 語言上的實習應用 , 博碩文化股份有限公司 , ISBN 9575274644, 2002.2.26