C# | Bridge Design Pattern | Structural Design 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.
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- Concrete Implementation class which
implements the IMessageSender interface.
/// <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);
}
}
---------------------------------------------Program Class --------------------------------------------------
internal 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";
Console.ReadKey();
}
}
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
Post a Comment