Bridge Design Pattern Real-Time Example

Bridge Design Pattern Real-Time Example – Send Message

In this article, I am going to discuss the Bridge Design Pattern Real-Time Example – Send Message in C#. Please read our previous article where we discussed the Bridge Design Pattern in C# with examples.

As already discussed, in Bridge Design Pattern there are two layers i.e. Abstraction and Implementation. As per the Bridge Design Pattern, if we do make any changes in the Implementation layer then it won’t affect the Abstraction Layer and in the same way, if we do any changes in the abstraction layer then it won’t affect the Implementation layer. With this keep in mind let us proceed to this article where we will discuss the real-time example send a message using the Bridge Design patter.

Bridge Design Pattern Real-Time Example – Send Message

Please have a look at the following diagram. On the left-hand side, you can see the abstraction layer. Let say you want to send a message to the corresponding person. On the right-hand side, you can see two implementations. You can Email the message or SMS the message to the corresponding person. So, you have two options to send the message and the abstraction will use one of the implementations to send the message to the corresponding person.

Bridge Design Pattern Real-Time Example - Send Message

Later if you want to add any new implementation to the Implementation layer then it will not affect the Abstraction layer. This is one of the best examples of bridge design pattern.

Implementation of Bridge Design Pattern Real-Time Example in C# – Send Message

Let us implement the above example in C# using the Bridge Design Pattern step by step. Let us first understand the class diagram and sequence and then we will implement the example using C#.

Class Diagram:

Please have a look at the following image.

Implementation of Bridge Design Pattern Real-Time Example in C# – Send Message

Step1: Creating Abstract Implementer (IMessageSender)

This is going to be an interface. Create an interface with the name IMessageSender and then copy and paste the following code in it. This interface will be implemented by concrete implementer classes. This interface has one method i.e. SendMessage which takes the message as a string parameter.

namespace BridgePatternRealTimeExample
{
    public interface IMessageSender
    {
        void SendMessage(string Message);
    }
}
Step2: Creating Concrete Implementer (SmsMessageSender and EmailMessageSender)

These are going to be concrete classes and implement the IMessageSender interface and provide the implementation for the SendMessage method. In our example, there are two concrete implementers i.e. SmsMessageSender and EmailMessageSender.

SmsMessageSender

Create a class file with the name SmsMessageSender and then copy and paste the following code in it. This class implements the IMessageSender interface and provides implementations for the SendMessage method. This SendMessage method is used to send the message using SMS.

using System;

using System;
namespace BridgePatternRealTimeExample
{
    public class SmsMessageSender : IMessageSender
    {
        public void SendMessage(string Message)
        {
            Console.WriteLine("'" + Message + "'   : This Message has been sent using SMS");
        }
    }
}
EmailMessageSender:

Create a class file with the name EmailMessageSender and then copy and paste the following code in it. This class also implements the IMessageSender interface and also provides implementations for the SendMessage method. This SendMessage method is used to send the message using Email.

using System;
namespace BridgePatternRealTimeExample
{
    public class EmailMessageSender : IMessageSender
    {
        public void SendMessage(string Message)
        {
            Console.WriteLine("'" + Message + "'   : This Message has been sent using Email");
        }
    }
}
Step3: Creating Abstraction (AbstractMessage )

This is going to be an abstract class. So, create a class with the name AbstractMessage and then copy and paste the following code in it. This class has one protected member i.e. messageSender 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.

namespace BridgePatternRealTimeExample
{
    public abstract class AbstractMessage
    {
        protected IMessageSender messageSender;
        public abstract void SendMessage(string Message);
    }
}

Step4: Creating Concrete Abstraction (ShortMessage and LongMessage)

These are going to be concrete classes and implement the AbstractMessage abstract class. In our example, we are going to create two concrete classes i.e. ShortMessage and LongMessage.

ShortMessage:

Create a class file with the name ShortMessage and then copy and paste the following code in it. In the constructor, we have initialized the superclass messageSender variable as well as provide the implementation for the SendMessage method.

using System;
namespace BridgePatternRealTimeExample
{
    public class ShortMessage : AbstractMessage
    {
        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:

This is the same as the ShortMessage class. Create a class file with the name LongMessage and then copy and paste the following code in it. In the constructor, initialize the messageSender variable and provide the implementation for the SendMessage method.

namespace BridgePatternRealTimeExample
{
    public class LongMessage : AbstractMessage
    {
        public LongMessage(IMessageSender messageSendor)
        {
            this.messageSender = messageSendor;
        }
        public override void SendMessage(string Message)
        {
            messageSender.SendMessage(Message);
        }
    }
}

Step5: Client

Please modify the Main method as shown below.

using System;
namespace BridgePatternRealTimeExample
{
    class Program
    {
        static void Main(string[] args)
        {
            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();
        }
    }
}

Bridge Design Pattern Real-Time Example - Send Message

In the next article, I am going to discuss the Composite Design Pattern in C# with some examples. Here, in this article, I try to explain the Bridge Design Pattern Real-Time Example – Send Message step by step using C#. I hope you understood the need and use of the Bridge Design Pattern in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *