Pattern · Advanced

Bridge

Split one thing that varies in TWO dimensions into two separate hierarchies, joined by a reference — so each can grow independently instead of multiplying.

Asked atAmazonMicrosoftAdobe
step 1 / 8
1/2N×M explosion
Shape
RedCircle
BlueCircle
RedSquare
N×M explosion
1class Shape { ... }
2class RedCircle extends Shape { ... }
3class BlueCircle extends Shape { ... }
4class RedSquare extends Shape { ... } // … and on, and on
5// 3 shapes × 4 colours = 12 classes 💥

Subclass every shape × colour combination: RedCircle, BlueCircle, RedSquare… each pairing needs its very own class.