copyright © 1995-2004 active frameworks inc. - all rights reserved - v2.0structural patterns - page...

32
Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-1 PS95&96-MEF-L13-1 Dr. M.E. Fayad Creation al Paradigm Shift, Inc. Software Factory Behavior al Structur al Lesson 6: Structural Patterns Object- Object- Oriented Oriented Design Design Pattern Pattern s s

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-1

PS95&96-MEF-L13-1Dr. M.E. Fayad

Creational

Paradigm Shift, Inc.Software Factory

Behavioral

Structural

Lesson 6:Structural Patterns

Lesson 6:Structural Patterns

Object-Object-OrientedOriented

DesignDesign PatternPatternss

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-2

PS95&96-MEF-L13-2Dr. M.E. Fayad

Lesson ObjectivesLesson Objectives

Objectives

Discuss the philosophy behind structural patterns

Explore detailed discussion of structural patterns

Present the following patterns:

Adapter

Bridge

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-3

PS95&96-MEF-L13-3Dr. M.E. Fayad

Structural Patterns are Concerned with How Classes and Objects are Composed to Form

Larger Structures.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-4

PS95&96-MEF-L13-4Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-5

PS95&96-MEF-L13-5Dr. M.E. Fayad

Structural Patterns

Adapter Pattern

• Topics– Adapter Definition

– Structures

– Graphic Editor Example

– Applicability

– Participants and Collaborations

– Trade-Offs

– Related Patterns

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-6

PS95&96-MEF-L13-6Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-7

PS95&96-MEF-L13-7Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-8

PS95&96-MEF-L13-8Dr. 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.

A Class Adapter uses inheritance to adapt one interface to another.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-9

PS95&96-MEF-L13-9Dr. M.E. Fayad

More Adapter Pattern: Structure

adaptee->SpecificRequest()

Client

Adaptee

SpecificRequest()

Target

Request()

Adapter

Request()

A Object Adapter relies on object composition.A Object Adapter relies on object composition.

adaptee

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-10

PS95&96-MEF-L13-10Dr. 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.

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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-11

PS95&96-MEF-L13-11Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-12

PS95&96-MEF-L13-12Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-13

PS95&96-MEF-L13-13Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-14

PS95&96-MEF-L13-14Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-15

PS95&96-MEF-L13-15Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-16

PS95&96-MEF-L13-16Dr. 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.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-17

PS95&96-MEF-L13-17Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-18

PS95&96-MEF-L13-18Dr. M.E. Fayad

Bridge Pattern: Definition

• A Class/Object Structural Pattern

• Allows separate class hierarchies to work together even as they evolve independently

Bridge

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-19

PS95&96-MEF-L13-19Dr. 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 ClassHandle Class

- Outer “skin” class that is visible- Fields requests & dispatches them to body- Presents intuitive/uniform interface.

- Inner class whereimplementation detailsare buried.- Application intelligenceresides here.

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-20

PS95&96-MEF-L13-20Dr. M.E. Fayad

Bridge Pattern: Structure

impl->OperationImpl();

Abstraction

Operation()

Implementor

OperationImpl()

ConcreteImplementorA

Client

RefinedAbstraction

OperationImpl()OperationImpl()

ConcreteImplementorB

impl

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-21

PS95&96-MEF-L13-21Dr. M.E. Fayad

Windowing Example

Window WindowRep

Bridge

rep

Application

IconWindow

DialogWindow

XWindowRep

SunWindowRep

MacWindowRep

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-22

PS95&96-MEF-L13-22Dr. M.E. Fayad

VLSI Example

Cell CellImplementation

Bridge

LogicalCell

PhysicalCell

Behavior

NetList

Schematic

MaskLayout

1 N

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-23

PS95&96-MEF-L13-23Dr. M.E. Fayad

Operating System Simulator Example

Process

Processor

Bridge

User

System

PowerPC

Sparc

Alpha

PA-RISC

N 1

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-24

PS95&96-MEF-L13-24Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-25

PS95&96-MEF-L13-25Dr. 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

CollaborationsThe abstraction forwards client requests to the Implementor object it references.

CollaborationsThe abstraction forwards client requests to the Implementor object it references.

Participants

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-26

PS95&96-MEF-L13-26Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-27

PS95&96-MEF-L13-27Dr. M.E. Fayad

Use Bridge Pattern to Handle Multiple Disjoint Hierarchies by Avoiding Multiple Inheritance

• Example Taken from OMT [Rumbaugh et al. 91]

Employeepensionstatus

paystatus

HourlyEmployee

SalariedEmployee

ExemptEmployee

VestedEmployee

UnvestedEmployee

VestedHourlyEmployee

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-28

PS95&96-MEF-L13-28Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-29

PS95&96-MEF-L13-29Dr. 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

HourlyEmployee

SalariedEmployee

ExemptEmployee

ExemptVestedEmployee

ExemptUnvestedEmployee

HourlyVestedEmployee

HourlyUnvestedEmployee

SalariedVestedEmployee

SalariedUnvestedEmployee

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-30

PS95&96-MEF-L13-30Dr. 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 statuspay status

HourlyEmployee

SalariedEmployee

ExemptEmployee

VestedEmployee

UnvestedEmployee

EmployeePension

Bridge

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-31

PS95&96-MEF-L13-31Dr. 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

Copyright © 1995-2004 Active Frameworks Inc. - All Rights Reserved - V2.0 Structural Patterns - Page L6-32

PS95&96-MEF-L13-32Dr. M.E. Fayad

Discussion QuestionsDiscussion 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.