Title: Structural Design Patterns
1Structural Design Patterns
- Yaodong Bi
- October 25, 2005
2Structural design patterns
- Façade
- Decorator
- Composite
- Proxy
- Adapter
- Bridge
- Flyweight
3Façade
- Design Purpose
- Provide a single and simple interface to a
package of classes - Design Pattern Summary
- Define a single and simple interface for clients
to use the functionality of the package
4Façade - examples
- A compiler package
- It normally contains many classes/subpackages
like Scanner, Parser, etc. - Most clients only want to compile their programs,
i.e., they dont care about functions of
individual components in the package - Use Façade to provide a simple default interface
to most clients.
5Façade - Structure
APackage
1
Façade exposed ---------------------------- cMet
hodOfFacade() bMethodOfFacade()
Client
2
C not exposed myCMethod()
B not exposed myBMethod()
6Façade - Sequence Diagram
Client
singleton Facade
C
cMethodOfFacade()
myCMethod()
(return if any)
(return if any)
7Façade - Examples
framework
Customer getCustomerName() getNumAccounts() getPer
sonalNote() getAccount( int )
Account getAccountNum() deposit( int
) getBalance()
AccountException
CustomerException
BankCustomers
1..n
BankCustomer
BankAccount
Client main()
BankCustomers doDeposit( int amt, Customer cust,
Account acc ) getBankAccount( Customer cust, int
accNum ) getBankCustomer( String custName )
8Façade - comments
- Façade can reduce the degree of dependency
between packages - Packages are dependent on each other only through
their facades, not individual classes - Use Façade to provide a simple default view of
the package that is enough for most clients - Façade does not try to encapsulate/hide the
components in the package since there may be
clients who need to access individual components
in the package
9Decorator
- Design Purpose
- Add responsibilities to an object at runtime.
- Design Pattern Summary
- Provide for a linked list of objects, each
encapsulating responsibility.
10Decorator - examples
- The word processor example
- A text view may have a border and a scroll bar
and maybe other bells and whistles attached to it - How can those bells and whistles be added to the
text view? - Inheritance?
11Decorator structure
Component add( Component ) doAction()
1
Client
objDecorated
Decorator doAction()
Substance doAction()
void doAction() // do actions of the
decorator objDecorated.doAction() // pass
along // do actions of the decorator
12Decorator examples
decoration1Decoration
clientClient
decoration1.objectDecoratedDecoration
Decoration
.Substance
13Decorator - Sequence Diagram
Client
decoration1 Decoration
Decoration1.objDecorated Decoration
Substance
doAction()
doAction()
doAction()
14Decorator examples
Reader
1
InputStreamReader
InputStream
BufferedReader
15Decorator examples
BufferedStreamReader
InputStreamReader
System.inInputStream
16Decorator key concept
- allows addition to and removal from objects at
runtime
17Decorator sample code
18Composite
- 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.
19Composite - structure
Objects
non-leaf node
leaf node
20Composite - structure
1..n
Component add( Component ) doIt()
Client
comp
NonLeafNode doIt()
LeafNode doIt()
for all elements e in comp e.doIt()
TypeANonLeafNode doIt()
TypeBNonLeafNode doIt()
21Composite sequence diagram
22Composite examples
Component
1..n
Composite in java.awt
Container
component
. .
Window
Canvas
23Proxy
- 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.
24Proxy examples
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() else realActiveObject.expens
iveMethod()
25Proxy sequence diagram
Client
Proxy
RealActiveClass
( if needed )
expensiveMethod()
realExpensiveMethod()
26Proxy examples
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()
27Proxy 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(FileN
ame fn) bitmap readImage(fn) display()
// draw the bitmap readImage(FileName fn)
// read from the file(fn) // create a
bitmap
28Adapter
- Design Purpose
- Allow an application to use external
functionality in a retargetable manner. - Design Pattern Summary
- Write the application against an abstract version
of the external class introduce a subclass that
aggregate the external class.
29Adapter - examples
- Interact with legacy systems
- When you design a new system which has to
interact with a legacy system, you may not want
to the new system tightly coupled with (or
dependent upon) the legacy system since the
legacy system may be replaced in the future. - Using 3rd party systems
- You may want to be able to easily substitute the
current 3rd party system with another one.
30Adapter - Structure
Target request()
Adaptee requestedMethod()
adaptee
Adapter request()
adaptee.requestedMethod()
31Adapter sequence diagram
Target request()
Adaptee requestedMethod()
adaptee
Adapter request()
adaptee.requestedMethod()
32Adapter sequence diagram
Client
Target
Adapter
Adaptee
request()
request()
requestedMethod()
33Adapter sample code
Interface Target public Item
request(InputItem) Class Adaptee
public AdapteeReturn
requestedMethod(AdapteeItem) //
produce AdapteeReturn return
AdapteeReturn
Class Adapter implements Target private
Adaptee adaptee public Adapter(Adaptee ad)
adaptee ad public Item
request(InputItem ii) AdapteeItem ai
convert(ii) AdapteeReturn ar ar
adaptee.requestedMethod(ai) return
convert(ar) // convert InputItem to
Adapteeitem private AdapteeItem
convert(InputItem ii) // convert
InputItem to Adapteeitem // return
AdapteeItem // convert AdapteeReturn to
Item private Item convert(AdapteeReturn ar)
// convert AdapteeReturn to Item //
return Item
34Adapter - comments
- An adapter may have more than one adaptee
- There may not be a one-to-one correspondence
between operations of Target and those of Adaptee - So it is possible that an operation of Target is
realized with two separate adaptee classes. - The pattern decouples Client from adaptee.
- When a different adaptee is needed, we only need
to change to another adapter and the client does
not need to change at all.
35Structural Patterns - Summary
- 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 - Adapter decouples client from an existing system