Chapter 8 Structural Design Patterns - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 8 Structural Design Patterns

Description:

'non-leaf nodes have one or more components' Classes. 1..n ... Facade provides an interface to collections of objects. Decorator adds to objects at runtime ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 25
Provided by: ericb172
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 Structural Design Patterns


1
Chapter 8Structural Design Patterns
2
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

3
Structure of Facade
APackage
1
Façade exposed ---------------------------- cMet
hodOfFacade() bMethodOfFacade()
Client
2
C not exposed myCMethod()
B not exposed myBMethod()
4
Sequence Diagram for Façade
Client
singleton Facade
C
cMethodOfFacade()
myCMethod()
(return if any)
(return if any)
5
Using 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 )
6
Decorator
  • Design Purpose
  • Add responsibilities to an object at runtime.
  • Design Pattern Summary
  • Provide for a linked list of objects, each
    encapsulating responsibility.

7
Decorator Class Model
1
Component add( Component ) doAction()
Client
objDecorated
Decoration doAction()
Substance doAction()
void doAction() // do actions of the
decorator objDecorated.doAction() // pass
along
8
Linked 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.
9
Sequence Diagram for Decorator
10
Use of Decorator in java.io
Reader
1
InputStreamReader
InputStream
BufferedReader
11
java.io Decorator example
BufferedStreamReader
InputStreamReader
System.inInputStream
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
12
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.
13
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.

14
Basis 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.
15
Composite 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.
16
Sequence Diagram for Composite
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
17
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.
18
Composite 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.
19
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.

20
Proxy 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()
21
Sequence Diagram for Proxy
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
22
Proxy 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()
23
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
24
Summary 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.
Write a Comment
User Comments (0)
About PowerShow.com