Bridge Design Pattern — 3 Minute Series

Your best decoupling friend.

Elric Edward
Sep 18, 2022
Photo by Mason Kimbarovsky on Unsplash

_00 / Concept

Think like you’re building a bridge. This pattern aims to break down your coupled code, then use an injection-like way to composite your “Bridge.”

_01 / Key Roles

Abstraction and Implementor. Abstraction doesn’t do anything by itself. Instead, it calls the implementor’s method. One abstraction can own several implementors.

// abstraction
class BasketballPlayer {
constructor(trainAPI, scoreAPI) {
this.trainAPI = trainAPI
this.scoreAPI = scoreAPI
}
train() { this.trainAPI.execute() }
score() { this.scoreAPI.execute() }
}
// implementor
class TrainShoot extends TrainAPI() {
execute() { ... }
}
class ScoreDunk extends ScoreAPI() {
execute() { ... }
}
// client
const trainShoot = new TrainShoot()
const scoreDunk = new ScoreDunk()
const playerA = new BasketballPlayer(trainShoot, scoreDunk)

_02 / Trade-offs

🟢 Decoupling and extendable.
🟢 Follow the OOP guideline very well.
🔴 More codes, hard to manage.

--

--