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: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.Redefined Abstraction
This is a class which inherits from the Abstraction class. It extends the interface defined by Abstraction class.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.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:- Message - Abstraction Class.
- SystemMessage & UserMessage- Redefined Abstraction Classes.
- IMessageSender- Bridge Interface.
- 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?
- Abstractions and implementations should be modified independently.
- Changes in the implementation of an abstraction should have no impact on clients.
- 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.