Design Pattern For Beginner Part 7: Bridge Design Pattern

Welcome to the Design Pattern For Beginner article series. In this article we will try to understand the Bridge Design Pattern. If this article is your first article in this series then I suggest you visit the other articles in this series.

Why Bridge Pattern?

Before starting with a technical explanation and example we will try to understand why the Bridge Design Pattern is essential and in which scenario it will be implemented.

As the name suggests, it makes a bridge between two components. Here the component may be two classes or any other entity. So the Bridge Design Pattern basically makes a channel between two components. And in this way it helps to create a de-couple architecture. We can communicate with two classes through the bridge component without changing existing class definitions.
BridgePattern1.jpg
So if we summarize the basic need for the Bridge Design Pattern then we will see that it help us to design a de-couple architecture in the software project. It makes abstraction over implementation.

Let's implement the first example


In the following we implement one small example of the Bridge Design Pattern. When we are talking of bridge let's implement a simple bridge between two cities. Here instead of city we will call it node.

In the example we have implemented an Inode interface in both the Node_A and Node_B class. Then in the following, the two node classes, we have defined another class called Bridge, that will communicate between two cities. The ReachTo() function of the bridge class will take us to a specific city (node). Now we are clearly seeing the Program class (Main() function) is talking with any city class (node) through the Bridge class.

  1. using System;  
  2. using System.Collections;  
  3. namespace BridgeDesign  
  4. {  
  5.     public interface Inode  
  6.     {  
  7.         void Reach();  
  8.     }  
  9.     class Node_A : Inode  
  10.     {  
  11.         public void Reach()  
  12.         {  
  13.             Console.WriteLine("Rreached to node A");  
  14.         }  
  15.     }  
  16.     class Node_B : Inode  
  17.     {  
  18.         public void Reach()  
  19.         {  
  20.             Console.WriteLine("Rreached to node B");  
  21.         }  
  22.     }  
  23.     class Bridge  
  24.     {  
  25.         public void ReachTo(Inode obj)  
  26.         {  
  27.             obj.Reach();  
  28.         }  
  29.     }  
  30.     class Program  
  31.     {  
  32.         static void Main(string[] args)  
  33.         {  
  34.             Bridge br = new Bridge();  
  35.             Node_A a = new Node_A();  
  36.             Node_B b = new Node_B();  
  37.             br.ReachTo(a); //Reach to Node_A  
  38.             br.ReachTo(b); //Reach to Node_B  
  39.             Console.ReadLine();  
  40.         }  
  41.     }  
  42. }
Here is sample output.

BridgePattern2.jpg

Hmm.. What an unrealistic example. Yes this example is only for you to understand the basic implementation of the Bridge Design Pattern. OK, let's work with one useful example (at least related to realistic project development).

The Mail sending operation was implemented by a Bridge Pattern.

Mail sending and notification is a very common task for a software developer. We can now send mail in various ways, for example in a new version of an old project you have developed a mail sending function in C# but in the same project there is another function to send mail that is written in VB6 (maybe it was written by a senior of your seniors. Ha ..Ha..) and is still working fine.

Anyway, your manager said that they want to use both C# and VB versions associated with sending mail from a database (so, in total three different ways). Now, let's implement this scenario using the Bridge Design Pattern. Have a look at the following example.

  1. using System;  
  2. using System.Collections;  
  3. namespace BridgeDesign  
  4. {  
  5.     public interface IMessage  
  6.     {  
  7.         void Send();  
  8.     }  
  9.     class CSharp_Mail : IMessage  
  10.     {  
  11.         public void Send()  
  12.         {  
  13.             Console.WriteLine("Mail send from C# code");  
  14.         }  
  15.     }  
  16.     class VB_Mail : IMessage  
  17.     {  
  18.         public void Send()  
  19.         {  
  20.             Console.WriteLine("Mail send from VB Code");  
  21.         }  
  22.     }  
  23.     class Databas_Mail : IMessage  
  24.     {  
  25.         public void Send()  
  26.         {  
  27.             Console.WriteLine("Mail send from Database");  
  28.         }  
  29.     }  
  30.     class MailSendBridge  
  31.     {  
  32.         public void SendFrom(IMessage obj)  
  33.         {  
  34.             obj.Send();  
  35.         }  
  36.     }  
  37.     class Program  
  38.     {  
  39.         static void Main(string[] args)  
  40.         {  
  41.             MailSendBridge mb = new MailSendBridge();  
  42.             CSharp_Mail objCS = new CSharp_Mail();  
  43.             VB_Mail objVB = new VB_Mail();  
  44.             mb.SendFrom(objCS);    //Mail send from C# cod  
  45.             mb.SendFrom(objVB);  //Mail Send from VB Code  
  46.             Console.ReadLine();  
  47.         }  
  48.     }  
  49. }
If you observe closely the body of the Main() function, you will find we are sending just an appropriate object to use the proper mail sending function. Here the MailSendBridge class is creating one abstraction over the implementation of the actual mail sending mechanism that was defined by a different mail sending class.

Here is sample output.

BridgePattern3.jpg

Conclusion

Here we were trying to understand the Bridge Design Pattern with examples. IU hope you have understood the basic concept and implementation of the Bridge Design Pattern.


Similar Articles