Structural Design Patterns - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Structural Design Patterns

Description:

Most clients only want to compile their programs, i.e., they don't care about ... read from the file(fn) // create a bitmap. Adapter. Design Purpose ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 36
Provided by: ericb172
Learn more at: http://www.cs.uofs.edu
Category:

less

Transcript and Presenter's Notes

Title: Structural Design Patterns


1
Structural Design Patterns
  • Yaodong Bi
  • October 25, 2005

2
Structural design patterns
  • Façade
  • Decorator
  • Composite
  • Proxy
  • Adapter
  • Bridge
  • Flyweight

3
Faç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

4
Faç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.

5
Façade - Structure
APackage
1
Façade exposed ---------------------------- cMet
hodOfFacade() bMethodOfFacade()
Client
2
C not exposed myCMethod()
B not exposed myBMethod()
6
Façade - Sequence Diagram
Client
singleton Facade
C
cMethodOfFacade()
myCMethod()
(return if any)
(return if any)
7
Faç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 )
8
Faç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

9
Decorator
  • Design Purpose
  • Add responsibilities to an object at runtime.
  • Design Pattern Summary
  • Provide for a linked list of objects, each
    encapsulating responsibility.

10
Decorator - 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?

11
Decorator 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
12
Decorator examples
decoration1Decoration
clientClient
decoration1.objectDecoratedDecoration
Decoration
.Substance
13
Decorator - Sequence Diagram
Client
decoration1 Decoration
Decoration1.objDecorated Decoration
Substance
doAction()
doAction()
doAction()
14
Decorator examples
Reader
1
InputStreamReader
InputStream
BufferedReader
15
Decorator examples
BufferedStreamReader
InputStreamReader
System.inInputStream
16
Decorator key concept
  • allows addition to and removal from objects at
    runtime

17
Decorator sample code
18
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.

19
Composite - structure
Objects
non-leaf node
leaf node
20
Composite - 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()
21
Composite sequence diagram
22
Composite examples
Component
1..n
Composite in java.awt
Container
component
. .
Window
Canvas
23
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.

24
Proxy 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()
25
Proxy sequence diagram
Client
Proxy
RealActiveClass
( if needed )
expensiveMethod()
realExpensiveMethod()
26
Proxy 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()
27
Proxy 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
28
Adapter
  • 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.

29
Adapter - 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.

30
Adapter - Structure
Target request()
Adaptee requestedMethod()
adaptee
Adapter request()
adaptee.requestedMethod()
31
Adapter sequence diagram
Target request()
Adaptee requestedMethod()
adaptee
Adapter request()
adaptee.requestedMethod()
32
Adapter sequence diagram
Client
Target
Adapter
Adaptee
request()
request()
requestedMethod()
33
Adapter 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
34
Adapter - 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.

35
Structural 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
Write a Comment
User Comments (0)
About PowerShow.com