Vous êtes sur la page 1sur 22

Bridge Design Pattern

Presented By:
Atif Saeed
Misbah Niazi
Department Of Computer Science UET Taxila 1
Bridge Design Pattern
Decouple an abstraction from its implementation so that the
two can vary independently

The Bridge pattern is designed to separate a class's interface
from its implementation so you can vary or replace the
implementation without changing the client code.

Also known as Handle/Body pattern
Department Of Computer Science UET Taxila 2
Motivation

Inheritance hierarchies can be hierarchies of abstractions
(concepts) or hierarchies of implementations.
Mixing concepts with implementations can lead to hierarchies
that are confusing and difficult to maintain.
It could lead to an exponential number of subclasses and soon
we will have an exploding class hierarchy.

Department Of Computer Science UET Taxila 3
Applicability

Avoid a permanent binding between an abstraction and its
implementation
Both the abstractions and their implementation should be
extensible by sub classing.
Changes in the implementation of an abstraction do not impact
the clients
Share an implementation among multi objects and this facts
should be hidden from the client
E.g.: Windows
Department Of Computer Science UET Taxila 4
Participants
The participants in the Bridge pattern are:

Abstraction
defines the abstraction's interface
maintains a reference to the Implementor
forwards requests to the Implementor (collaboration)

Refined Abstraction
extends abstraction interface

Department Of Computer Science UET Taxila 5

Contd
Implementor
defines interface for implementations

Concrete Implementor
implements Implementer interface, i.e. defines an
implementation


Department Of Computer Science UET Taxila 6
Design Pattern Structure
Department Of Computer Science UET Taxila 7
The Bridge Pattern: example 1




The shape interface provides an abstract interface for handling
shapes.
The Drawing Program is used by both Rectangle and Circle to
implement the Draw() method.
Abstraction and implementation are coupled.
Department Of Computer Science UET Taxila 8
Contd
Department Of Computer Science UET Taxila 9
Contd
Update design to be scalable by separating abstraction from
implementation.
OO Principle: find what varies and encapsulate it
The abstract classes Shape and Drawing encapsulate the
specific variations.


Department Of Computer Science UET Taxila 10
Contd
OO Principle: use composition instead of inheritance.
Shape uses Drawing.


Department Of Computer Science UET Taxila 11
Solution using Bridge
Department Of Computer Science UET Taxila 12
Contd
Now Addition of further dependencies
Shape contains an abstract interface to Drawing.
The derivatives of Shape no longer dependent on the drawing
program.
Drawing interface uses adaptors to wrap Drawing DP1 and
DP2.
Note that the solution presented integrates the Adapter pattern
with the Bridge pattern. This is caused by the fact that the
interfaces of DP1 and DP2 have to be adapted to the interface
needed.
Department Of Computer Science UET Taxila 13
Example:2
Department Of Computer Science UET Taxila 14
Example:2
Here's the another example of remote control implemented. First, we have
our TV implementation interface:
1.//Implementor
2.public interface TV
3.{
4.public void on();
5.public void off();
6.public void tuneChannel(int channel);
7.}
And then we create two specific implementations - one for Sony and one
for Sanyo:

Department Of Computer Science UET Taxila 15
Contd
01.//Concrete Implementor
02.public class Sony implements TV
03.{
04.public void on()
05.{
06.//Sony specific on
07.}
08.
09.
10.public void off()
11.{
12.//Sony specific off
13.}
14.
15.public void tuneChannel(int channel);
16.{
17.//Sony specific tuneChannel
Department Of Computer Science UET Taxila 16
Contd
These classes deal with the specific implementations of the TV from each vendor. Now, we
create a remote control abstraction to control the TV:
01.//Abstraction
02.public abstract class RemoteControl
03.{
04.private TV implementor;
05.public void on()
05.{
06.implementor.on();
07.}
08.public void off()
09.{
10.implementor.off();
11.public void setChannel(int channel)
12.{
13.implementor.tuneChannel(channel);

Department Of Computer Science UET Taxila 17
Contd
01.//Refined abstraction
02.public class ConcreteRemote extends RemoteControl
03.{
04.private int currentChannel;
05.public void nextChannel()
06.{
07.currentChannel++;
08.setChannel(currentChannel);
9.}
10.public void prevChannel()
11.{
12.currentChannel--;
13.setChannel(currentChannel);
14.}
15.}

Department Of Computer Science UET Taxila 18
Pros & Cons
Pros
Decoupling of the implementation from the interface improved
extensibility of classes
Additional capability for hiding implementation details from
clients
Best leveraged as a pure design-time pattern
Cons (Implementation Issues)
Abstractions that have only one implementation
Creating the right Implementor.
Sharing implementors,Use of multiple inheritance.
Department Of Computer Science UET Taxila 19
Adapter vs Bridge
Similarities:
Both used to hide the details of the underlying
implementation.
Difference:
In adapter Pattern, Adapts existing classes to an expected
interface. It is geared towards making unrelated components
work together
Applied to systems after theyre designed (reengineering,
interface engineering).
Department Of Computer Science UET Taxila 20
Contd
A bridge, on the other hand, creates an abstract Interface to
be implemented by multiple classes ,it keeps the
implementations separate from the interface. The new thing
is the interface.
It is used up-front in a design to let abstractions and
implementations vary independently.
Green field engineering of an extensible system
Structural difference: Bridge can abstract a complex entity
from its implementation; Adapter only abstracts a single
interface

Department Of Computer Science UET Taxila 21

Vous aimerez peut-être aussi