Bridge Design Pattern
Structural pattern. Structural design patterns are concerned with composing classes and objects into larger structures.
The Bridge design pattern solves problems like: How can an abstraction and its implementation vary independently?
Open in the interactive referenceDownload the PDF reference
Problem
The Bridge design pattern solves problems like:
How can an abstraction and its implementation
vary independently?
How can an implementation
be selected and exchanged at run-time?
See Applicability section for all problems Bridge can solve.
See Solution section for how Bridge solves the problems.
-
The standard way is to implement an abstraction
by inheritance, i.e., different (sub)classes
(
Implementor1,…) implement the abstraction (interface) in different ways. -
This commits (binds) the implementation to an abstraction at compile-time
and makes it impossible to change the implementation at run-time.
"Inheritance binds an implementation to the abstraction permanently, which makes it difficult to modify, extend, and reuse abstractions and implementations independently. [GoF, p151] - That's the kind of approach to avoid if we want that an implementation can be selected and exchanged at run-time instead of committing to an implementation at compile-time.
-
For example, a reusable application
that supports different hardware environments.
To make an application portable across different hardware environments, it should be possible to select the proper hardware-specific implementation at run-time.
Solution
The Bridge design pattern provides a solution:
Define separate inheritance hierarchies for
an abstraction and its implementation.
Abstraction
delegates its implementation
to an Implementor object
instead of committing to an implementation at
compile-time.
Describing the Bridge
design in more detail is the theme of the following sections.
See Applicability section for all problems Bridge can solve.
-
The key idea in this pattern is to separate (decouple)
an abstraction from its implementation
so that the two can be defined independently from each other.
The pattern calls the relationship between an abstraction and its implementation a bridge "because it bridges the abstraction and its implementation, letting them vary independently." [GoF, p152] -
Define separate inheritance hierarchies for
an abstraction
(Abstraction)
and its implementation(Implementor).-
The
Abstractioninterface is implemented in terms of (by delegating to) anImplementorobject (imp.operationImp()). - "Typically the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives." [GoF, p154]
-
The
-
This enables compile-time flexibility (via inheritance).
Abstraction and implementation can be defined independently from each other. -
Abstractiondelegates its implementation to anImplementorobject
(imp.operationImp()). -
This enables run-time
flexibility (via object composition).
Abstractioncan be configured with anImplementorobject, and even more, theImplementorobject can be exchanged at run-time.
Applicability
Design Problems
-
Defining Abstraction and Implementation Independently
- How can an abstraction and its implementation vary independently?
-
How can a compile-time binding between
an abstraction and its implementation be avoided?
-
Exchanging Implementations at Run-Time
- How can an abstraction be configured with an implementation?
- How can an implementation be selected and exchanged at run-time?
-
Flexible Alternative to Subclassing
-
How can a flexible alternative be provided to subclassing
for changing an implementation at compile-time?
-
How can a flexible alternative be provided to subclassing
Structure
Static Class Structure
-
Abstraction- Defines an interface for an abstraction.
-
Abstraction1,…-
Implement the
Abstractioninterface in terms of (by delegating to) theImplementorinterface (imp.operationImp()). -
Maintains a reference
(
imp) to anImplementorobject.
-
Implement the
-
Implementor- For all supported implementations, defines a common interface for implementing an abstraction.
- "Typically the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives." [GoF, p154]
-
Implementor1,…-
Implement
the
Implementorinterface.
-
Implement
the
Dynamic Object Collaboration
-
In this sample scenario,
an
Abstraction1object delegates implementation to anImplementor1object (by callingoperationImp()onImplementor1). -
See also Sample Code / Example 1.
Sample Code 1
Basic Java code for implementing the sample UML diagrams.
1 package com.sample.bridge.basic;
2 public class MyApp {
3 public static void main(String[] args) {
4 // Creating an Abstraction1 object
5 // and configuring it with an Implementor1 object.
6 Abstraction abstraction = new Abstraction1(new Implementor1());
7 // Calling an operation on abstraction.
8 System.out.println(abstraction.operation());
9 }
10 }
Abstraction1: Delegating implementation to an implementor.
Implementor1: Hello World1!
1 package com.sample.bridge.basic;
2 public interface Abstraction {
3 String operation();
4 }
1 package com.sample.bridge.basic;
2 public class Abstraction1 implements Abstraction {
3 private Implementor imp;
4 //
5 public Abstraction1(Implementor imp) {
6 this.imp = imp;
7 }
8 public String operation() {
9 return "Abstraction1: Delegating implementation to an implementor.\n"
10 + imp.operationImp();
11 }
12 }
1 package com.sample.bridge.basic;
2 public interface Implementor {
3 String operationImp();
4 }
1 package com.sample.bridge.basic;
2 public class Implementor1 implements Implementor {
3 public String operationImp() {
4 return "Implementor1: Hello World1!";
5 }
6 }
Not included on this page: Intent, Motivation, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.