What is the Bridge pattern?

Topic: design-patterns, Structural Patterns. Level: Hard.

Answer

Bridge decouples an **abstraction** from its **implementation** so the two can vary independently, replacing a combinatorial explosion of subclasses with composition. Instead of `RedCircle, BlueCircle, RedSquare, BlueSquare…`, you have a `Shape` abstraction holding a `Color` implementor. Adding a shape or a color is then linear, not multiplicative. Use it when both dimensions change independently. ```java interface Color { String fill(); } // implementation side class Red implements Color { public String fill(){ return "red"; } } class Blue implements Color { public String fill(){ return "blue"; } } abstract class Shape { // abstraction side protected final Color color; // bridge (composition) Shape(Color c){ this.color = c; } abstract String draw(); } class Circle extends Shape { Circle(Color c){ super(c); } String draw(){ return "Circle in " + color.fill(); } } // New shape OR new color added independently — no NxM subclasses. Shape s = new Circle(new Blue()); ```