24
• INTENT » DECOUPLE AN ABSTRACTION FROM ITS IMPLEMENTATION SO THAT THE TWO CAN VARY INDEPENDENTLY » SEPARATE THE ABSTRACTION AND ITS IMPLEMENTATION AND HAVE SEPARATE INHERITANCE STRUCTURE FOR BOTH. Design Pattern – Bridge (Structural) • Also known as » Handle / Body by Shahriar Hyder Oct 05, 2011

Bridge Design Pattern

Embed Size (px)

DESCRIPTION

Bridge Design Pattern - Structural Pattern

Citation preview

Page 1: Bridge Design Pattern

• I N T E N T

» D E C O U P L E A N A B S T R A C T I O N F R O M I T S I M P L E M E N TAT I O N S O T H AT T H E T W O C A N VA RY I N D E P E N D E N T LY

» S E PA R AT E T H E A B S T R A C T I O N A N D I T S I M P L E M E N TAT I O N A N D H AV E S E PA R AT E I N H E R I TA N C E S T R U C T U R E F O R B O T H .

Design Pattern – Bridge(Structural)

• Also known as» Handle / Body

by

Shahriar HyderOct 05, 2011

Page 2: Bridge Design Pattern

Motivation

Write a program that will draw rectangles with either of two drawing programs:

drawing program 1 (DP1)

drawing program 2 (DP2)

Page 3: Bridge Design Pattern

DP 1 DP 2

draw a line draw_a_line( x1, y1, x2, y2) drawline( x1, x2, y1, y2)

draw a circle draw_a_circle( x, y, r) drawcircle( x, y, r)

The collection (the client of rectangles) doesn’t want to worry about what type of drawing program it should use.

Example

Page 4: Bridge Design Pattern
Page 5: Bridge Design Pattern

Cont..

new requirement:

asked to support a new shape - circle

adding another level, called Shape, which derives the Rectangle and Circle class. ?

Page 6: Bridge Design Pattern
Page 7: Bridge Design Pattern

new problems arise …

if I get another drawing program – DP3• I will have 6 different kinds of Shapes (two Shape concepts

times three drawing programs).

if I get another type of Shape• I will have nine different types of Shapes (three Shape

concepts times three drawing programs).

class explosion!

DP:Drawing Program S:Shapes Need ≈DP*S new classes Want ≈DP+S new classes The new classes are hard wired for each type of Drawing Programs

Page 8: Bridge Design Pattern

Problem

“Hardening of the software arteries” has occurred by using sub-classing of an abstract base class to provide alternative implementations. This locks in compile-time binding between interface and implementation. The abstraction and implementation cannot be independently extended or composed.

Page 9: Bridge Design Pattern

Try another alternate hierarchy

redundancy still there !

Page 10: Bridge Design Pattern

Step 1. Identify what is varying

Page 11: Bridge Design Pattern

Step 2. Represent the variations

Page 12: Bridge Design Pattern

Step3. Tie the class together

Page 13: Bridge Design Pattern

Step4. Expanding the design

Page 14: Bridge Design Pattern

Bridge – Structure

Page 15: Bridge Design Pattern

Bridge – Participants

• Abstraction» Defines the abstractions interface

» Maintains a reference to an object of type implementor

• RefinedAbstraction» Extends the interface defined by

Abstraction

Page 16: Bridge Design Pattern

Bridge – Participants – 2

• Implementer » Defines the interface for implementation classes

> Can be different from the Abstraction interface

– Implementer provides primitive operations – Abstraction provides higher-level operations

• ConcreteImplementer » Implements the Implementer interface » Defines its concrete implementation

Page 17: Bridge Design Pattern

Demo Time!

Page 18: Bridge Design Pattern

Bridge – Applicability

• The normal method of dealing with an abstraction having several implementations is through inheritance.

• Avoid permanent binding between an abstraction and its implementation » Especially if selection or switching of implementation is at run-time rather than design time.

• Both abstractions and implementations should be extensible by sub-classing

• Changes in implementation should have no impact on clients• Share an implementation among multi objects and this fact

should be hidden from the client• You have a proliferation of classes resulting from a coupled

interface and numerous implementations• You need to map orthogonal class hierarchies.

Page 19: Bridge Design Pattern

The Bridge pattern is useful when you have an abstraction that has different implementations. It allows the abstraction and the implementation to vary independently of each other.

• Find what varies and encapsulate it.

• Favor composition over inheritance.

Bridge – Applicability Summary

Page 20: Bridge Design Pattern

Collaborations

Abstraction forwards client requests to its Implementor object.

Page 21: Bridge Design Pattern

Bridge – Consequences

• Decouples interface and implementation» Can configure implementation to use at runtime» Encourages better structure through layering

> The client only has to know about Abstraction

and Implementer

• Improved extensibility» Extend in Abstraction and Implementerindependently

• Hide implementation details from clients

Page 22: Bridge Design Pattern

Implementation

Ditto from GoF Please!

Page 23: Bridge Design Pattern

Bridge – Related Patterns

• Abstract Factory can create and configure a particular Bridge

• Adapter is geared toward making unrelated classes work together» Usually applied after systems are designed» Bridge is used during design

• Structural difference: Bridge can abstract a complex entity from its implementation; Adapter only abstracts a single interface

• A bridge is by design. An adaptor is not. An adaptor is a patch. A bridge is put in place on purpose.

Page 24: Bridge Design Pattern

Thank You