Decouple an abstraction from its implementation so that the two can vary independently. Use it when you want to avoid a permanent binding between an abstraction and its implementation, when both should be extensible by subclassing, or when changes in the implementation of an abstraction should have no impact on clients.
Examples:
- A Shape hierarchy with Circle and Square gains Red and Blue variants, requiring four subclasses (RedCircle, BlueCircle, RedSquare, BlueSquare). Bridge extracts Color into its own hierarchy; Shape holds a reference (the 'bridge') to a Color object and delegates color work to it — adding a new color or shape now costs one class, not N×M.
// Abstraction holds a reference to Implementor
class Window {
public:
Window(View* contents);
virtual void DrawRect(const Point& p1, const Point& p2);
protected:
WindowImp* GetWindowImp(); // returns platform-specific impl
View* GetView();
private:
WindowImp* _imp; // bridge to implementation
View* _contents;
};
Synonyms: handle/body