Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structural Design Patterns Yaodong Bi October 25, 2005.

Similar presentations


Presentation on theme: "Structural Design Patterns Yaodong Bi October 25, 2005."— Presentation transcript:

1 Structural Design Patterns Yaodong Bi October 25, 2005

2 Structural design patterns Façade Façade Decorator Decorator Composite Composite Proxy Proxy Adapter Adapter Bridge Bridge Flyweight Flyweight

3 Façade Design Purpose – –Provide a single and simple interface to a package of classes Design Pattern Summary – –Define a single and simple interface for clients to use the functionality of the package

4 Façade - examples A compiler package – –It normally contains many classes/subpackages like Scanner, Parser, etc. – –Most clients only want to compile their programs, i.e., they don’t care about functions of individual components in the package – –Use Façade to provide a simple default interface to most clients.

5 APackage C «not exposed» myCMethod() Façade «exposed» ---------------------------- cMethodOfFacade() bMethodOfFacade() Client 1 2 B «not exposed» myBMethod() Façade - Structure

6 :Client cMethodOfFacade() singleton :Facade :C myCMethod() (return if any) Façade - Sequence Diagram

7 framework BankCustomers Client main() BankCustomers doDeposit( int amt, Customer cust, Account acc ) getBankAccount( Customer cust, int accNum ) getBankCustomer( String custName ) BankCustomerBankAccount Customer getCustomerName() getNumAccounts() getPersonalNote() getAccount( int ) Account getAccountNum() deposit( int ) getBalance() 1..n AccountException CustomerException Façade - Examples

8 Façade - comments Façade can reduce the degree of dependency between packages – –Packages are dependent on each other only through their facades, not individual classes Use Façade to provide a simple default view of the package that is enough for most clients Façade does not try to encapsulate/hide the components in the package since there may be clients who need to access individual components in the package

9 Design Purpose – –Add responsibilities to an object at runtime. Design Pattern Summary – –Provide for a linked list of objects, each encapsulating responsibility. Decorator

10 The word processor example – –A text view may have a border and a scroll bar and maybe other bells and whistles attached to it – –How can those bells and whistles be added to the text view? – –Inheritance? Decorator - examples

11 Client objDecorated Decorator doAction() 1 Substance doAction() Component add( Component ) doAction() void doAction() { // do actions of the decorator objDecorated.doAction(); // pass along // do actions of the decorator } Decorator – structure

12 decoration1:Decoration decoration1.objectDecorated:Decoration … :Decoration ….:Substance client:Client Decorator – examples

13 :Client doAction() decoration1 :Decoration doAction() Decoration1.objDecorated :Decoration :Substance doAction() Decorator - Sequence Diagram

14 InputStreamReaderInputStream BufferedReader Reader 1 Decorator – examples

15 :InputStreamReader System.in:InputStream : BufferedStreamReader Decorator – examples

16 Decorator – key concept allows addition to and removal from objects at runtime

17 Decorator – sample code

18 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.

19 leaf node non-leaf node Objects NonLeafNode Component “every object involved is a Component object” “non-leaf nodes have one or more components” Classes 1..n Composite - structure

20 NonLeafNode doIt() comp Component add( Component ) doIt() LeafNode doIt() TypeANonLeafNode doIt() TypeBNonLeafNode doIt() 1..n Client for all elements e in comp e.do I t() Composite - structure

21 :LeafNode nonLeaf1ChildX :NonLeafNode nonLeaf1ChildX :NonLeafNode :Client doIt() nonLeaf1 :NonLeafNode doIt() nonLeaf1ChildX :NonLeafNode :LeafNode Composite – sequence diagram

22 Component Composite – examples Container Window ….. component 1..n Canvas Composite in java.awt

23 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.

24 BaseActiveClass expensiveMethod() anotherMethod() RealActiveClass expensiveMethod() anotherMethod() Proxy expensiveMethod() anotherMethod() Client realActiveObject if ( realActiveObject == null ) // not loaded yet { realActiveObject = getRealActiveObject(); realActiveObject.expensiveMethod(); } else { realActiveObject.expensiveMethod(); } Instantiate with Proxy object Proxy – examples

25 realExpensiveMethod() :Client expensiveMethod() :Proxy:RealActiveClass ( if needed: ) Proxy – sequence diagram

26 Graphics Display() Image draw() bitmap ImageProxy display() fileName TexDoc graphics image if ( image == null ) { // not loaded yet image = new Image(fileName); } Image.display(); Instantiate with Proxy object Proxy – examples

27 Proxy – 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) if (image == null) image = new Image(fileName); image = new Image(fileName); image.display(); 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 }}

28 Adapter 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 aggregate the external class.

29 Adapter - examples Interact with legacy systems – –When you design a new system which has to interact with a legacy system, you may not want to the new system tightly coupled with (or dependent upon) the legacy system since the legacy system may be replaced in the future. Using 3 rd party systems – –You may want to be able to easily substitute the current 3 rd party system with another one.

30 Adapter - Structure Client Target +request() Adapter +request() Adaptee +requestedMethod(); adaptee.requestedMethod() adaptee

31 Adapter – sequence diagram Client Target +request() Adapter +request() Adaptee +requestedMethod(); adaptee.requestedMethod() adaptee

32 Adapter – sequence diagram request() requestedMethod() ClientTargetAdapterAdaptee

33 Adapter – sample code Interface Target { public Item request(InputItem); public Item request(InputItem);} Class Adaptee { … public AdapteeReturn public AdapteeReturn requestedMethod(AdapteeItem) requestedMethod(AdapteeItem) { // produce AdapteeReturn; // produce AdapteeReturn; return AdapteeReturn; return AdapteeReturn; } …} Class Adapter implements Target { private Adaptee adaptee; private Adaptee adaptee; public Adapter(Adaptee ad) { public Adapter(Adaptee ad) { adaptee = ad; adaptee = ad; } public Item request(InputItem ii) { public Item request(InputItem ii) { AdapteeItem ai = convert(ii); AdapteeItem ai = convert(ii); AdapteeReturn ar; AdapteeReturn ar; ar = adaptee.requestedMethod(ai); ar = adaptee.requestedMethod(ai); return convert(ar); return convert(ar); } // convert InputItem to Adapteeitem // convert InputItem to Adapteeitem private AdapteeItem convert(InputItem ii) private AdapteeItem convert(InputItem ii) { // convert InputItem to Adapteeitem // convert InputItem to Adapteeitem // return AdapteeItem; // return AdapteeItem; } // convert AdapteeReturn to Item // convert AdapteeReturn to Item private Item convert(AdapteeReturn ar) { private Item convert(AdapteeReturn ar) { // convert AdapteeReturn to Item // convert AdapteeReturn to Item // return Item; // return Item; }}

34 Adapter - comments An adapter may have more than one adaptee – –There may not be a one-to-one correspondence between operations of Target and those of Adaptee – –So it is possible that an operation of Target is realized with two separate adaptee classes. The pattern decouples Client from adaptee. – –When a different adaptee is needed, we only need to change to another adapter and the client does not need to change at all.

35 Structural Patterns - Summary 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 Adapter decouples client from an existing system


Download ppt "Structural Design Patterns Yaodong Bi October 25, 2005."

Similar presentations


Ads by Google