Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Structural Design Patterns

Similar presentations


Presentation on theme: "Chapter 8 Structural Design Patterns"— Presentation transcript:

1 Chapter 8 Structural Design Patterns

2 Facade Design Purpose Design Pattern Summary
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 Client «exposed» Façade cMethodOfFacade() bMethodOfFacade() 2 B «not exposed» myBMethod() C «not exposed» myCMethod()

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() getPersonalNote() getAccount( int ) AccountException Account getAccountNum() deposit( int ) getBalance() 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 Design Pattern Summary
Add responsibilities to an object at runtime. Design Pattern Summary Provide for a linked list of objects, each encapsulating responsibility.

7 Decorator Class Model Component 1 Client Substance Decoration
add( Component ) doAction() 1 Client objDecorated Substance doAction() Decoration doAction() void doAction() { // do actions of the decorator objDecorated.doAction(); // pass along }

8 Linked Objects in Decorator
client:Client decoration1:Decoration decoration1.objectDecorated:Decoration … :Decoration ….:Substance Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

9 Decoration1.objDecorated
Sequence Diagram for Decorator :Client doAction() decoration1 :Decoration Decoration1.objDecorated :Substance

10 Use of Decorator in java.io
Reader 1 InputStream InputStreamReader BufferedReader

11 java.io Decorator example
: BufferedStreamReader :InputStreamReader System.in:InputStream 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 Design Pattern Summary
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 NonLeafNode Component “every object involved is a Component object” “non-leaf nodes have one or more components” Classes 1..n 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 Client Component add( Component ) doIt() 1..n
LeafNode doIt() NonLeafNode 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
:LeafNode nonLeaf1ChildX :NonLeafNode :Client doIt() nonLeaf1 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 Design Pattern Summary
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 BaseActiveClass Client RealActiveClass
Instantiate with Proxy object BaseActiveClass expensiveMethod() anotherMethod() Client RealActiveClass expensiveMethod() anotherMethod() Proxy expensiveMethod() anotherMethod() realActiveObject if ( realActiveObject == null ) // not loaded yet { realActiveObject = getRealActiveObject(); realActiveObject.expensiveMethod(); }

21 realExpensiveMethod()
Sequence Diagram for Proxy realExpensiveMethod() :Client expensiveMethod() :Proxy :RealActiveClass ( if needed: ) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

22 Proxy: An Example Graphics TexDoc Image Instantiate with Proxy object
Display() 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(FileName 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.


Download ppt "Chapter 8 Structural Design Patterns"

Similar presentations


Ads by Google