Structural Design Patterns Neeraj Ray 1 Structural Patterns

  • Slides: 35
Download presentation
Structural Design Patterns - Neeraj Ray 1

Structural Design Patterns - Neeraj Ray 1

Structural Patterns - Overview n n Adapter Bridge Composite Decorator 2

Structural Patterns - Overview n n Adapter Bridge Composite Decorator 2

Adapter (Wrapper) - Intent Convert the interface of a class into another interface that

Adapter (Wrapper) - Intent Convert the interface of a class into another interface that clients expect. 3

Adapter - Motivation Suppose your organization has a useful class Customer with operations get.

Adapter - Motivation Suppose your organization has a useful class Customer with operations get. Name() and add. Account(). The application you are building is filled with references to an abstract interface called Cust, with operations name() and add. Acc() etc. , and a concrete subclass Bank. Cust. You want to be able to use Customer without changing the application you are building. 4

Adapter - Example Client <<Abstract>> Customer + add. Acc () + add. Account ()

Adapter - Example Client <<Abstract>> Customer + add. Acc () + add. Account () Bank. Cust + add. Account () customer. add. Acc() 5

Object Adapter - Structure Client <<Abstract>> Target Adaptee + specific. Request () + request

Object Adapter - Structure Client <<Abstract>> Target Adaptee + specific. Request () + request () Adapter + request () adaptee. specific. Request() 6

Class Adapter - Structure Client <<Abstract>> Target Adaptee + specific. Request () + request

Class Adapter - Structure Client <<Abstract>> Target Adaptee + specific. Request () + request () Adapter + request () specific. Request() 7

Adapter - Applicability n n n To use an existing class, and its interface

Adapter - Applicability n n n To use an existing class, and its interface does not match the one you need. To create a reusable class that cooperates with classes that don’t have compatible interfaces. (Object adapter only) To use several existing subclasses, but it can be impractical to adapt their interfaces by subclassing every one. An object adapter can adapt the interface of its parent. 8

Class Adapter - Consequences : Lets Adapter override some of the Adaptee’s behavior, since

Class Adapter - Consequences : Lets Adapter override some of the Adaptee’s behavior, since Adapter is a subclass of Adaptee. : Introduces only one object, and no additional pointer indirection is needed to get to the Adapter. è Adapts Adaptee to Target by committing to a concrete Adapter class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses. 9

Object Adapter - Consequences : Lets a single Adapter work with many Adaptee’s-- i.

Object Adapter - Consequences : Lets a single Adapter work with many Adaptee’s-- i. e. , the Adaptee itself and all of its subclasses. è Makes it harder to override Adaptee’s behavior. 10

Adapter - Other Issues n n n How much adapting should an Adapter do?

Adapter - Other Issues n n n How much adapting should an Adapter do? It depends on how similar the Target interface is to Adaptee’s. Using two-way adapters -- making two separate interfaces work as both Adapter and Adaptee. Pluggable interface -- design an interface for adaptation. This is similar to the idea of Open Implementation. This can be designed using Adapter design pattern. 11

Bridge - Intent Decouple an abstraction from an implementation so that the two can

Bridge - Intent Decouple an abstraction from an implementation so that the two can vary independently. 12

Bridge - Motivation Inheritance binds an implementation to the abstraction permanently, which makes it

Bridge - Motivation Inheritance binds an implementation to the abstraction permanently, which makes it difficult to modify, extend and reuse abstractions and implementations independently. 13

Bridge - Motivation (Cont. ) Window XWindow PMWindow Icon. Window XWindow. Icon PMWindow. Icon

Bridge - Motivation (Cont. ) Window XWindow PMWindow Icon. Window XWindow. Icon PMWindow. Icon 14

Bridge - Motivation (Cont. ) n n It is inconvenient to extend the window

Bridge - Motivation (Cont. ) n n It is inconvenient to extend the window abstraction to cover different kinds of windows or new platform. It makes the client-code platform dependent. 15

Bridge - Example <<Abstract>> Window. Impl <<Abstract>> Window draw. Line () draw. Text ()

Bridge - Example <<Abstract>> Window. Impl <<Abstract>> Window draw. Line () draw. Text () draw. Rect () impl. draw. Line() Icon. Window XWindow draw. Line () draw. Text () PMWindow draw. Line () draw. Text () 16

Bridge - Structure <<Abstract>> Implementor <<Abstract>> Abstraction impl. Operation () operation () impl. Operation()

Bridge - Structure <<Abstract>> Implementor <<Abstract>> Abstraction impl. Operation () operation () impl. Operation() Refined. Abstraction Concrete. Implementor. A impl. Operation () Concrete. Implentor. B impl. Operation () 17

Bridge - Applicability n n n To avoid a permanent binding between an abstraction

Bridge - Applicability n n n To avoid a permanent binding between an abstraction and its implementation. Specific implementation can be selected at run-time To independently extend abstraction and implementation by subclassing. Different abstractions can be combined with different implementations. Changes in the implementation of an abstraction should have no impact on the clients. 18

Bridge - Applicability (Cont. ) n n To avoid proliferation of classes as shown

Bridge - Applicability (Cont. ) n n To avoid proliferation of classes as shown in the Motivation section. To share an implementation among multiple objects. e. g. Handle/Body from Coplien. 19

Bridge - Consequences : Decoupling interface and implementation. : Improved extensibility of both abstraction

Bridge - Consequences : Decoupling interface and implementation. : Improved extensibility of both abstraction and implementation class hierarchies. : Hiding implementation detail from client. 20

Bridge - Related Patterns n n Similar structure to Adapter but different intent. Abstract

Bridge - Related Patterns n n Similar structure to Adapter but different intent. Abstract Factory pattern can be used with the Bridge for creating objects. 21

Composite - Intent Compose objects into tree structure to represent part-whole hierarchies. Composite lets

Composite - Intent Compose objects into tree structure to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. 22

Composition - Motivation Classes for containers (e. g. . Rectangle) and primitives (e. g.

Composition - Motivation Classes for containers (e. g. . Rectangle) and primitives (e. g. . Line) are designed separately, but in many cases the client wants to treat the container and the primitive identically. The Composite pattern describes how to use recursive compositions so that clients don’t have to make this distinction. 23

Composite - Example <<Abstract>> Graphic 0. . * + draw () + add (g

Composite - Example <<Abstract>> Graphic 0. . * + draw () + add (g : Graphic) + remove (g : Graphic) + get. Child (int) Picture Line Text Rectangle draw (g : Graphic) add (g : Graphic) remove (g : Graphic) get. Child () draw(){ for all g in graphics g. draw() } 24

Composite - Structure <<Abstract>> Component + operation () + add () + remove ()

Composite - Structure <<Abstract>> Component + operation () + add () + remove () + get. Children () Leaf + operation () children 0. . * Composite + operation () + add () + remove () + get. Children () operation(){ for all g in graphics g. operation() } 25

Composite - Applicability n n To represent part-whole hierarchies of objects. To be able

Composite - Applicability n n To represent part-whole hierarchies of objects. To be able to ignore the differences between containers and primitives. Clients will treat all objects in the composite pattern uniformly. 26

Composite - Consequences : Clients can treat composite structures (containers) and individual objects (primitives)

Composite - Consequences : Clients can treat composite structures (containers) and individual objects (primitives) uniformly. : Composite makes it easier to add new kinds of components (leaf or composite). è Can make the design overly general. Sometimes you want the composite to have certain kinds of components. 27

Decorator - Intent Attach additional responsibility to an object dynamically. Decorators provide a flexible

Decorator - Intent Attach additional responsibility to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. 28

Decorator - Motivation Suppose that bank application must deal with many different varieties of

Decorator - Motivation Suppose that bank application must deal with many different varieties of customers (having checking only, checking and money-market fund and safe-deposit box etc. ) The combinatorial varieties are too much to handle using one big class. 29

Decorator - Example <<Abstract>> Account. Componet 1. . 1 + get. Balance () <<Abstract>>

Decorator - Example <<Abstract>> Account. Componet 1. . 1 + get. Balance () <<Abstract>> Account. Decortor Account. Info + get. Balance () get. Balance{ acc. Component. get. Balance(); } + get. Balance () Checking. Account + get. Balance () Saving. Account + get. Balance () + calculate. Balance () + get. Interest. Rate () MMFAccount + get. Balance () get. Balance(){ super. get. Balance(); calculate. Balance(); } 30

Decorator - Structure <<Abstract>> Component (from Composition) 1. . 1 + operation () <<Abstract>>

Decorator - Structure <<Abstract>> Component (from Composition) 1. . 1 + operation () <<Abstract>> Decorator Concrete. Component + operation () Concrete. Decorator. A Concrete. Decorator - added. State + operation () + added. Behavior () 31

Decorator - Applicability n n n To add responsibility to individual objects dynamically and

Decorator - Applicability n n n To add responsibility to individual objects dynamically and transparently, that is, without affecting other objects. For responsibility that can be withdrawn. When extension by subclassing is impractical. 32

Decorator - Consequences : More flexibility than static inheritance - responsibilities can be added

Decorator - Consequences : More flexibility than static inheritance - responsibilities can be added and removed at runtime. : Avoids feature-laden classes high up in the hierarchy. Responsibility is added on demand. è Lots of little objects. Decorator may produce a system with lots of little look-alike objects. The objects only differ in the way they are interconnected. These systems can be hard to learn and debug. 33

Decorator - Related Patterns n n Adapter gives an object a completely new interface,

Decorator - Related Patterns n n Adapter gives an object a completely new interface, whereas the Decorator changes an object’s responsibility but not its interface. Decorator can be used with a Composite to represent a whole-part relationship. 34

Next Pattern Presentation n More Structural Patterns u Facade u Flyweight u Proxy n

Next Pattern Presentation n More Structural Patterns u Facade u Flyweight u Proxy n Behavioral Patterns u Chain of Responsibility u Command u Interpreter u … And more 35