Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Bridge Design Pattern

Bridge Design Pattern

23 Aug 2022
Intermediate
99.6K Views
5 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Bridge design 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 cannot be modified independently. The Bridge pattern provides an alternative to inheritance when there is more than one version of abstraction. In this article, I would like to share what is bridge pattern and how is it work?

What is Bridge Pattern

The bridge pattern is used to separate 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 act 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

public abstract class Abstraction
{
 public Bridge Implementer { get; set; }
 
 public virtual void Operation()
 {
 Console.WriteLine("ImplementationBase:Operation()");
 Implementer.OperationImplementation();
 }
}
 
public class RefinedAbstraction : Abstraction
{
 public override void Operation()
 {
 Console.WriteLine("RefinedAbstraction:Operation()");
 Implementer.OperationImplementation();
 }
}
 
public interface Bridge
{
 void OperationImplementation();
}
 
public class ImplementationA : Bridge
{
 public void OperationImplementation()
 {
 Console.WriteLine("ImplementationA:OperationImplementation()");
 }
}
 
public class ImplementationB : Bridge
{
 public void OperationImplementation()
 {
 Console.WriteLine("ImplementationB:OperationImplementation()");
 }
}

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

/// <summary>
/// The 'Abstraction' class
/// </summary>
public abstract class Message
{
 public IMessageSender MessageSender { get; set; }
 public string Subject { get; set; }
 public string Body { get; set; }
 public abstract void Send();
}

/// <summary>
/// The 'RefinedAbstraction' class
/// </summary>
public class SystemMessage : Message
{
 public override void Send()
 {
 MessageSender.SendMessage(Subject, Body);
 }
}

/// <summary>
/// The 'RefinedAbstraction' class
/// </summary>
public class UserMessage : Message
{
 public string UserComments { get; set; }

 public override void Send()
 {
 string fullBody = string.Format("{0}\nUser Comments: {1}", Body, UserComments);
 MessageSender.SendMessage(Subject, fullBody);
 }
}

/// <summary>
/// The 'Bridge/Implementor' interface
/// </summary>
public interface IMessageSender
{
 void SendMessage(string subject, string body);
}

/// <summary>
/// The 'ConcreteImplementor' class
/// </summary>
public class EmailSender : IMessageSender
{
 public void SendMessage(string subject, string body)
 {
 Console.WriteLine("Email\n{0}\n{1}\n", subject, body);
 }
}

/// <summary>
/// The 'ConcreteImplementor' class
/// </summary>
public class MSMQSender : IMessageSender
{
 public void SendMessage(string subject, string body)
 {
 Console.WriteLine("MSMQ\n{0}\n{1}\n", subject, body);
 }
}

/// <summary>
/// The 'ConcreteImplementor' class
/// </summary>
public class WebServiceSender : IMessageSender
{
 public void SendMessage(string subject, string body)
 {
 Console.WriteLine("Web Service\n{0}\n{1}\n", subject, body);
 }
}

/// <summary>
/// Bridge Design Pattern Demo
/// </summary>
class Program
{
 static void Main(string[] args)
 {
 IMessageSender email = new EmailSender();
 IMessageSender queue = new MSMQSender();
 IMessageSender web = new WebServiceSender();

 Message message = new SystemMessage();
 message.Subject = "Test Message";
 message.Body = "Hi, This is a Test Message";
 
 message.MessageSender = email;
 message.Send();

 message.MessageSender = queue;
 message.Send();

 message.MessageSender = web;
 message.Send();

 UserMessage usermsg = new UserMessage();
 usermsg.Subject = "Test Message";
 usermsg.Body = "Hi, This is a Test Message";
 usermsg.UserComments = "I hope you are well";

 usermsg.MessageSender = email;
 usermsg.Send();

 Console.ReadKey();
 }
}

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 needs 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.

Read More Articles Related to Design patterns

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.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this