Docsity

The Bridge Pattern: Decoupling Abstraction and Implementation in Software Design - Prof. J, Papers of Computer Science

Toggle sidebar
bg1
CS 350
CS 350 –
–Software Design
Software Design
The Bridge Pattern
The Bridge Pattern –
–Chapter 10
Chapter 10
Most powerful pattern so far.
Most powerful pattern so far.
Gang of Four Definition: Decouple an abstraction from its implem
Gang of Four Definition: Decouple an abstraction from its implementation so that the two can
entation so that the two can
vary independently.
vary independently.
What does this really mean?
What does this really mean?
Let
Let’
’s dissect it.
s dissect it.
Decouple means to have things behave independently for each othe
Decouple means to have things behave independently for each other.
r.
Abstraction is how different things are related to each other co
Abstraction is how different things are related to each other conceptually.
nceptually.
I personally prefer Google
I personally prefer Google’
’s definition: a concept or idea not associated with any specific
s definition: a concept or idea not associated with any specific
instance
instance
Implementations ?
Implementations ? –
–Build concrete abstractions?
Build concrete abstractions?
So what does it mean to separate the abstraction from the implem
So what does it mean to separate the abstraction from the implementation?
entation?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download The Bridge Pattern: Decoupling Abstraction and Implementation in Software Design - Prof. J and more Papers Computer Science in PDF only on Docsity!

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

Most powerful pattern so far.Most powerful pattern so far.Gang of Four Definition: Decouple an abstraction from its implementation so that the two canGang of Four Definition: Decouple an abstraction from its implem

entation so that the two can

vary independently.vary independently.What does this really mean?What does this really mean?LetLet’’s dissect it.s dissect it.Decouple means to have things behave independently for each other.Decouple means to have things behave independently for each othe

r.

Abstraction is how different things are related to each other conceptually.Abstraction is how different things are related to each other co

nceptually.

I personally prefer Google’I personally prefer Google

’s definition: a concept or idea not associated with any specifics definition: a concept or idea not associated with any specific

instanceinstanceImplementations ?Implementations? –

– Build concrete abstractions?Build concrete abstractions?

So what does it mean to separate the abstraction from the implementation?So what does it mean to separate the abstraction from the implem

entation?

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

Truly the best way to learn is by an exampleTruly the best way to learn is by an exampleSuppose we are writing a program that will draw rectangles for either of two drawingSuppose we are writing a program that will draw rectangles for e

ither of two drawing

programs (DP1 and DP2).programs (DP1 and DP2).We will know which we are using at the instantiation of the rectangle.We will know which we are using at the instantiation of the rect

angle.

A rectangle is defined as a pair of two points:A rectangle is defined as a pair of two points:

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

We do not want the code that draws the rectangles to worry aboutWe do not want the code that draws the rectangles to worry about what type of drawing

what type of drawing

program it should use.program it should use.Since the rectangles are told at the time of instantiation what drawing program they use, weSince the rectangles are told at the time of instantiation what

drawing program they use, we

could have two different kinds of Drawing objects, one that works of DP1 and one on DP2.could have two different kinds of Drawing objects, one that work

s of DP1 and one on DP2.

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

Use an abstract class to represent a Rectangle.Use an abstract class to represent a Rectangle.The only difference between DP1 and DP2’The only difference between DP1 and DP

’s rectangles are the implementation of thes rectangles are the implementation of the

drawline method.method.drawlineV1Rectangle is implemented by having a reference to a DP1 object that uses theV1Rectangle is implemented by having a reference to a DP1 object

that uses the draw_a_line

draw_a_line

method.method.V2Rectangle is implemented by having a reference to a DP2 object that uses theV2Rectangle is implemented by having a reference to a DP2 object

that uses the drawline

drawline

method.method.Once a Rectangle is instantiated I no longer have to worry about which it uses.Once a Rectangle is instantiated I no longer have to worry about

which it uses.

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

As we add shapes it is intuitive to develop a class for each shaAs we add shapes it is intuitive to develop a class for each shape/drawing system

pe/drawing system

combination that uses the appropriate drawing system.combination that uses the appropriate drawing system.

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

abstract^ class

Shape^ { abstract^ class

Shape^ { abstract public^ void^ draw();abstract public^ void^ draw(); }} abstract^ public

class^ Circle

extends^ Shape

abstract^ public

class^ Circle

extends^ Shape

protected^ double

_x,^ _y,^ _r; protected^ double

_x,^ _y,^ _r; public^ Circle

(double^ x,^

double^ y,^ double

r)^ {

public^ Circle

(double^ x,^ double^ y,^ double

r)^ {

_x^ =^ x;^ _y^ = y;^ _r^ =^ r; _x^ =^ x;^ _y^ = y;^ _r^ =^ r; }} public^ void public^ void^ draw()^ {^ draw()^ { drawCircledrawCircle();(); }} abstract^ protected

void abstract^ protected

void^ drawCircle();drawCircle

public^ class V1Circle^ extends

Circle{ public^ class V1Circle^ extends

Circle{ public^ V1Circle(double

x,^ double^ y,

double^ r)^ { public^ V1Circle(double

x,^ double^ y,

double^ r)^ { super(x,y,r););super(x,y,r}} protected^ voidprotected^ void^

drawCircledrawCircle()

DP1.draw_a_circle(_x,

_y,^ _r); DP1.draw_a_circle(_x,

_y,^ _r); }} }}

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

Observe how a rectangle is drawn:Observe how a rectangle is drawn:

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

While intuitive, this method leads to a class explosion.While intuitive, this method leads to a class explosion.The abstraction (Shape) and the implementation (drawing programs) are tightly coupled.The abstraction (Shape) and the implementation (drawing programs

) are tightly coupled.

Each type of shape must know what kind of drawing program it is using.Each type of shape must know what kind of drawing program it is

using.

We need to separate the variations in abstraction from the variation in implementation.We need to separate the variations in abstraction from the varia

tion in implementation.

Remember, this is the idea behind the Bridge Pattern,Remember, this is the idea behind the Bridge Pattern, “

“to decouple the abstraction from theto decouple the abstraction from the

implementation, so the two can vary independently.”implementation, so the two can vary independently.

Abstraction 1Abstraction 1

Implementation AImplementation A

Abstraction 2Abstraction 2

Implementation BImplementation B

Abstraction 3Abstraction 3

Implementation CImplementation C

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

Is the problem inheritance?Is the problem inheritance?Let’’s try another form.s try another form.Let

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

LetLet’’s repeat our mantras repeat our mantraFind what varies and encapsulate itFind what varies and encapsulate itFavor aggregation over inheritance.Favor aggregation over inheritance.Let’’s start by identify what is varying.s start by identify what is varying.Let•^ Different types of shapes•Different types of shapesDifferent types of drawing programs••^ Different types of drawing programs

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

We also must represent the specific variations that are present.We also must represent the specific variations that are present.A shape has a rectangle and circle.A shape has a rectangle and circle.A drawing program has a program based on DP1 and one based on DP2A drawing program has a program based on DP1 and one based on DP

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

How do these classes relate to one another?How do these classes relate to one another?Two possibilities:Two possibilities:Drawing Program uses ShapeDrawing Program uses ShapeTo accomplish this, drawing programs would need to know something about shapes inTo accomplish this, drawing programs would need to know somethin

g about shapes in

general.general.This violates fundamental principles of objects. Objects are responsible only for themselves.This violates fundamental principles of objects. Objects are res

ponsible only for themselves.

This violates encapsulation.This violates encapsulation.Shape uses the Drawing ProgramShape uses the Drawing ProgramShapes do not need to know what type of drawing object they used, it would just refer to theShapes do not need to know what type of drawing object they used

, it would just refer to the

Drawing class. Shapes are responsible for controlling the drawinDrawing class. Shapes are responsible for controlling the drawing.

g.

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

Figure with the missing relationship to the actual drawing progrFigure with the missing relationship to the actual drawing programs

ams

CS 350 –– Software DesignCS 350

Software Design

The Bridge Pattern –The Bridge Pattern

– Chapter 10Chapter 10

We now have a separation of the abstraction from the implementatWe now have a separation of the abstraction from the implementation

ion

Also have One Rule One PlaceAlso have One Rule One Place