HI WELCOME TO SIRIS

Bridge Design Pattern - C#

Bridge pattern falls under Structural Pattern of Gang of Four (GOF) Design Patterns in .Net. All we know, Inheritance is a way to specify different implementations of an abstraction. But in this way, implementations are tightly bound to the abstraction and can not be modified independently. The Bridge pattern provides an alternative to inheritance when there are more than one version of an abstraction. In this article, I would like share what is bridge pattern and how is it work?

What is Bridge Pattern

Bridge pattern is used to separate an abstraction from its implementation so that both can be modified independently.
This pattern involves an interface which acts as a bridge between the abstraction class and implementer classes and also makes the functionality of implementer class independent from the abstraction class. Both types of classes can be modified without affecting to each other.

Bridge Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the bridge design pattern is given below:
The classes, interfaces and objects in the above UML class diagram are as follows:


  1. Abstraction

    This is an abstract class and containing members that define an abstract business object and its functionality. It contains a reference to an object of type Bridge. It can also acts as the base class for other abstractions.
  2. Redefined Abstraction

    This is a class which inherits from the Abstraction class. It extends the interface defined by Abstraction class.
  3. Bridge

    This is an interface which acts as a bridge between the abstraction class and implementer classes and also makes the functionality of implementer class independent from the abstraction class.
  4. ImplementationA & ImplementationB

    These are classes which implement the Bridge interface and also provide the implementation details for the associated Abstraction class.

C# - Implementation Code

  1. public abstract class Abstraction
  2. {
  3. public Bridge Implementer { get; set; }
  4. public virtual void Operation()
  5. {
  6. Console.WriteLine("ImplementationBase:Operation()");
  7. Implementer.OperationImplementation();
  8. }
  9. }
  10. public class RefinedAbstraction : Abstraction
  11. {
  12. public override void Operation()
  13. {
  14. Console.WriteLine("RefinedAbstraction:Operation()");
  15. Implementer.OperationImplementation();
  16. }
  17. }
  18. public interface Bridge
  19. {
  20. void OperationImplementation();
  21. }
  22. public class ImplementationA : Bridge
  23. {
  24. public void OperationImplementation()
  25. {
  26. Console.WriteLine("ImplementationA:OperationImplementation()");
  27. }
  28. }
  29. public class ImplementationB : Bridge
  30. {
  31. public void OperationImplementation()
  32. {
  33. Console.WriteLine("ImplementationB:OperationImplementation()");
  34. }
  35. }

Bridge Pattern - Example


Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:
  1. Message - Abstraction Class.
  2. SystemMessage & UserMessage- Redefined Abstraction Classes.
  3. IMessageSender- Bridge Interface.
  4. EmailSender, WebServiceSender & MSMQ Sender- ConcreteImplementation class which implements the IMessageSender interface.

C# - Sample Code

  1. /// <summary>
  2. /// The 'Abstraction' class
  3. /// </summary>
  4. public abstract class Message
  5. {
  6. public IMessageSender MessageSender { get; set; }
  7. public string Subject { get; set; }
  8. public string Body { get; set; }
  9. public abstract void Send();
  10. }
  11.  
  12. /// <summary>
  13. /// The 'RefinedAbstraction' class
  14. /// </summary>
  15. public class SystemMessage : Message
  16. {
  17. public override void Send()
  18. {
  19. MessageSender.SendMessage(Subject, Body);
  20. }
  21. }
  22.  
  23. /// <summary>
  24. /// The 'RefinedAbstraction' class
  25. /// </summary>
  26. public class UserMessage : Message
  27. {
  28. public string UserComments { get; set; }
  29.  
  30. public override void Send()
  31. {
  32. string fullBody = string.Format("{0}\nUser Comments: {1}", Body, UserComments);
  33. MessageSender.SendMessage(Subject, fullBody);
  34. }
  35. }
  36.  
  37. /// <summary>
  38. /// The 'Bridge/Implementor' interface
  39. /// </summary>
  40. public interface IMessageSender
  41. {
  42. void SendMessage(string subject, string body);
  43. }
  44.  
  45. /// <summary>
  46. /// The 'ConcreteImplementor' class
  47. /// </summary>
  48. public class EmailSender : IMessageSender
  49. {
  50. public void SendMessage(string subject, string body)
  51. {
  52. Console.WriteLine("Email\n{0}\n{1}\n", subject, body);
  53. }
  54. }
  55.  
  56. /// <summary>
  57. /// The 'ConcreteImplementor' class
  58. /// </summary>
  59. public class MSMQSender : IMessageSender
  60. {
  61. public void SendMessage(string subject, string body)
  62. {
  63. Console.WriteLine("MSMQ\n{0}\n{1}\n", subject, body);
  64. }
  65. }
  66.  
  67. /// <summary>
  68. /// The 'ConcreteImplementor' class
  69. /// </summary>
  70. public class WebServiceSender : IMessageSender
  71. {
  72. public void SendMessage(string subject, string body)
  73. {
  74. Console.WriteLine("Web Service\n{0}\n{1}\n", subject, body);
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// Bridge Design Pattern Demo
  80. /// </summary>
  81. class Program
  82. {
  83. static void Main(string[] args)
  84. {
  85. IMessageSender email = new EmailSender();
  86. IMessageSender queue = new MSMQSender();
  87. IMessageSender web = new WebServiceSender();
  88.  
  89. Message message = new SystemMessage();
  90. message.Subject = "Test Message";
  91. message.Body = "Hi, This is a Test Message";
  92. message.MessageSender = email;
  93. message.Send();
  94.  
  95. message.MessageSender = queue;
  96. message.Send();
  97.  
  98. message.MessageSender = web;
  99. message.Send();
  100.  
  101. UserMessage usermsg = new UserMessage();
  102. usermsg.Subject = "Test Message";
  103. usermsg.Body = "Hi, This is a Test Message";
  104. usermsg.UserComments = "I hope you are well";
  105.  
  106. usermsg.MessageSender = email;
  107. usermsg.Send();
  108.  
  109. Console.ReadKey();
  110. }
  111. }

Bridge Pattern Demo - Output

When to use it?

  1. Abstractions and implementations should be modified independently.
  2. Changes in the implementation of an abstraction should have no impact on clients.
  3. The Bridge pattern is used when a new version of a software or system is brought out, but the older version of the software still running for its existing client. There is no need to change the client code, but the client need to choose which version he wants to use.

Note

Bridge pattern has nearly the same structure as the Adapter Pattern. But it is used when designing new systems instead of the Adapter pattern which is used with already existing systems.
What do you think?
I hope you will enjoy the bridge Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.