Bridge Pattern
Bridge is used to decouple abstraction from implementation, allowing the two to vary independently. This type of design pattern belongs to the structural pattern category; it achieves the decoupling by providing a bridge structure between abstraction and implementation.
This pattern involves an interface acting as a bridge, making the functionality of concrete classes independent of the interface implementation classes; these two types of classes can be structurally changed without affecting each other.
The purpose of the bridge pattern is to separate abstraction from implementation so they can vary independently; the pattern separates an object's abstract part from its implementation part, allowing them to change independently. It connects the abstraction and implementation parts through composition rather than inheritance.
We demonstrate the usage of the Bridge Pattern through the following example, in which the same abstract class methods but different bridge implementation classes can be used to draw circles of different colors.
Introduction
Intent
Used to separate the abstraction part from the implementation part, so that they can change independently..
Main problem solved
Avoid the class explosion problem caused by inheritance, providing a more flexible way of extension.
Use Cases
The bridge pattern is suitable when a system may be classified from multiple perspectives, and each perspective may change independently.
Implementation
- Separate multi-perspective classification: separate the classification logic of different perspectives, allowing them to change independently.
- Reduce coupling: reduce the degree of coupling between abstraction and implementation.
Key code
- abstract classes: define an abstract class as part of the system.
- Implementation class: define one or more implementation classes, associated with the abstract class through aggregation (rather than inheritance).
Application Example
- Reincarnation: the separation of soul (abstraction) and body (implementation), allowing the soul to choose different bodies.
- The switch on the wall: the separation of the switch (abstraction) and the internal implementation (implementation); the user does not need to care about the internal working mechanism of the switch.
Advantages
- Separation of abstraction and implementation: improves the flexibility and maintainability of the system.
- Strong extensibility: abstraction and implementation can be extended independently.
- Implementation details are transparent: users do not need to understand the implementation details.
Disadvantages
- Understanding and design difficulty: the bridge pattern increases the difficulty of understanding and designing the system.
- Aggregation association: requires developers to design and program at the abstraction layer.
Usage recommendation
- When a system needs to increase flexibility between abstraction and concrete roles, consider using the bridge pattern.
- The bridge pattern is especially suitable for systems that do not want to use inheritance or where multi-level inheritance causes a sharp increase in the number of classes.
- When a class has two independently varying dimensions, and both need to be extended, use the bridge pattern.
Notes
- The bridge pattern applies to two independently varying dimensions, ensuring they can be extended and changed independently.
structures
The following are the key roles of the bridge pattern:
- Abstraction: defines the abstract interface, usually containing a reference to the implementation interface.
- Refined Abstraction: an extension of the abstraction, which can be a subclass of the abstract class or a concrete implementation class.
- Implementor: defines the implementation interface, providing the interface for basic operations.
- Concrete Implementor: the concrete class that implements the implementation interface.
Create a class that implements
We have an DrawAPI interface as the bridge implementation and concrete classes that implement the DrawAPI interface RedCircle、GreenCircle。Shape is an abstract class that will use DrawAPI objects.BridgePatternDemo Class uses Shape class to draw circles of different colors.
Step 1
Create the bridge implementation interface.
DrawAPI.java
public
interface
DrawAPI
{
public
void
drawCircle
(
int
radius
,
int
x
,
int
y
)
;
}
Step 2
Create concrete classes that implement the DrawAPI concrete bridge implementation classes of the interface.
RedCircle.java
public
class
RedCircle
implements
DrawAPI
{
@
Override
public
void
drawCircle
(
int
radius
,
int
x
,
int
y
)
{
System
.
out
.
println
(
"
Drawing Circle[ color: red, radius:
"
radius
"
, x:
"
x
"
,
"
y
"
]
"
)
;
}
}
GreenCircle.java
public
class
GreenCircle
implements
DrawAPI
{
@
Override
public
void
drawCircle
(
int
radius
,
int
x
,
int
y
)
{
System
.
out
.
println
(
"
Drawing Circle[ color: green, radius:
"
radius
"
, x:
"
x
"
,
"
y
"
]
"
)
;
}
}
Step 3
Use DrawAPI abstract class created from the interface Shape。
Shape.java
public
abstract
class
Shape
{
protected
DrawAPI
drawAPI
;
protected
Shape
(
DrawAPI
drawAPI
)
{
this
.
drawAPI
=
drawAPI
;
}
public
abstract
void
draw
(
)
;
}
Step 4
Create concrete classes that implement the Shape concrete classes of the abstract class.
Circle.java
public
class
Circle
extends
Shape
{
private
int
x
,
y
,
radius
;
public
Circle
(
int
x
,
int
y
,
int
radius
,
DrawAPI
drawAPI
)
{
super
(
drawAPI
)
;
this
.
x
=
x
;
this
.
y
=
y
;
this
.
radius
=
radius
;
}
public
void
draw
(
)
{
drawAPI
.
drawCircle
(
radius
,
x
,
y
)
;
}
}
Step 5
Use Shape and DrawAPI class to draw circles of different colors.
BridgePatternDemo.java
public
class
BridgePatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
Shape
redCircle
=
new
Circle
(
100
,
100
,
10
,
new
RedCircle
(
)
)
;
Shape
greenCircle
=
new
Circle
(
100
,
100
,
10
,
new
GreenCircle
(
)
)
;
redCircle
.
draw
(
)
;
greenCircle
.
draw
(
)
;
}
}
Step 6
Run the program, output result:
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]