Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.

Similar presentations

Presentation on theme: "1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator."— Presentation transcript:

1 1 Structural Design Patterns - Neeraj Ray

2 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator

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

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

5 5 Adapter - Example Customer + addAcc () Cust + addAccount () > Client BankCust + addAccount () customer.addAcc()

6 6 Object Adapter - Structure Target + request () > Client adaptee.specificRequest() Adapter + request () Adaptee + specificRequest ()

7 7 Class Adapter - Structure Client Target + request () > Adaptee + specificRequest () Adapter + request () specificRequest()

8 8 Adapter - Applicability n To use an existing class, and its interface does not match the one you need. n To create a reusable class that cooperates with classes that don’t have compatible interfaces. n (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.

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

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

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

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

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

14 14 Bridge - Motivation (Cont.) Window XWindow PMWindow IconWindow XWindowIconPMWindowIcon

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

16 16 Bridge - Example XWindow drawLine () drawText () PMWindow drawLine () drawText () IconWindow WindowImpl drawLine () drawText () > Window drawRect () > impl.drawLine()

17 17 Bridge - Structure ConcreteImplentorB implOperation () RefinedAbstraction ConcreteImplementorA implOperation () Implementor implOperation () > Abstraction operation () > impl.implOperation()

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

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

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

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

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

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

24 24 Composite - Example 0..* LineText Rectangle Graphic + draw () + add (g : Graphic) + remove (g : Graphic) + getChild (int) > Picture draw (g : Graphic) add (g : Graphic) remove (g : Graphic) getChild () 0..* draw(){ for all g in graphics g.draw() }

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

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

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

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

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

30 30 Decorator - Example getBalance{ accComponent.getBalance();} AccountInfo + getBalance () CheckingAccount + getBalance () SavingAccount + getBalance () + calculateBalance () + getInterestRate () MMFAccount + getBalance () AccountComponet + getBalance () > AccountDecortor + getBalance () > 1..1 getBalance(){ super.getBalance(); calculateBalance();}

31 31 Decorator - Structure Decorator + operation () > ConcreteComponent + operation () ConcreteDecoratorA - addedState + operation () ConcreteDecorator + operation () + addedBehavior () Component (from Composition) + operation () > 1..1

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

33 33 Decorator - Consequences : More flexibility than static inheritance - responsibilities can be added and removed at run- time. : 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.

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

35 35 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

Download ppt "1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator."

Similar presentations

Ads by Google