Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structural Design Patterns Yaodong Bi December 21, 2015December 21, 2015December 21, 2015.

Similar presentations


Presentation on theme: "Structural Design Patterns Yaodong Bi December 21, 2015December 21, 2015December 21, 2015."— Presentation transcript:

1 Structural Design Patterns Yaodong Bi December 21, 2015December 21, 2015December 21, 2015

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 Client Use op1(), op2(), op3(), and op4() ClassB op1_b() op2_b() Facade op1() op2() op3() op4() ClassC op4_c() ClassA op3_a() Façade - Structure

6 Sequence Diagram ClientB:ClassB op1()op1_b() op4()op4_c() FacadeA:ClassAC:ClassC op3() op3_a() op2()op2_b()

7 Client Compiler.compile(“test.c”) Parser Compiler compile() ScannerProgramNodeBuilder Façade - Structure CodeGenerator

8 – Sample code Façade – Sample code Class Scanner { public Scanner(InputStream sourcecode) public Token scan() { … } public Token scan() { … }} Class Parser { public parse(Scanner s, ProgramNodeBudiler p) { … } public parse(Scanner s, ProgramNodeBudiler p) { … }} Class ProgramNodeBuilder { public ProgramNode newVariable(…) { … } public ProgramNode newAssignment(…) { … } public ProgramNode newReturnStmt(…) { … } } Class CodeGenerator { public void visitStatementNode(…) { … } public void visitExpressionNode(…) { … } } Class Compiler { public void compile(InputStream sc, public void compile(InputStream sc, OutputStream bytecode) { OutputStream bytecode) { Scanner sc = new Scanner(sc); Scanner sc = new Scanner(sc); ProgramNodeBuilder pnb = new ProgramNodeBuilder pnb = new ProgramNodeBuilder(); ProgramNodeBuilder(); Parser parser = new Parser(); Parser parser = new Parser(); parser.parse(sc, pnb); parser.parse(sc, pnb); IntelCodeGenerator cg = new IntelCodeGenerator cg = new IntelCodeGenerator(bytecode) IntelCodeGenerator(bytecode) ProgramNode nodetree = pnb.getRootNode(); ProgramNode nodetree = pnb.getRootNode(); parsetree.traverse(cg); parsetree.traverse(cg);}

9 framework BankCustomers Client 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

10 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

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

12 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

13 Decorator – structure Client comp void operation() { // do actions of the decorator comp.operation(); // do actions of the decorator } Component operation() add(Component) remove(Component) getChild() ConcreteComp operation() Decorator Operation()

14 Decorator - example

15 Decorator – structure Client comp super.draw() this.drawBorder() VisualComponent draw() TextView draw() Decorator draw() ScrollDecorator draw() scrollTo() scrollPosition BorderDescrotor draw() drawBorder() borderWidth comp.draw()

16 :Decorator1 :Decorator2 :ConcreteComp Decorator – examples Client

17 Decorator - Sequence Diagram Client:Decorator1 operation() Component:Decorator2 :ConcreteComp operation() return

18 1 Decorator – examples :InputStreamReader System.in:InputStream : BufferedStreamReader :Reader

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

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

21 Decorator – sample code class VisualComponent { virtual void Draw(); virtual void Resize(); }; class Decorator : public VisualComponent { Decorator(VisualComponent*); void Decorator::Draw () { _component->Draw(); } VisualComponent* _component; }; class BorderDecorator : public Decorator { BorderDecorator(VisualComponent*, int borderWidth); void Draw(){ Decorator::Draw(); DrawBorder(_width); } private void DrawBorder(int); private int _width; }; Class Window { void SetContents (VisualComponent* contents) { //... } Window* window = new Window(); TextView* textView = new TextView; window->SetContents(textView); window->SetContents( new BorderDecorator( new ScrollDecorator(textView), 1 ) ); Window.draw();

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

23 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

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

25 :ClientN0:NonLeafNode N1:NonLeafNode L1:LeafNode Composite – A Class Diagram L2:LeafNode L3:LeafNode N2:NonLeafNode

26 :ClientN0:NonLeafNode doIt() N1:NonLeafNodeL1:LeafNode Composite – sequence diagram L2:LeafNode L3:LeafNode N2:NonLeafNode doIt()

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

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

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

30 expensiveMethod() Proxy – sequence diagram BaseActiveClass RealActiveClassProxy Client create() if needed

31 Graphics Display() Image display() 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

32 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 }}

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

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

35 Class Adapter

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

37 Adapter – sequence diagram request(TargetIn):TargetOut ClientTargetAdapterAdaptee convert(TargetIn):AdapteeI n requestedMethod(AdapteeIn):AdapteeOut convert(AdapteeOut):TargetOutReturn TargetOut request(TargetIn):TargetOut

38 Object Adapter – sample code Interface Target { public TargetOut request(TargetIn); public TargetOut request(TargetIn);} Class Adaptee { … public AdapteeOut public AdapteeOut requestedMethod(AdapteeIn) requestedMethod(AdapteeIn) { // produce AdapteeOut; // produce AdapteeOut; return AdapteeOut; return AdapteeOut; } …} Class Adapter implements Target { private Adaptee adaptee; private Adaptee adaptee; public Adapter(Adaptee ad) { public Adapter(Adaptee ad) { adaptee = ad; adaptee = ad; } public TargetOut request(TargetIn ti) { public TargetOut request(TargetIn ti) { AdapteeIn ai = convert(ti); AdapteeIn ai = convert(ti); AdapteeOut ao = AdapteeOut ao = adaptee.requestedMethod(ai); adaptee.requestedMethod(ai); return convert(ao); return convert(ao); } private AdapteeIn convert(TargetIn ti) private AdapteeIn convert(TargetIn ti) { // convert TargetIn to AdapteeIn // convert TargetIn to AdapteeIn } private TargetOut convert(AdapteeOut ao) private TargetOut convert(AdapteeOut ao) { // convert AdapteeOut to TargetOut // convert AdapteeOut to TargetOut }}

39 Object Adapter - Example DrawingTool Shape +boundingBox() +createManipulator() TextShape +boundingBox() +createManipulator() TextView +getExtent(); text.getExtent() text return new TextManipulator()

40 Design for Adaption Pluggable Adapters Pluggable Adapters –A small set of operations is specified for adapter & adaptee to implement –Using Abstract Operations Client and target are the same entity Client and target are the same entity Adapter overrides abstract operations to delegate operations to adaptee Adapter overrides abstract operations to delegate operations to adaptee –Using Delegate Objects A delegate interface specifies operations the adapter needs to implement A delegate interface specifies operations the adapter needs to implement

41 Using Abstract Operations

42 Using Delegate Objects

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

44 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 December 21, 2015December 21, 2015December 21, 2015."

Similar presentations


Ads by Google