Bridge Pattern Explained with Code and Real-World Scenarios
This article introduces the Bridge design pattern, explains its purpose of separating two independent dimensions to reduce class explosion, provides practical real‑world examples, and walks through a complete Java implementation with interface, abstract bridge, concrete classes, and a test driver.
Bridge Pattern
Definition and purpose
Bridge separates two independent varying dimensions of a concept, connecting them through a bridge rather than creating a subclass for every combination. The goal is to reduce class count and allow each side to evolve independently.
Typical scenarios
Pen size (big/medium/small) combined with color (red/black/blue) – avoid classes like BigRedPen.
Software: multiple platforms (Android/iOS) with multiple themes (light/dark); message type (SMS/WeChat) with delivery channel.
Video player: format (MP4/AVI) with operating system (Windows/Mac).
Java implementation (Pen + Color)
1. Color dimension (independent variation layer)
// Color interface
public interface Color {
void showColor();
}
// Red
public class Red implements Color {
@Override
public void showColor() {
System.out.print("红色");
}
}
// Black
public class Black implements Color {
@Override
public void showColor() {
System.out.print("黑色");
}
}2. Pen abstraction (holds a Color as bridge)
public abstract class Pen {
protected Color color;
public Pen(Color color){
this.color = color;
}
public abstract void draw();
}3. Concrete pen variants (independent extensions)
// Big pen
public class BigPen extends Pen{
public BigPen(Color color) {
super(color);
}
@Override
public void draw() {
color.showColor();
System.out.println("大号画笔");
}
}
// Small pen
public class SmallPen extends Pen{
public SmallPen(Color color) {
super(color);
}
@Override
public void draw() {
color.showColor();
System.out.println("小号画笔");
}
}4. Test invocation
public class Test {
public static void main(String[] args) {
Pen bigRed = new BigPen(new Red());
bigRed.draw();
Pen smallBlack = new SmallPen(new Black());
smallBlack.draw();
}
}Common business use cases
Objects with two or more independent varying dimensions that need continuous extension.
When multi‑layer combinations would generate many subclasses, use Bridge to avoid class explosion.
Multi‑platform clients: separate system layer from business functionality layer.
Multimedia tools: decouple file format from device system.
Message push: separate message type from delivery channel.
Distinguishing from Adapter
Adapter addresses post‑hoc interface compatibility, while Bridge separates dimensions proactively during design.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
liandk
Seasoned Java and mobile developer with years of experience, specializing in mini‑programs, public accounts, and full‑stack front‑end development. In the AI era, I continuously learn to broaden my knowledge and evolve. I revived a public account I started a decade ago during a dessert‑startup venture, using code as a vessel and knowledge as a companion. I share personal projects, technical articles, programming tips, and growth insights—let’s improve together and set sail.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
