Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

54
Chapter 8 Structural Design Patterns Facade Decorator Composite Adapter Flyweight Proxy

Transcript of Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Page 1: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Chapter 8Structural Design Patterns

Facade Decorator Composite Adapter Flyweight Proxy

Page 2: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Façade Design Pattern

Page 3: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Design Purpose

Provide an interface to a package of classes

Design Pattern Summary

Define a class (typically a singleton) that is

the sole means for obtaining functionality

from the package.

Facade

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.

Page 4: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Facade Design Pattern Structure

C«not exposed»

myCMethod()

Façade«exposed»

cMethodOfFacade()Client1

2

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 5: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Sequence Diagram for Façade

:Client

cMethodOfFacade()

singleton:Facade

:C

myCMethod()

(return if any)

(return if any)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 6: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Using Façade for Architecture of a Video Game

MyGameCharacters

MyGameEngine

MyGameCast«facade»

MyGame«facade»

MyGameEnvironment

MyGameEnvironment«facade»

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

packages

Page 7: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 8: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Using Façade to Access Bank Customers

bankCustomers

Clientmain()

«facade»BankCustomers

doDeposit( int amt, Customer cust, Account acc )getBankAccount( Customer cust, int accNum )

getBankCustomer( String custName )introduceApplication()

BankCustomer BankAccount

CustomergetCustomerName()getNumAccounts()getPersonalNote()getAccount( int )

AccountgetAccountNum()

deposit( int )getBalance()

1..n

IntroMessage

framework

AccountException

CustomerException

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

package

<<interface>> <<interface>>

Page 9: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Key Concept: Facade Design Pattern

-- modularizes designs by hiding complexity (similar to the web services provided by a web site)

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 10: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Decorator Design Pattern

Page 11: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Design Purpose

Add responsibilities to an object at runtime.

Design Pattern Summary

Provide for a linked list of objects,

each encapsulating responsibility.

Decorator

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 12: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Decorator Class Model

Client

objDecoratedDecoration

doAction()

1

SubstancedoAction()

Componentadd( Component )

doAction()

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.

Undecoratedclass

Page 13: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Linked Objects in Decoratordecoration1:Decoration

decoration1.objectDecorated:Decoration

… :Decoration

….:Substance

client:Client

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

This list is created backwardsso that the last decorationadded is the first one to beexecuted

Page 14: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Sequence Diagram for Decorator

:Client

doAction()

decoration1:Decoration

doAction()

Decoration1.objDecorated:Decoration

:Substance

doAction()

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 15: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Decorator Applied to Customer / Accounts Example

Client

nextComponentAccountdescribe()

1

Customerdescribe()

BankingComponentadd( Component )

describe()

CheckAccountdescribe()

getLastCheckNum(): int

SavingsAccountdescribe()

getInterestRate(): int

CDAccountdescribe()

getDuration(): int

Setup

AttemptToAddBadBankingComponentException

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

<<interface>> Means exactlyone aggregateper account

main()

Page 16: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Decorator Applied to Construction Example

Client

nextComponentConstrMaterial

showPrice()

1

ConstrJobadd()

showPrice()

ConstructionComponentadd( Component )

showPrice()

WindowshowPrice()

DoorshowPrice()

BeamshowPrice()

Setup

construction

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 17: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Use of Decorator in java.io

InputStreamReaderInputStream BufferedReader

Reader1

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

BufferedReader bufReader = new BufferedReader (new InputStreamReader(System.in) );

Page 18: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Key Concept: Decorator Design Pattern

-- allows addition to and removal from objects at runtime-- represents an object version of a linked list where a client does not need to know about the subclasses in the list -- emphasizes the structural properties of the substance in the list

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 19: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Composite Design Pattern

Page 20: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Composite

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 21: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

NonLeafNode

Basis for Composite Class Model

Component

“every object involvedis a Component object”

“non-leaf nodes have one or more

components”

leaf nodenon-leaf node

Objects

Classes 1..n

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 22: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

NonLeafNodedoIt()

Composite Class Model

etc.

component

Componentadd( Component )

doIt()

LeafNodedoIt()

TypeANonLeafNodedoIt()

TypeBNonLeafNodedoIt()

1..nClient

FOR ALL elements e in component e.doIt()

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 23: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

:LeafNodenonLeaf1ChildX:NonLeafNode

nonLeaf1ChildX:NonLeafNode

Sequence Diagram for Composite

:Client

doIt()

nonLeaf1:NonLeafNode

doIt()

doIt()

nonLeaf1ChildX:NonLeafNode

:LeafNode:LeafNode

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 24: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Employee Hierarchy

Pete:President

Able:Manager

Becky:Manager

Lonny:Teller

Cal:Clerk

Tina:Teller

Thelma:Teller

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 25: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 26: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Bank/Teller Example

Client

reports

Supervisoradd(Employee)

EmployeestateName()

1..n

Setup

ClerkstateName()

PresidentstateName()

TellerstateName()

ManagerstateName()

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

main()

Composite Design Pattern

Page 27: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Sequence Diagram for Bank/Teller Example

:Client

stateName()

pete:President

xxxx:Employee

:Setp

doClientTasks()

stateName()

xxxx:Employee

xxxx:Employee

xxxx:Employee

*

• Creates the tree of Employee objectswith Pete as President

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 28: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Component

Composite in java.awt

Container

Window … . .

component

1..n

Canvas

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 29: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 30: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Adapter Design Pattern

Page 31: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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 aggregates the external class.

Adapter

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 32: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Adapter Example

Client

AbstractClassclientNameForRequiredMethod()

AdapterclientNameForRequiredMethod()

{ adaptee. requiredMethod();}

adaptee

RequiredClassrequiredMethod()

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 33: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Sequence Diagram for Adapter

:Client

clientNameForRequiredMethod()

:AbstractClass :Adapter

RequiredMethod()

adaptee:RequiredClass

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 34: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 35: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Adapter Design Pattern

Financialamount() Principle

computeValue()

Client

Legacy systemAdaptationApplication

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

FinancialAdapteramount()

Setup code in the client instantiates the Financial object as a FinancialAdapter instance

Page 36: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Code Example

class FinancialAdapter extends Financial{ Principal legacyAdaptee = null;

// Constructor goes here . . .

// This method uses the legacy computeValue() method float amount(float originalAmount, float numYears, float intRate) { return legacyAdaptee.computeValue(orginalAmount, numYears, intRate); }}

executeFinanceApplication(new FinancialAdapter() );

Page 37: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 38: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Flyweight Design Pattern

Page 39: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Design Purpose

Manage a large number of almost

indistinguishable objects without

constructing them all at once.

Design Pattern Summary

Share representatives for the objects; use

context to obtain the effect of multiple instances.

Flyweight

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 40: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Flyweight

Flyweight Class Model

FlyweightdoAction(Context)

ConcreteFlyweightdoAction(Context)

FlyweightFactorygetFlyweight(Characteristic)

Client

1..n

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

static method

singleton

Page 41: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Sequence Diagram for Flyweight

:Client

getFlyweight()

:FlyweightFactory flyweight:Flyweight

doAction( context )

Get context

flyweight

. . . .

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 42: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Example A: Window size of 15; folder contains 20 items

Page 43: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Example B: Window size of 15; folder contains over 500 items

Each line item had a drawing window associated with it

Page 44: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Design Goal At Work: Space Efficiency

We want to avoid proliferating an object for every item to be displayed.

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 45: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 46: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Proxy Design Pattern

Page 47: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o 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.

Proxy

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 48: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Adaptation

Proxy Design Pattern

BaseActiveClassexpensiveMethod()anotherMethod()

RealActiveClassexpensiveMethod()anotherMethod()

ProxyexpensiveMethod()anotherMethod()

Client

realActiveObject

. . . // One way to check if it is really needed:if ( realActiveObject == null ) // never referenced{ realActiveObject = getRealActiveObject(); realActiveObject.expensiveMethod(); }else // try to avoid calling the real expensiveMethod()

Instantiate withProxy object

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

<<abstract>>

Page 49: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

realExpensiveMethod()

Sequence Diagram for Proxy

:Client

expensiveMethod()

:Proxy :RealActiveClass

( if needed: )

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 50: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

I/O of Telephone Record Proxy

Example

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

> all

> middle

> all

Page 51: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 52: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

TelNumsvalue: Vector

getTelNums(): VectorshowMiddleRecord()

RemoteTelNumsgetTelNums()

TelNumsProxygetTelNums()

TelephoneAppdisplay( TelNums )

display MiddleRecord()

remoteTelNums

. . . // One way to check if really needed:if ( value == null ) // never referenced

remoteTelNums.getTelNums(); else // no need to call ‘getTelNums()’

static

1

Proxy Example

Setup

Ensures that TelephoneApp makes calls with TelNumsProxyinstance

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Page 53: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

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.

Page 54: Chapter 8 Structural Design Patterns o Facade o Decorator o Composite o Adapter o Flyweight o Proxy.

Structural Design Patterns relate objects (as trees, lists etc.)

Facade provides an interface to collections of objects

Decorator adds to objects at runtime (in a list structure)

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

Summary of Structural Design Patterns

Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.