Introduction
In this article, we will cover the Bridge Design Pattern.
So, Let's get started.
Please refer to my previous article on Design Patterns.
SOLID Principles
Other Design Patterns
What is Ba Ridge?
The Bridge Design Pattern is a structural pattern that decouples an abstraction from its implementation, allowing both to vary independently. It helps in reducing the complexity by separating abstraction and implementation into different class hierarchies.
Why Use Bridge?
- Scalability: Easily extend both abstraction and implementation independently.
- Maintainability: Simplifies code maintenance by minimizing interdependencies.
- Flexibility: Enhances the ability to switch between implementations dynamically.
Where to Use Bridge?
- Multiple Implementations: When you have multiple ways to perform the same operation (e.g., different database access techniques).
- Abstraction from Implementation: When you need to isolate core functionalities from specific details of the platform or technology.
- Runtime Flexibility: Want to switch between implementations dynamically at runtime? The bridge allows for that.
Real-World Example. Send Message
Creating Abstract Implementer (IMessageSender)
This Interface acts as a bridge between the abstraction class and implementer classes.
// This interface acts as a bridge between the Abstraction Layer and Implementation Layer.
// The Implementor Interface defines operations for all implementation classes.
// It doesn't have to match the Abstraction's interface.
// In fact, the two interfaces can be entirely different.
public interface IMessageSender
{
void SendMessage(string message);
}
Creating Concrete Implementer (SmsMessageSender and EmailMessageSender)
These are going to be concrete classes that implement the IMessageSender interface and provide the implementation for the SendMessage method.
We are going to create two concrete implementers i.e. SmsMessageSender and EmailMessageSender. Each Concrete Implementation corresponds to a specific platform.
- SMSMessageSender: This class implements the IMessageSender interface and provides implementations for the SendMessage method. This SendMessage method is used to send messages using SMS.
// This is going to be a class that implements the Implementor Interface i.e. IMessageSender
// It also provides the implementation details for the associated Abstraction class
// Each Concrete Implementation corresponds to a specific platform, in this case sending messages using SMS
public class SmsMessageSender : IMessageSender
{
public void SendMessage(string Message)
{
//Send a message using SMS
Console.WriteLine("'" + Message + "' : This Message has been sent using SMS");
}
}
- EmailMessageSender: This class also implements the IMessageSender interface and also provides implementations for the SendMessage method. This SendMessage method is used to send messages using email.
// This is going to be a class that implements the Implementor Interface i.e. IMessageSender
// It also provides the implementation details for the associated Abstraction class
// Each Concrete Implementation corresponds to a specific platform, in this case sending messages using Email
public class EmailMessageSender : IMessageSender
{
public void SendMessage(string Message)
{
Console.WriteLine("'" + Message + "' : This Message has been sent using Email");
}
}
- Creating Abstraction (AbstractMessage): This class has one protected member i.e. message sender which will be available to the subclasses and one abstract method i.e. SendMessage which is going to be implemented by the concrete abstraction classes.
//This is an abstract class that going to be implemented by the Concrete Abstraction
//It contains a reference to an object of type IMessageSender Interface i.e. messageSender
//and delegates all of the real work to this object (the class that implements IMessageSender Interface).
//It can also act as the base class for other abstractions.
public abstract class AbstractMessage
{
protected IMessageSender messageSender;
public abstract void SendMessage(string Message);
}
Creating Concrete Abstraction (ShortMessage and LongMessage)
we are going to create two concrete classes i.e. ShortMessage and LongMessage.
ShortMessage
Here, In the constructor, we have initialized the superclass message sender variable as well as providing the implementation for the SendMessage method.
// This is going to be a concrete class which inherits from the Abstraction class i.e. AbstractMessage.
// This Concrete Abstraction Class implements the operations defined by AbstractMessage class.
public class ShortMessage : AbstractMessage
{
//The constructor expected an argument of type object which implements the IMessageSender interface
public ShortMessage(IMessageSender messageSender)
{
//Initialize the super class messageSender variable
this.messageSender = messageSender;
}
public override void SendMessage(string Message)
{
if (Message.Length <= 10)
{
messageSender.SendMessage(Message);
}
else
{
Console.WriteLine("Unable to send the message as length > 10 characters");
}
}
}
LongMessage
Here, In the constructor, initialize the message sender variable and provide the implementation for the SendMessage method.
// This is going to be a concrete class that inherits from the Abstraction class i.e. AbstractMessage.
// This Concrete Abstraction Class implements the operations defined by AbstractMessage class.
public class LongMessage : AbstractMessage
{
public LongMessage(IMessageSender messageSender)
{
//Initialize the super class messageSender variable
this.messageSender = messageSender;
}
public override void SendMessage(string Message)
{
messageSender.SendMessage(Message);
}
}
Client
public class Program
{
public static void Main(string[] args)
{
// Except for the initialization phase, where an Abstraction object
// (LongMessage or ShortMessage) is linked with a specific Implementation
// object (new EmailMessageSender() or new SmsMessageSender()),
// the client code should only depend on the Abstraction class (AbstractMessage).
Console.WriteLine("Select the Message Type: 1 for LongMessage or 2 for ShortMessage");
int messageType = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the message that you want to send:");
string message = Console.ReadLine();
if (messageType == 1)
{
AbstractMessage longMessage = new LongMessage(new EmailMessageSender());
longMessage.SendMessage(message);
}
else
{
AbstractMessage shortMessage = new ShortMessage(new SmsMessageSender());
shortMessage.SendMessage(message);
}
Console.ReadKey();
}
}
Output
Send a Long Message. Sending through Email.
![Output]()
Send Short Message. Sending through SMS
![SMS]()
Summary
In this article, I have tried to cover Bridge Design Patterns.