Академический Документы
Профессиональный Документы
Культура Документы
Michael Eichberg
Software Engineering
Department of Computer Science
Technische Universität Darmstadt
}
The Factory Method Design Pattern
Example / Motivation -
Class Diagram of an Application Using the Framework
The GoF Design Patterns | 8
Document
Application
docs
open()
createDocument()
close()
newDocument()
save()
MyApplication
TextDocument
createDocument()
The Factory Method Design Pattern
Structure
The GoF Design Patterns | 9
Creator
Product
factoryMethod()
anOperation()
«method»
... factoryMethod()...
ConcreteCreator
ConcreteProduct
factoryMethod()
The Factory Method Design Pattern
Participants
The GoF Design Patterns | 10
• Product
… defines the interface of objects the factory method
creates.
• ConcreteProduct
… implements the Product interface.
• 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.
• ConcreteCreator
… overrides the factory method to return an instance of a
ConcreteProduct.
The Factory Method Design Pattern
Consequences (I)
The GoF Design Patterns | 11
Responsibility A Collaborator A
Collaborator B
Responsibility B
Responsibility C
Manipulator
Figure
Client
drag()
createManipulator()
...
Line Text
LineManipulator TextManipulator
createManipulator() createManipulator()
The Factory Method Design Pattern
Implementation
The GoF Design Patterns | 13
Requirements:
▶The application should support several databases
(We want to be able to change the database at startup
time.)
▶We want to support further databases
(We want to make the implementation unaware of the
specific database(s).)
Supporting Variety
Excursion | 20
DB2ResultSet FirebirdResultSet
first()
next()
close()
Client
«interface»
AbstractFactory «interface»
AbstractProductA
createProdA()
createProdB()
ProductA1 ProductA2
ConcreteFactory
createProdA() «interface»
createProdB() AbstractProductB
ConcreteFactory
▶AbstractFactory
… provides an interface for creating products of a family
▶ConcreteFactory
… implements the operations to create concrete
products
▶AbstractProduct
… declares the interface for concrete products
▶ConcreteProduct
... provides an implementation for the product created
by the corresponding ConcreteFactory
▶Client
… creates products by calling the ConcreteFactory;
uses the AbstractProduct interface
The Abstract Factory Method Design Pattern
Consequences
The GoF Design Patterns | 27
«interface»
Solution
Connection
Factory Class
createStatement()
▶ Group creation functions into a createBlob()
special "factory" class responsible create...()
for creating the objects to interact
MySQLConnection
with the database on request.
▶ Has functions like...
createStatement()
createBlob()
createStatement(), createBlob() and create...()
prepareStatement()
FirebirdConnection
as part of its interface
createStatement()
▶ Different factory subclasses provide createBlob()
create...()
implementations for different
databases.
DB2Connection
Statement s = connection.createStatement(); createStatement()
createBlob()
create...()
The Abstract Factory Method Design Pattern
Product Creation
The GoF Design Patterns | 29
«interface»
«interface» java.sql.Statement
Connection
createStatement()
createBlob()
DB2Statement FirebirdStatement MySQLStatement
DB2Connection «interface»
java.sql.Blob
createStatement()
createBlob()
ple «interface»
x am the IJavaPOSDevicesFactory
E m
fro OS . getNewCashDrawer() : jpos.CashDrawer
P in
m a getNewCoinDispenser() : jpos.CoinDispenser
D o ...
NCRJavaPOSDevicesFactory NCRJavaPOSDevicesFactory
«method» «method»
{ {
return new com.ibm.pos.jpos.CashDrawer; return new com.ncr.posdevices.CashDrawer;
} }
«interface»
jpos.CashDrawer
isDrawerOpened() : boolean
...
com.ibm.pos.jpos.CashDrawer com.ncr.posdevices.CashDrawer