0% found this document useful (0 votes)
21 views17 pages

Bridge Pattern

Uploaded by

abinaya sriram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views17 pages

Bridge Pattern

Uploaded by

abinaya sriram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

DESIGN PATTERNS

Bridge Pattern
THE BRIDGE PATTERN

Java Design Patterns


Bridge is a structural design pattern that lets you split a large class or a set of
closely related classes into two separate hierarchies—abstraction and
implementation—which can be developed independently of each other.

2
THE BRIDGE PATTERN
Say you have a geometric Shape class with a pair of subclasses: Circle and

Java Design Patterns


Square. You want to extend this class hierarchy to incorporate colors, so you plan
to create Red and Blue shape subclasses. However, since you already have two
subclasses, you’ll need to create four class combinations such as BlueCircle and
RedSquare.

3
THE BRIDGE PATTERN
Adding new shape types and colors to the hierarchy will grow it exponentially.

Java Design Patterns


For example, to add a triangle shape you’d need to introduce two subclasses, one
for each color. And after that, adding a new color would require creating three
subclasses, one for each shape type. The further we go, the worse it becomes

This problem occurs because we’re trying to extend the shape classes in two independent dimensions:

4
by form and by color. That’s a very common issue with class inheritance. .
THE BRIDGE PATTERN
The Bridge pattern attempts to solve this problem by switching from inheritance

Java Design Patterns


to the object composition.
What this means is that you extract one of the dimensions into a separate class
hierarchy, so that the original classes will reference an object of the new
hierarchy, instead of having all of its state and behaviors within one class.

5
THE BRIDGE PATTERN
:: Building the Bridge Pattern - I

Java Design Patterns


6
THE BRIDGE PATTERN
:: Building the Bridge Pattern - I

Java Design Patterns


Following this approach, we can extract the color-related code
into its own class with two subclasses: Red and Blue.

The Shape class then gets a reference field pointing to one of


the color objects.

7
Now the shape can delegate any color-related work to the linked
color object.
That reference will act as a bridge between the Shape and Color
classes. From now on, adding new colors won’t require changing
the shape hierarchy, and vice versa.
THE BRIDGE PATTERN
:: Building the Bridge Pattern - I

Java Design Patterns


8
THE BRIDGE PATTERN
:: Building the Bridge Pattern - I

Java Design Patterns


9
THE BRIDGE PATTERN

Java Design Patterns


The base remote control class declares a reference field that links it
with a device object. All remotes work with the devices via the
general device interface, which lets the same remote support
multiple device types.

You can develop the remote control classes independently from the

10
device classes. All that’s needed is to create a new remote subclass.
For example, a basic remote control might only have two buttons,
but you could extend it with additional features, such as an extra
battery or a touchscreen.

The client code links the desired type of remote control with a
specific device object via the remote’s constructor.
THE BRIDGE PATTERN
:: Building the Bridge Pattern - I

Java Design Patterns


Following the example from the previous slide, suppose that we need to make
some changes in the way these lists display data. So, rather than deriving new
classes whenever we need to change these displays further, let’s build a single
bridge that does this work for us.

11
THE BRIDGE PATTERN
:: Definition & Applicability

Java Design Patterns


The Bridge pattern is used to separate the interface of class from its
implementation, so that either can be varied separately.
 At first sight, the bridge pattern looks much like the Adapter pattern, in
that a class is used to convert one kind of interface to another.
However, the intent of the Adapter pattern is to make one or more

12
classes’ interfaces look the same as that of a particular class.
 The Bridge pattern is designed to separate a class’ interface from its
implementation, so that you can vary or replace the implementation
without changing the client code.

Example:
Suppose that we have a program that displays a list of products in a
window. The simplest interface for that display is a simple JList box. But,
once a significant number of products have been sold, we may want to
display the products in a table along with their sales figures.
THE BRIDGE PATTERN
:: Building the Bridge Pattern - II

Java Design Patterns


We want the bridge class to return an appropriate visual component, so we will
extend the Java JSrollPane class:

public class ListBridge extends JScrollPane

13
{…}

When we design a bridge class, we have to decide how the bridge will determine
which of the several classes it will instantiate. This could be decided based on the
values or quantity of data to be displayed, or based on some simple constants.
Here we define the two constants inside the ListBridge class:

static public final int TABLE = 1, LIST = 2;


THE BRIDGE PATTERN
:: Building the Bridge Pattern - III

Java Design Patterns


The constructor of the ListBridge class:

public ListBridge(Vector v, int table_type)


{

14
Vector sort = sortVector(v); //sort the vector
if (table_type == LIST)
getViewport().add(makeList(sort)); //make table

if (table_type == TABLE)
getViewport().add(makeTable(sort)); //make list
}
THE BRIDGE PATTERN
:: Building the Bridge Pattern - IV

Java Design Patterns


We can use the JTable and JList classes directly without modification and thus
can put any adapting interface computations in the data models that construct the
data for the list and table.

15
private JList makeList(Vector v) {
return new JList(new ListModel(v));
}
//---------------------------------
private JTable makeTable(Vector v) {
return new JTable(new TableModel(v));
}

Where ListModel and TableModel are Java API classes.


THE BRIDGE PATTERN
:: The Bridge Pattern Class

Java Design Patterns


public class ListBridge extends JScrollPane{
static public final int TABLE = 1, LIST = 2;
private JList makeList(Vector v) {
return new JList(new ListModel(v));

16
}
private JTable makeTable(Vector v) {
return new JTable(new TableModel(v));
}
public ListBridge(Vector v, int table_type) { //constructor
Vector sort = sortVector(v); //sort the vector
if (table_type == LIST)
getViewport().add(makeList(sort)); //make table
if (table_type == TABLE)
getViewport().add(makeTable(sort)); //make list
}
}
THE BRIDGE PATTERN
:: Consequences of the Bridge Pattern

Java Design Patterns


 The Bridge pattern is intended to keep the interface to your client
program constant while allowing you to change the actual kind of
class you display or use.

17
 This can prevent you from recompiling a complicated set of user
interface modules, and only require that you recompile the bridge
itself and the actual end display class.

 You can extend the implementation class and the bridge class
separately, and usually without much interaction with each other.

You might also like