Upload
sophie-skinner
View
213
Download
0
Embed Size (px)
Citation preview
1© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Generator Design Generator Design Patterns: The Patterns: The Factory PatternsFactory Patterns
2© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
ObjectivesObjectives
To present the structure, behavior, and characteristics of generator patterns
To discuss the uses of factory methods
To present the Factory Method and Abstract Factory design patterns
3© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
TopicsTopics
Instance creation Generator patterns Factory methods The Factory Method pattern The Abstract Factory pattern
4© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Instance CreationInstance Creation
There are two ways to create objects:• Instantiating a class using one of its
constructors• Cloning an existing object
Clients may use another class to create an instance on their behalf; this is the essence of the generator pattern category.
Analogy: a tailor
5© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Generator Pattern Generator Pattern StructureStructure
The Client must access the Generator that creates an instance of the Product and provides it to the Client
Client Generator
Product
creates
6© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Broker Pattern Broker Pattern BehaviorBehavior
:Client :Generator
p:Product
p := createProduct()create
sd GeneratorBehavior
7© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Generator Pattern Generator Pattern AdvantagesAdvantages
Product Creation Control—A generator can mediate access to constructors so that only a certain number or type of product instances are created.
Product Configuration Control—A generator can take responsibility for configuring product instances.
Client and Product Decoupling—A generator can determine how to create product instances for a client.
8© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Factory MethodsFactory Methods
A Generator must have an operation that creates and returns Product instances.
A factory method is a non-constructor operation that creates and returns class
instances.
A factory method is a non-constructor operation that creates and returns class
instances.
9© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Factory Method Factory Method CapabilitiesCapabilities
Access to product constructors can be restricted.
Private data can be provided to new product objects.
Product objects can be configured after creation.
Product class bindings can be deferred until runtime.
10© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Factory PatternsThe Factory Patterns
All generator patterns have factory methods.
Factory patterns configure participating classes in certain ways to decouple the client from the product.
Interfaces are used to• Change the generator• Change the product instances
Analogy: automobile factories
11© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Factory PatternsThe Factory Patterns
Factory Method—Uses interfaces and abstract classes to decouple the client from the generator class and the resulting products.
Abstract Factory—Has a generator that is a container for several factory methods, along with interfaces decoupling the client from the generator and the products.
12© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Factory Method The Factory Method PatternPattern
The generator usually contains both factory methods and other methods.
Analogy: different auto factories producing the same kind of automobile (SUVs for example).
13© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Factory Pattern Factory Pattern StructureStructure
«interface»Product
ConcreteProduct
Client
creates
createProduct() : Product
ConcreteFactory
Factory
createProduct() : Product
14© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Factory Method Factory Method BehaviorBehavior
:Factory
:Product
:Client
p=createProduct() create
sd FactoryMethodBehavior
15© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Iterator and The Iterator and Factory Method Factory Method PatternsPatterns
ConcreteCollection
ConcreteIterator
iterator() : Iterator
«interface»Collection
«interface»Iterator
reset()
isDone() : booleangetCurrent() : Object
next()
Client
creates
16© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
When to Use the When to Use the Factory Method PatternFactory Method Pattern
Use the Factory Method pattern when there is a need to decouple a client from a particular product that it uses.
Use the Factory Method to relieve a client of responsibility for creating and configuring instances of a product.
17© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Abstract Factory The Abstract Factory PatternPattern
A factory class is one that contains only factory methods for different (though usually related) products.
The Abstract Factory generator class is a factory class.
• Restricts the Factory Method pattern because the generator holds only factory methods
• Generalizes the Factory Method pattern because the generator creates several different kinds of product
Analogy: auto factory with assembly lines for different kinds of vehicles
18© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Abstract Abstract Factory Factory Pattern Pattern StructurStructuree «interface»
ProductA
ConcreteProductA1
Client
creates
AbstractFactory
«interface»ProductB
ConcreteProductB2
creates
ConcreteProductA2
ConcreteProductB1
createProductA() : ProductA createProductB() : ProductB
ConcreteFactory1
createProductA() : ProductA createProductB() : ProductB
ConcreteFactory2
createProductA() : ProductAcreateProductB() : ProductB
19© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Abstract Factory Pattern Abstract Factory Pattern BehaviorBehavior
:Factory2:Client
pA:ProductA2pA=createProductA()
pB:ProductB2pB=createProductB()
create
create
sd AbstractFactoryMethodBehavior
20© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Abstract Factory Pattern Abstract Factory Pattern ExampleExample
«interface»SensorDevice
SimSensor
Client
RealValve
creates
RealSensor
SimValve
«interface»DeviceFactory
createSensorDevice( port : Object ) : SensorDevicecreateValveDevice( type : String, port : Object ) : ValveDevicecreateDisplayDevice( port : Object ) : DisplayDevicecreateKeypadDevice( port : Object ) : KeypadDevicecreateScreenButtonDevice( port : Object ) : ScreenButtonDevicecreateClockDevice( port : Object ) : ClockDevice
SimDeviceFactory RealDeviceFactory
«interface»ValveDevice
«interface»DisplayDevice
«interface»KeypadDevice
«interface»ClockDevice
RealDisplay
SimDisplay
RealKeypad
SimKeypad
RealClock
SimClock
«interface»ScreenButtonDevice
RealScreenButtonDevice
SimScreenButtonDevice
21© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
When to Use the When to Use the Abstract Factory Abstract Factory PatternPattern
Use the Abstract Factory pattern when clients must be decoupled from product classes.
• Especially useful for program configuration and modification
The Abstract Factory pattern can also enforce constraints about which classes must be used with others.
It may be a lot of work to make new concrete factories.
22© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
SummarySummary
Generator patterns use a Generator class to produce and configure instances of a Product class for a Client.
The Factory Method pattern uses interfaces and abstract classes to decouple clients from generators and products.
The Abstract Factory pattern has a factory class whose factory methods create instances of different but related products.