Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm.

Similar presentations


Presentation on theme: "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm."— Presentation transcript:

1 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm Shift, Inc. Software Factory Behaviora l Structural Lesson 6: Structural Patterns Object-Oriented Design Patterns

2 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-2 PS95&96-MEF-L13-2 Dr. M.E. Fayad Lesson Objectives oDiscuss the philosophy behind structural patterns oExplore detailed discussion of structural patterns oPresent the following patterns: Adapter Bridge

3 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-3 PS95&96-MEF-L13-3 Dr. M.E. Fayad Structural Patterns are Concerned with How Classes and Objects are Composed to Form Larger Structures.

4 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-4 PS95&96-MEF-L13-4 Dr. M.E. Fayad Structural Patterns Structural Class Patterns use inheritance to compose interfaces or implementations, such as Adapter Structural Object Patterns describe ways (not composing interfaces or implementations) to compose objects to realize new functionality, such as Proxy Structural Compound Patterns compose objects recursively to allow an open-ended number of additional attributes and services, such as Decorator and Composite

5 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-5 PS95&96-MEF-L13-5 Dr. M.E. Fayad Structural Patterns  Adapter Pattern Topics –Adapter Definition –Structures –Graphic Editor Example –Applicability –Participants and Collaborations –Trade-Offs –Related Patterns

6 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-6 PS95&96-MEF-L13-6 Dr. M.E. Fayad Adapter Pattern: Definition An Adapter makes one interface (the adaptee’s) conform to another, thereby providing a uniform abstraction of different interfaces. –A class Adapter accomplishes this by inheriting privately from an adaptee class –The Adapter then expresses its interface in terms of the adaptee’s

7 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-7 PS95&96-MEF-L13-7 Dr. M.E. Fayad Adapter Pattern: Definition (2) Adapter pattern converts the interface of one class to an interface expected by clients. The Adapter pattern lets classes work together that couldn’t otherwise because of non-conforming interfaces or representations.

8 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-8 PS95&96-MEF-L13-8 Dr. M.E. Fayad Adapter Pattern: Structure SpecificRequest() Client Adaptee SpecificRequest() Target Request() Adapter Request() (Implementation) A Class Adapter uses inheritance to adapt one interface to another.

9 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-9 PS95&96-MEF-L13-9 Dr. M.E. Fayad More Adapter Pattern: Structure adaptee->SpecificRequest() Client Adaptee SpecificRequest() Target Request() Adapter Request() A Object Adapter relies on object composition. adaptee

10 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-10 PS95&96-MEF-L13-10 Dr. M.E. Fayad Graphic Editor Example return text->GetExtent() TextShape Shape BoundingBox) line BoundingBox() DrawingEditor test BoundingBox() TextView GetExtent() an Adapter This diagram illustrates the object Adapter case. It shows how requests for the operation BoundingBox, declared in the Shape class, are converted to GetExtent requests defined in TextView. Because the interface of class TextShape conforms to that of Shape, the DrawingEditor need not to be modified to use this class.

11 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-11 PS95&96-MEF-L13-11 Dr. M.E. Fayad Use an Adapter when a client needs to use a class with an incompatible interface –Operation names or parameters are different –Argument types or representations don’t match Provides a consistent abstraction of unrelated classes Resolves unforeseen problem of two independently designed classes needed to work together Adapter Pattern: Applicability

12 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-12 PS95&96-MEF-L13-12 Dr. M.E. Fayad Adapter Pattern: Participants & Collaborations Target (Shape) –declares an interface used by a client Client (DrawingEditor) –an object defined in terms of Target’s interface Adaptee (TextView) –defines the specific operation to be adapted to abstract operations Adapter (TextShape) –implements the Target’s abstract operations in terms of the operations provided by the Adaptee. The Adapter conforms to the interface defined by the Target class Collaborations Clients call operations on an Adapter instance. Adapter invokes Adaptee operations that carry out the service requested. Collaborations Clients call operations on an Adapter instance. Adapter invokes Adaptee operations that carry out the service requested. Participants

13 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-13 PS95&96-MEF-L13-13 Dr. M.E. Fayad Class Adapter Trade-Offs Commits to a concrete Adapter class to adapt client to Adaptee Can override behavior of the adaptee Avoids indirection though pointers to the Adaptee. A class Adapter’s services are accessed directly through inheritance

14 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-14 PS95&96-MEF-L13-14 Dr. M.E. Fayad Object Adapter Trade-Offs Permits a single Adapter to work with many adaptees, as long as the Adaptee is an abstract class Makes it harder to override behavior. You must subclass the Adaptee and refer to the subclass in the Adapter Hides indirection to the Adaptee from client

15 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-15 PS95&96-MEF-L13-15 Dr. M.E. Fayad Issues in Using the Adapter Pattern 1. Supporting families of Adaptees –Adaptee should be an abstract class to achieve this flexibility 2. The degree of Adaption 3. The granularity of Adaption –Fine grained abstractors have a much wider applicability 4. Two-way Adapters –Let frameworks work with each other

16 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-16 PS95&96-MEF-L13-16 Dr. M.E. Fayad Adapter Pattern: Related Patterns Bridge –Bridges are designed so that both classes in the Bridge can evolve independently. Decorator –A Decorator enhances another object without changing its protocol. –A Decorator also supports recursive composition. Proxy –A Proxy is a representative for another object and does not change its protocol.

17 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-17 PS95&96-MEF-L13-17 Dr. M.E. Fayad Structural Patterns  Bridge Pattern Topics –Bridge Definition –Example Applications –Applicability &Structure –Participants & Collaborations –Benefits –Situations –Comments on Coding the Pattern –Related Patterns

18 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-18 PS95&96-MEF-L13-18 Dr. M.E. Fayad Bridge Pattern: Definition A Class/Object Structural Pattern Allows separate class hierarchies to work together even as they evolve independently Bridge

19 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-19 PS95&96-MEF-L13-19 Dr. M.E. Fayad Also referred to as the Handle/Body idiom The focus of Handle/Body is on Sharing (e.g., string, pointer ) The focus of Bridge is more on independent extensibility of abstraction and implementation Handle/Body idiom: –Allows decomposition of a complex abstraction into smaller, more manageable classes, and –Allows sharing of a single resource by multiple objects that control access to it More Bridge Pattern: Definition Client Body Class Handle Class - Outer “skin” class that is visible - Fields requests & dispatches them to body - Presents intuitive/ uniform interface. - Inner class where implementation details are buried. - Application intelligence resides here.

20 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-20 PS95&96-MEF-L13-20 Dr. M.E. Fayad Bridge Pattern: Structure impl->OperationImpl(); Abstraction Operation() Implementor OperationImpl() ConcreteImplementorA Client RefinedAbstraction OperationImpl() ConcreteImplementorB impl

21 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-21 PS95&96-MEF-L13-21 Dr. M.E. Fayad Windowing Example WindowWindowRep Bridge rep Application IconWindow DialogWindow XWindowRep SunWindowRep MacWindowRep

22 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-22 PS95&96-MEF-L13-22 Dr. M.E. Fayad VLSI Example CellCellImplementation Bridge LogicalCell PhysicalCell Behavior NetList Schematic MaskLayout 1 N

23 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-23 PS95&96-MEF-L13-23 Dr. M.E. Fayad Operating System Simulator Example Process Processor Bridge User System PowerPC Sparc Alpha PA-RISC N 1

24 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-24 PS95&96-MEF-L13-24 Dr. M.E. Fayad Bridge Pattern: Applicability When you want to avoid a permanent binding between an abstraction and its implementation Both abstraction and implementations are extensible by subclasses. Bridge pattern supports the combination of different abstractions and implementations When you have a proliferation of classes When you want to share an implementation among multiple objects by, say, reference counting and this fact should be hidden from the client

25 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-25 PS95&96-MEF-L13-25 Dr. M.E. Fayad Bridge Pattern: Participants & Collaborations Abstraction (Window) –defines the abstraction’s interface –maintains a reference to an Implementor RefinedAbstraction (IconWindow) –extends the interface defined by abstraction Implementor (WindowRep) –defines the interface for implementation class ConcreteImplementor (XWindowRep, SunWindowRep) –implements implementor interface & defines its concrete representation Collaborations The abstraction forwards client requests to the Implementor object it references. Collaborations The abstraction forwards client requests to the Implementor object it references. Participants

26 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-26 PS95&96-MEF-L13-26 Dr. M.E. Fayad Bridge Pattern: Benefits Allows separation of interface and implementation –Handle caters to the needs of application programmers (logical notion) –Body caters to the need of implementation systems Client code does not have to commit to a concrete implementation –Implementation is selected at run-time –Avoids permanent binding between abstraction and implementation Both the interface and implementation are extensible by subclassing Avoids proliferation of classes (or nested generalization) where individual class hierarchies are kept small and stable. Hides representation sharing from clients. –Allows implementation to be shared by multiple objects through reference counting without clients having to be aware of this fact

27 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-27 PS95&96-MEF-L13-27 Dr. M.E. Fayad Use Bridge Pattern to Handle Multiple Disjoint Hierarchies by Avoiding Multiple Inheritance Example Taken from OMT [Rumbaugh et al. 91] Employee pension status pay status Hourly Employee Salaried Employee Exempt Employee Vested Employee Unvested Employee Vested Hourly Employee

28 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-28 PS95&96-MEF-L13-28 Dr. M.E. Fayad Multiple Inheritance is GOOD –Greater power in specifying classes Multiple Inheritance is BAD –Decreased opportunity for reuse –Loss of conceptual and implementation simplicity Multiple Inheritance Support May Be UNAVAILABLE Use Bridge Pattern to Handle Multiple Disjoint Hierarchies by Avoiding Multiple Inheritance (2

29 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-29 PS95&96-MEF-L13-29 Dr. M.E. Fayad Solution I: Class Based Form (Nested Generalization) Uses concept of inheritance to bridge two hierarchies Works when the number of combinations is small Inflexible; awkward when large quantities of code needs duplication Uses concept of inheritance to bridge two hierarchies Works when the number of combinations is small Inflexible; awkward when large quantities of code needs duplication Employee pension status pay status Hourly Employee Salaried Employee Exempt Employee Exempt Vested Employee Exempt Unvested Employee Hourly Vested Employee Hourly Unvested Employee Salaried Vested Employee Salaried Unvested Employee

30 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-30 PS95&96-MEF-L13-30 Dr. M.E. Fayad Solution II: Object Based Form (Single Inheritance & Delegation) Allows run-time changes to implementation Incurs slightly higher storage (because of reference to implementation class) and run-time cost (due to delegation) Allows run-time changes to implementation Incurs slightly higher storage (because of reference to implementation class) and run-time cost (due to delegation) Employee pension status pay status Hourly Employee Salaried Employee Exempt Employee Vested Employee Unvested Employee EmployeePension Bridge

31 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-31 PS95&96-MEF-L13-31 Dr. M.E. Fayad Related Patterns Adapter Pattern –An Adapter can act as a special-purpose Bridge between two existing interfaces –An Adapter doesn’t support varying multiple abstractions independently –An Adapter has no participant comparable to an Implementor for providing an abstract interface to the Adaptee Abstract Factory Pattern –An Abstract Factory can create and configure a particular Bridge

32 Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-32 PS95&96-MEF-L13-32 Dr. M.E. Fayad Discussion Questions Mark (T) for true or (F) for false ( ) 1. A class Adapter uses object composition to adapt one interface to another while an object Adapter relies on inheritance. ( ) 2. Two-way adapters let frameworks work with each other. ( ) 3. There is a lot of flexibility with adapters that have fine- grained interfaces. ( ) 4. In the Bridge pattern, the reference to the Implementor is kept in Abstraction base class, but in the Adapter pattern, the reference to Adaptee is in the Adapter class. ( ) 5. An Abstract Factory can create and configure a particular Bridge and an Adapter can act as a special-purpose Bridge between two existing interfaces. ( ) 6. The Bridge pattern allows separate class hierarchies to work together even as they evolve independently. ( ) 7. The Bridge pattern hides representation sharing from client.


Download ppt "Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creationa l Paradigm."

Similar presentations


Ads by Google