Chapter 8 Structural Design Patterns. Facade Design Purpose – Provide an interface to a package of...
-
Upload
bruno-lindsey -
Category
Documents
-
view
231 -
download
4
Transcript of Chapter 8 Structural Design Patterns. Facade Design Purpose – Provide an interface to a package of...
Facade
Design Purpose– Provide an interface to a package of classes
Design Pattern Summary– Define a singleton which is the sole means for
obtaining functionality from the package
Notes: the classes need not be organized as a package; more than one class may be used for the façade
APackage
Structure of Facade
C«not exposed»myCMethod()
«exposed»
----------------------------cMethodOfFacade()bMethodOfFacade()
Client1
2
B«not exposed»myBMethod()
Sequence Diagram for Façade
:Client
cMethodOfFacade()
singleton:Facade
:C
myCMethod()
(return if any)
(return if any)
framework
Using Façade to Access Bank Customers
bankCustomers
Clientmain()
BankCustomers: «facade»doDeposit( int amt, Customer cust, Account acc )
getBankAccount( Customer cust, int accNum )getBankCustomer( String custName )
BankCustomer BankAccount
CustomergetCustomerName()getNumAccounts()getPersonalNote()getAccount( int )
AccountgetAccountNum()
deposit( int )getBalance()
1..n
AccountException
CustomerException
Decorator
Design Purpose– Add responsibilities to an object at runtime.
Design Pattern Summary– Provide for a linked list of objects, each
encapsulating responsibility.
Decorator Class Model
Client
objDecoratedDecoration
doAction()
1
SubstancedoAction()
Componentadd( Component )
doAction()
void doAction(){ // do actions of the decorator objDecorated.doAction(); // pass along}
Linked Objects in Decorator
decoration1:Decoration
decoration1.objectDecorated:Decoration
… :Decoration
….:Substance
client:Client
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Sequence Diagram for Decorator
:Client
doAction()
decoration1:Decoration
doAction()
Decoration1.objDecorated:Decoration
:Substance
doAction()
java.io Decorator example
:InputStreamReader
System.in:InputStream
: BufferedStreamReader
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Key Concept: Decorator
-- allows addition to and removal from objects at runtime
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Composite
Design Purpose– Represent a Tree of Objects
Design Pattern Summary– Use a Recursive Form in which the tree class
aggregates and inherits from the base class for the objects.
Basis for Composite Class Model
leaf node
non-leaf node
Objects
NonLeafNode
Component
“every object involvedis a Component object”
“non-leaf nodes have one or more
components”
Classes1..n
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
NonLeafNodedoIt()
Composite Class Model
comp
Componentadd( Component )
doIt()
LeafNodedoIt()
TypeANonLeafNodedoIt()
TypeBNonLeafNodedoIt()
1..nClient
for all elements e in comp e.doIt()
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Sequence Diagram for Composite
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
:LeafNodenonLeaf1ChildX:NonLeafNode
nonLeaf1ChildX:NonLeafNode
:Client
doIt()
nonLeaf1:NonLeafNode
doIt()
doIt()
nonLeaf1ChildX:NonLeafNode
:LeafNode:LeafNode
Design Goal At Work: Flexibility, Correctness
We need to add and remove employees at runtime and execute operations on all of them.
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Component
Composite in java.awt
Container
Window … . .
component
1..n
Canvas
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Proxy
Design Purpose– Avoid the unnecessary execution of
expensive functionality in a manner transparent to clients.
Design Pattern Summary– Interpose a substitute class which accesses
the expensive functionality only when required.
Proxy Design Pattern
BaseActiveClassexpensiveMethod()anotherMethod()
RealActiveClassexpensiveMethod()anotherMethod()
ProxyexpensiveMethod()
anotherMethod()
Client
realActiveObject
if ( realActiveObject == null ) // not loaded yet{ realActiveObject = getRealActiveObject(); realActiveObject.expensiveMethod(); }realActiveObject.expensiveMethod();
Instantiate withProxy object
Sequence Diagram for Proxy
realExpensiveMethod()
:Client
expensiveMethod()
:Proxy :RealActiveClass
( if needed: )
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
Proxy: An Example
GraphicsDisplay()
Imagedraw()
bitmap
ImageProxydisplay()fileName
TexDocgraphics
image
if ( image == null ) { // not loaded yet image = new Image(fileName); }Image.display();
Instantiate withProxy object
Sample code
Class TextDoc {graphics g;TextDoc(ImageProxy ip) {
g = ip;}void display() {
g.display();}
}Class ImageProxy implements Graphics {
FileName fileName;Image image;ImageProxy(FileName fn) {
fileName = fn;}display() { if (image == null) image = new Image(fileName); image.display();}
}
Interface Graphics {display();
}Class Image Implements Graphics {
Bitmap bitmap;Image(FileName fn) {
bitmap = readImage(fn);}display() {
// draw the bitmap}readImage(FileName fn) {
// read from the file(fn)// create a bitmap
}}
Facade provides an interface to collections of objects
Decorator adds to objects at runtime Composite represents trees of objects Proxy avoids calling expensive operations
unnecessarily
Summary of Structural Patterns
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.