Title: Chapter 8 Structural Design Patterns
1Chapter 8Structural Design Patterns
2Facade
- 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
3Structure of Facade
APackage
1
Façade exposed ---------------------------- cMet
hodOfFacade() bMethodOfFacade()
Client
2
C not exposed myCMethod()
B not exposed myBMethod()
4Sequence Diagram for Façade
Client
singleton Facade
C
cMethodOfFacade()
myCMethod()
(return if any)
(return if any)
5Using Façade to Access Bank Customers
framework
Customer getCustomerName() getNumAccounts() getPer
sonalNote() getAccount( int )
Account getAccountNum() deposit( int
) getBalance()
AccountException
CustomerException
1..n
bankCustomers
BankCustomer
BankAccount
Client main()
BankCustomers
facade doDeposit( int amt, Customer cust,
Account acc ) getBankAccount( Customer cust, int
accNum ) getBankCustomer( String custName )
6Decorator
- Design Purpose
- Add responsibilities to an object at runtime.
- Design Pattern Summary
- Provide for a linked list of objects, each
encapsulating responsibility.
7Decorator Class Model
1
Component add( Component ) doAction()
Client
objDecorated
Decoration doAction()
Substance doAction()
void doAction() // do actions of the
decorator objDecorated.doAction() // pass
along
8Linked Objects in Decorator
clientClient
decoration1Decoration
decoration1.objectDecoratedDecoration
Decoration
.Substance
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
9Sequence Diagram for Decorator
10Use of Decorator in java.io
Reader
1
InputStreamReader
InputStream
BufferedReader
11java.io Decorator example
BufferedStreamReader
InputStreamReader
System.inInputStream
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
12Key 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.
13Composite
- 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.
14Basis for Composite Class Model
Objects
non-leaf node
leaf node
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
15Composite Class Model
Component add( Component ) doIt()
1..n
Client
comp
NonLeafNode doIt()
LeafNode doIt()
for all elements e in comp e.doIt()
TypeANonLeafNode doIt()
TypeBNonLeafNode doIt()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
16Sequence Diagram for Composite
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
17Design 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.
18Composite in java.awt
Component
1..n
Container
component
. .
Window
Canvas
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
19Proxy
- 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.
20Proxy Design Pattern
Instantiate with Proxy object
BaseActiveClass expensiveMethod() anotherMethod()
Client
RealActiveClass expensiveMethod() anotherMethod()
Proxy expensiveMethod() anotherMethod()
realActiveObject
if ( realActiveObject null ) // not
loaded yet realActiveObject
getRealActiveObject() realActiveObject.exp
ensiveMethod() realActiveObject.expensiveMetho
d()
21Sequence Diagram for Proxy
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
22Proxy An Example
Instantiate with Proxy object
Graphics Display()
TexDoc graphics
Image draw() bitmap
ImageProxy display() fileName
image
if ( image null ) // not loaded
yet image new Image(fileName)
Image.display()
23Sample 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(FileN
ame fn) bitmap readImage(fn) display()
// draw the bitmap readImage(FileName fn)
// read from the file(fn) // create a
bitmap
24Summary of Structural Patterns
- 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
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.