The Bridge Design Pattern allows you to decouple the implementation from the abstraction. In other words we can have the implementation separated from our classes and reuse them rather than implementing another hierarchy level.

One simple example mentioned on Stack Overflow is the following structure:

structure

Implementing the Bridge Pattern it becomes:

Bridge

This simple example explains what it is and shows you why you would want it. But what about a more realistic world example? Well, we could, for example, rename the “Color” stuff to “DrawingApi”.

  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. var hotApi = new HotDrawingImplementor();
  8. var coolApi = new CoolDrawingImplementor();
  9. var hotRectangle = new Rectangle(hotApi);
  10. var coolRectangle = new Rectangle(coolApi);
  11. hotRectangle.Draw();
  12. coolRectangle.Draw();
  13. Console.ReadKey(true);
  14. }
  15. }
  16. public abstract class Shape
  17. {
  18. protected DrawingImplementor implementor;
  19. public Shape(DrawingImplementor implementor)
  20. {
  21. this.implementor = implementor;
  22. }
  23. public void Draw()
  24. {
  25. implementor.Draw();
  26. }
  27. }
  28. public class Rectangle : Shape
  29. {
  30. public Rectangle(DrawingImplementor implementor)
  31. : base(implementor)
  32. {
  33. }
  34. }
  35. public class Circle : Shape
  36. {
  37. public Circle(DrawingImplementor implementor)
  38. : base(implementor)
  39. {
  40. }
  41. }
  42. public abstract class DrawingImplementor
  43. {
  44. public abstract void Draw();
  45. }
  46. public class CoolDrawingImplementor : DrawingImplementor
  47. {
  48. public override void Draw()
  49. {
  50. Console.WriteLine("Drawing cool!");
  51. }
  52. }
  53. public class HotDrawingImplementor : DrawingImplementor
  54. {
  55. public override void Draw()
  56. {
  57. Console.WriteLine("Drawing hot!");
  58. }
  59. }
  60. }
Output

    Drawing hot!
    Drawing cool!

If you have a hierarchy like that you might want to consider applying this pattern.