8 structural design patterns - mississippi state...

57
Software Architecture – Structural Design Patterns 1 Chapter 8 Structural Design Patterns

Upload: others

Post on 21-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 1

Chapter 8 Structural Design Patterns

Page 2: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 2

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.

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 3: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 3

C «not exposed» myCMethod()

Façade «exposed»

cMethodOfFacade() Client 1

2

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

Facade Design Pattern Structure

Page 4: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 4

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 5: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 5

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.

Page 6: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 6

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 7: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 7

Using Façade to Access Bank Customers

bankCustomers

Client main()

«facade» BankCustomers

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

getBankCustomer( String custName ) introduceApplication()

BankCustomer BankAccount

Customer getCustomerName() getNumAccounts() getPersonalNote() getAccount( int )

Account getAccountNum()

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.

Page 8: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 8

Output of Façade Banking

Example

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

Page 9: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 9

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.

Page 10: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 10

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 11: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 11

Decorator Class Model

Client

objDecorated Decoration

doAction()

1

Substance doAction()

Component add( 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.

Page 12: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 12

Linked Objects in Decorator decoration1:Decoration

decoration1.objectDecorated:Decoration

… :Decoration

….:Substance

client:Client

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

Page 13: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 13

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 14: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 14

Output of Customer/Account Example

Page 15: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 15

Process Phases Discussed in This Chapter Requirements Analysis

Design

Implementation

Architecture Framework Detailed Design

x Key: = secondary emphasis x = main emphasis

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

Page 16: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 16

Decorator Applied to Customer /

Accounts Example

Client

nextComponent Account describe()

1

Customer describe()

BankingComponent add( Component )

describe()

CheckAccount describe()

getLastCheckNum(): int

SavingsAccount describe()

getInterestRate(): int

CDAccount describe()

getDuration(): int

Setup

AttemptToAddBadBankingComponentException

Page 17: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 17

Use of Decorator in java.io

InputStreamReader InputStream BufferedReader

Reader 1

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

Page 18: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 18

java.io Decorator example

:InputStreamReader

System.in:InputStream

: BufferedStreamReader

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

Page 19: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 20: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 20

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: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 21

NonLeafNode

Basis for Composite Class Model

Component

“every object involved is a Component object”

“non-leaf nodes have one or more

components”

leaf node non-leaf node

Objects

Classes 1..n

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

Page 22: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 22

NonLeafNode doIt()

Composite Class Model

etc.

component

Component add( Component )

doIt()

LeafNode doIt()

TypeANonLeafNode doIt()

TypeBNonLeafNode doIt()

1..n Client

FOR ALL elements e in component e.doIt()

Page 23: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 23

:LeafNode nonLeaf1ChildX :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: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 24

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: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 26: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 26

Bank/Teller Example

Client

reports

Supervisor add(Employee)

Employee stateName()

1..n

Setup

Clerk stateName()

President stateName()

Teller stateName()

Manager stateName()

Page 27: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 27

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 objects with Pete as President

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

Page 28: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 28

Output of Bank/Teller Example

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

Page 29: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 29

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 30: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 30

Attempt to Simplify Composite

Component 0…n

children

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

Page 31: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 32: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 32

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 33: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 33

Adapter Example

Client

AbstractClass clientNameForRequiredMethod()

Adapter clientNameForRequiredMethod()

{ adaptee. requiredMethod();}

adaptee

RequiredClass requiredMethod()

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

Page 34: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 34

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 35: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 36: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 36

Adapter Design Pattern

Financial amount() Principle

computeValue()

Client

Legacy system Adaptation Application

FinancialAdapter amount() legacyAdaptee

{ legacyAdaptee.computeValue(); }

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

Page 37: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 37

Adaptation

MyListener actionPerformed()

Java Listeners as Adapters

Result of button press

MyButton addActionListener()

ActionListener

MyClass myMethod()

actionListener

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

Page 38: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 38

Adapter Example: Inheritance Version

Client

AbstractClass standinForRequiredMethod()

Adapter standinForRequiredMethod()

{ requiredMethod();}

RequiredClass requiredMethod()

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

Page 39: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 40: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 40

Design Purpose

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.

Flyweight

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

Page 41: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 41

Flyweight

Flyweight Class Model

Flyweight doAction(Context)

ConcreteFlyweight doAction(Context)

FlyweightFactory getFlyweight(Characteristic)

Client

1..n

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

Page 42: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 42

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 43: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 43

Flyweight Example: Text Magnifier

Input

ABBRA CADABBRAA ARE THE FIRST TWO OF MANY WORDS IN THIS FILE …

Output o

o oo o

o oo o o o o

o oo o

v v vv vv v- R E D - v v

v vv v v

. . . . . .

1

2 Input color: RED ….. Starting character: 2 … Ending character: 3

v v vv vv v- R E D - v v

v vv v v

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

Page 44: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 44

When Space is No Limitation: Linked BigCharacter Objects

A1:BigACharacter color == “black”

A1:BigACharacter color “red” letter “o”…

B1:BigBCharacter color == “red”

A2:BigACharacter color “black” letter “o”…

B1:BigBCharacter color “red” letter “v”…

B2:BigBCharacter color “black” letter “v”…

R1:BigRCharacter color “black” letter “x”…

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

Page 45: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 46: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 46

Flyweights (1 each)

Line for output

Use string to determine which flyweight . Use color information to form the context (parameter value).

Make (shared) BigA, BigB, …

flyweight object available to clients

Mapping “A”, “B” etc. to a BigA BigB etc. object Client Responsibilities DP Responsibilities

v v v

v v

v v

- r e d -

v v

v v

v v v

. . . . . .

bigA:BigA

bigB:BigB v v v

v v

v v

b l a c k

v v

v v

v v v

getMatrix( ”red” )

getMatrix( “black” )

color “RED” begins 0 …

ABBRA CADABBRA …

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

Page 47: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 47

Typical Output For Large Type Example

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

Page 48: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 48

Application of Flyweight

BigCharacter Flyweight Application: Class Model

BigChar constructionChar

getMatrix( String color )

BigCharFactory getFlyweight( char )

PagePrinter

26

«singleton» BigB

«singleton» BigA

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

Page 49: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 50: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 50

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 51: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 51

Adaptation

Proxy Design Pattern

BaseActiveClass expensiveMethod()

anotherMethod()

RealActiveClass expensiveMethod()

anotherMethod()

Proxy expensiveMethod()

anotherMethod()

Client

realActiveObject

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

Instantiate with Proxy object

Page 52: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 52

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 53: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 53

I/O of Telephone

Record Proxy Example

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

Page 54: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 55: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 55

TelNums value: Vector

getTelNums(): Vector showMiddleRecord()

RemoteTelNums getTelNums()

TelNumsProxy getTelNums()

TelephoneApp display( 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 TelNumsProxy instance

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

Page 56: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 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.

Page 57: 8 Structural Design Patterns - Mississippi State Universityweb.cse.msstate.edu/~hamilton/7700/lessons/8... · Software Architecture – Structural Design Patterns 2 Design Purpose

Software Architecture – Structural Design Patterns 57

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

Summary of Structural Patterns

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