Chapter 8 Structural Design Patterns - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 8 Structural Design Patterns

Description:

Structural Design Patterns. Process Phases Discussed in This Chapter ... Adapted from Software Design: From Programming to Architecture ... D. E. R. v ... – PowerPoint PPT presentation

Number of Views:625
Avg rating:3.0/5.0
Slides: 64
Provided by: ericb171
Category:

less

Transcript and Presenter's Notes

Title: Chapter 8 Structural Design Patterns


1
Chapter 8Structural Design Patterns
2
Process Phases Discussed in This Chapter
Requirements Analysis
Design
Architecture
Framework
Detailed Design
Implementation
Key
secondary emphasis
main emphasis
x
x
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
3
Design Purpose
Facade
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.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
4
Facade Design Pattern Structure
1
Façade exposed cMethodOfFacade()
Client
2
C not exposed myCMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
5
Sequence Diagram for Façade
Client
singleton Facade
C
cMethodOfFacade()
myCMethod()
(return if any)
(return if any)
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
6
Using Façade for Architecture of a Video Game
MyGameEngine
MyGame facade
MyGameCharacters
MyGameCast facade
MyGameEnvironment
MyGameEnvironment facade
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
7
Design Goals At Work ? Correctness and
Reusability ?
Collecting customer-related classes in a package
with a clear interface clarifies the design,
allows independent verification, and makes this
part reusable.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
8
Using Façade to Access Bank Customers
Customer getCustomerName() getNumAccounts() getPer
sonalNote() getAccount( int )
Account getAccountNum() deposit( int
) getBalance()
AccountException
CustomerException
framework
1..n
BankCustomer
BankAccount
IntroMessage
facade BankCustomers doD
eposit( int amt, Customer cust, Account acc
) getBankAccount( Customer cust, int accNum
) getBankCustomer( String custName
) introduceApplication()
Client main()
bankCustomers
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
9
Output of Façade Banking Example
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
10
Key Concept ? Facade Design Pattern ?
-- modularizes designs by hiding complexity
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
11
Design Purpose
Decorator
Add responsibilities to an object at runtime.
Design Pattern Summary
Provide for a linked list of objects, each
encapsulating responsibility.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
12
Decorator Class Model
Component add( Component ) doAction()
1
Client
Decoration doAction()
Substance doAction()
objDecorated
void doAction() .. // do actions special to
this decoration objDecorated.doAction() //
pass along
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
13
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.
14
Sequence Diagram for Decorator
Client
decoration1 Decoration
Decoration1.objDecorated Decoration
Substance
doAction()
doAction()
doAction()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
15
Output of Customer/Account Example
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
16
Decorator Applied to Customer / Accounts Example
AttemptToAddBadBankingComponentException
BankingComponent add( Component ) describe()
1
Client
nextComponent
Setup
Account describe()
Customer describe()
CheckAccount describe() getLastCheckNum() int
SavingsAccount describe() getInterestRate() int
CDAccount describe() getDuration() int
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
17
Use of Decorator in java.io
Reader
1
InputStreamReader
BufferedReader
InputStream
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
18
java.io Decorator example
BufferedStreamReader
InputStreamReader
System.inInputStream
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
19
Key Concept ? Decorator Design Pattern ?
-- 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.
20
Design Purpose
Composite
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.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
21
Basis for Composite Class Model
Objects
leaf node
non-leaf node
1..n
Classes
Component
non-leaf nodes have one or more components
every object involved is a Component object
NonLeafNode
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
22
Composite Class Model
Component add( Component ) doIt()
1..n
Client
FOR ALL elements e in component e.doIt()
NonLeafNode doIt()
LeafNode doIt()
component
etc.
TypeANonLeafNode doIt()
TypeBNonLeafNode doIt()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
23
Sequence Diagram for Composite
Client
nonLeaf1 NonLeafNode
nonLeaf1ChildX NonLeafNode
LeafNode
nonLeaf1ChildX NonLeafNode
LeafNode
LeafNode
nonLeaf1ChildX NonLeafNode
doIt()
doIt()
doIt()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
24
Employee Hierarchy
Pete President
Able Manager
Becky Manager
Tina Teller
Lonny Teller
Cal Clerk
Thelma Teller
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
25
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.
26
Bank/Teller Example
1..n
Employee stateName()
Client
reports
Clerk stateName()
Supervisor add(Employee)
Teller stateName()
Setup
President stateName()
Manager stateName()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
27
Sequence Diagram for Bank/Teller Example
Setp
Client
pete President
xxxx Employee
xxxx Employee
xxxx Employee
xxxx Employee

doClientTasks()
stateName()
stateName()
  • Creates the tree of Employee objects
  • with Pete as President

Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
28
Output of Bank/Teller Example
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
29
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.
30
Attempt to Simplify Composite
children
Component
0n
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
31
Key Concept ? Composite Design Pattern ?
-- used to represent trees of objects.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
32
Design Purpose
Adapter
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
aggregates the external class.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
33
Adapter Example
AbstractClass clientNameForRequiredMethod()
RequiredClass requiredMethod()
Client
adaptee
Adapter clientNameForRequiredMethod()
adaptee. requiredMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
34
Sequence Diagram for Adapter
Client
AbstractClass
Adapter
adaptee RequiredClass
clientNameForRequiredMethod()
RequiredMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
35
Design Goal At Work ? Flexibility and
Robustness ?
We want to separate the application as a whole
from financial calculations which will be
performed externally.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
36
Adapter Design Pattern
Legacy system
Adaptation
Application
Financial amount()
Principle computeValue()
Client
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
37
Java Listeners as Adapters
actionListener
ActionListener
Result of button press
MyButton addActionListener()
MyClass myMethod()
Adaptation
MyListener actionPerformed()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
38
Adapter Example Inheritance Version
AbstractClass standinForRequiredMethod()
RequiredClass requiredMethod()
Client
Adapter standinForRequiredMethod()
requiredMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
39
Key Concept ? Adapter Design Pattern ?
-- to interface flexibly with external
functionality.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
40
Design Purpose
Flyweight
Manage a large number of objects without
constructing them all.
Design Pattern Summary
Share representatives for the objects use
context to obtain the effect of multiple
instances.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
41
Flyweight Class Model
Client
Flyweight
1..n
Flyweight doAction(Context)
FlyweightFactory getFlyweight(Characteristic)
ConcreteFlyweight doAction(Context)
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
42
Sequence Diagram for Flyweight
Client
FlyweightFactory
flyweight Flyweight
getFlyweight()
flyweight
Get context
. . . .
doAction( context )
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
43
Flyweight Example Text Magnifier
Input
ABBRA CADABBRAA ARE THE FIRST TWO OF MANY WORDS
IN THIS FILE
1
Input color RED .. Starting character 2
Ending character 3
2
Output
o
o o
o o
o o
o o o o o
o o
o o
v v v
v v
v v
- R E D -
v v
v v
v v v
v v v
v v
v v
- R E D -
v v
v v
v v v
. . . . . .
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
44
When Space is No LimitationLinked BigCharacter
Objects
A1BigACharacter color red letter o
B1BigBCharacter color red
B1BigBCharacter color red letter v
A1BigACharacter color black
B2BigBCharacter color black letter v
R1BigRCharacter color black letter x
A2BigACharacter color black letter o
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
45
Design Goal At Work ? Space Efficiency ?
We want to avoid proliferating an object for
every big character to be displayed.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
46
Mapping A, B etc. to a BigA BigB etc. object
Client Responsibilities
DP Responsibilities
Use string to determine which flyweight . Use
color information to form the context (parameter
value).
Make (shared) BigA, BigB, flyweight object
available to clients
ABBRA CADABBRA
color RED begins 0
Flyweights (1 each)
bigABigA
getMatrix( red )
Line for output
v v v
v v
v v
- r e d -
v v
v v
v v v
v v v
v v
v v
b l a c k
v v
v v
v v v
bigBBigB
. . . . . .
getMatrix( black )
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
47
Typical Output For Large Type Example
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
48
BigCharacter Flyweight Application Class Model
PagePrinter
Application of Flyweight
BigChar constructionChar getMatrix( String color )
26
BigCharFactory getFlyweight( char )
singleton BigB
singleton BigA
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
49
Key Concept ? Flyweight Design Pattern ?
-- to obtain the benefits of a large set of
individual objects without efficiency penalties.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
50
Design Purpose
Proxy
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.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
51
Proxy Design Pattern
Client
Adaptation
BaseActiveClass expensiveMethod() anotherMethod()
Instantiate with Proxy object
RealActiveClass expensiveMethod() anotherMethod()
Proxy expensiveMethod() anotherMethod()
realActiveObject
. . . // One way to check if really needed if (
realActiveObject null ) // never
referenced realActiveObject
getRealActiveObject() realActiveObject.exp
ensiveMethod() else // try to avoid calling
the real expensiveMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
52
Sequence Diagram for Proxy
Client
Proxy
RealActiveClass
expensiveMethod()
( if needed )
realExpensiveMethod()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
53
I/O of Telephone Record Proxy Example
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
54
Design Goal At Work ? Efficiency and Reuse ?
Avoid unnecessary data downloads.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
55
Proxy Example
TelNums value Vector getTelNums()
Vector showMiddleRecord()
TelephoneApp display( TelNums ) display
MiddleRecord()
static
RemoteTelNums getTelNums()
TelNumsProxy getTelNums()
remoteTelNums
1
Setup
. . . // One way to check if really needed if (
value null ) // never referenced remoteTelNu
ms.getTelNums() else // no need to call
getTelNums()
Ensures that TelephoneApp makes calls with
TelNumsProxy instance
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
56
Key Concept ? Proxy Design Pattern ?
-- to call expensive or remote methods.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
57
Summary of Structural Patterns
  • Structural Design Patterns relate objects (as
    trees, lists etc.)
  • Facade provides an interface to collections of
    objects
  • Decorator adds to objects at runtime
  • Composite represents trees of objects
  • Adapter simplifies the use of external
    functionality
  • Flyweight gains the advantages of using multiple
    instances while minimizing space penalties
  • Proxy avoids calling expensive operations
    unnecessarily

Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
58
Queuing Configuration Exercise
Customer being served
Queues
Tellers
c
c c c
Lonny
c
Juanita
Key c a customer
c
c c c c c
Tina
c
Thelma
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
59
Solution to Exercise 8.1 Queueing Configurations
1..n
Queue .()
Client
queues
QueueForOneTeller .()
Teller
Setup
QueueForTellers add( Queue
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
60
Using Façade
InventoryItemException
Inventory ( )
InventoryItem ( )
inventoryFramework
Xinventory ( )
1..n
Toaster
Toaster
Toaster
1
XIntroMessage
facade XInventoryFacade
( )
Client main()
xInventory
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
61
Decorator Applied to Construction Example
construction
ConstructionComponent add( Component ) showPrice()
1
Client
nextComponent
Setup
ConstrJob add() showPrice()
ConstrMaterial showPrice()
Window showPrice()
Door showPrice()
Beam showPrice()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
62
Typical Stencil Placement on a Wall
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
63
Typical Wall Perspective
etc.
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