Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bridge Pattern

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

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

structures

The following are the key roles of the bridge pattern:

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]


Related article recommendations

Bridge Pattern