Skip to main content

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.

Comments

Popular posts from this blog

gcAllowVeryLargeObjects Element

There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {    ...

Support for debugging lambda expressions with Visual Studio 2015

Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitati...

How to allow a very large object in .net application?

Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio, and create C# Console Application. Lets create simple struct with two double members like example below: public struct ComplexNumber { public double Re; public double Im; public ComplexNumber(double re, double im) { Re = re; Im = im; } } As we know this structure consumes about 16 bytes of memory. So if we want to create array of this type which consume more than 2GB we need to create array at least with 134217728 instances. So this sample program below creates 130000000 (about 1,97 GB) of array. int maxCount = 130000000; ComplexNumber[] arr = null; try { arr = new ComplexNumber[maxCount]; } catch (Exception ex) { Console.WriteLine(ex.Message); } So if we run t...