Bridge Design Pattern in C#

Bridge Design Pattern in C# with Examples

In this article, I am going to discuss the Bridge Design Pattern in C# with some examples. Please read our previous article where we discussed the Decorator Design Pattern in C# with examples. The Bridge Design Pattern falls under the category of Structural Design Pattern. As part of this article, we are going to discuss the following pointers.

  1. What is the Bridge Design Pattern?
  2. Understanding Abstraction and Implementation
  3. Understanding the Bridge Design Pattern.
  4. Implementation of the Bridge Design Pattern in C#.
  5. When do we need to use the Bridge Design Pattern in C#?

What is the Bridge Design Pattern?

As per the Gang of Four definitions, the Bridge Pattern “Decouples an abstraction from its implementation so that the two can vary independently“.

In simple words, we can say that the Bridge Design Pattern is used to separate abstraction from its implementation so that both can be modified independently. The Bridge design pattern creates an interface that acts as a bridge between the abstraction and implementation classes.

Understanding Abstraction and Implementation:

Before understanding the Bridge Design Pattern, we need to aware of two things i.e. abstraction and implementation. In order to understand the Abstraction and Implementation, please have a look at the following diagram.

Understanding Abstraction and Implementation in C#

As shown in the above image, we have many electronic devices such as Bulb, AC, Fan, etc. To on and off all these electronic devices, the abstraction is a switch that has on and off. For example, if you want to on the Fan, then you just need to on the Fan switch. Similarly, if you want to on the AC, then you just need to on the AC switch. If you want to Off the Fan, then you just need to off the Switch. Here, Switch is your abstraction which abstracts how the implementation works.

Understanding the Bridge Design Pattern:

We want to design an application to manage electronic devices. As per the Bridge Design Pattern, we need to separate the implementation from the abstraction. Please have a look at the following diagram which shows the implementation.

Implementation in Bridge Design Pattern

As shown in the above image, we have two implementations i.e. Bulb and Refrigerator. Each implementation class is having two methods i.e. Start and Stop. To make things unified we make use of interface.

Then we need to create the abstraction. In our example, abstraction is nothing but the switch which is going to on and off the electronic devices. Please have a look at the following diagram.

Abstraction in Bridge Design Pattern

As you can see in the above image, here we create an interface with two methods i.e. On and Off. Then we implement that interface in the Switch class. This Switch class having the SetDevice method which is basically used to set the Device on which the operations (Start and Stop) are going to performed. The Implementation of On and Off is very straight forward. They just used to call the respective devices Start and Stop method.

Now, we need to create the test our code. In order to test this, first we need to create an instance of the Switch class and then we need to set the device using the SetDevice method. Finally, we need to call the On and Off method as shown below.

Bridge Design Pattern in C#

The complete example code is given below:
using System;

namespace BridgeDesignPattern
{
    public interface IElectronicDevice
    {
        void Start();
        void Stop();
    }
    
    public class Refrigerator : IElectronicDevice
    {
        public void Start()
        {
            Console.WriteLine("Compressor Start");
            Console.WriteLine("Ice Cooling Start");
        }

        public void Stop()
        {
            Console.WriteLine("Ice Cooling Stop");
            Console.WriteLine("Compressor Stop");
        }
    }
    
    public class Bulb : IElectronicDevice
    {
        public void Start()
        {
            Console.WriteLine("Warm up the bulb");
            Console.WriteLine("Glow the bulb");
        }
        public void Stop()
        {
            Console.WriteLine("Switch of the bulb");
        }
    }

    public interface ISwitch
    {
        void On();
        void Off();
    }

    public class Switch : ISwitch
    {
        private IElectronicDevice electronicDevice;
        public void SetDevice(IElectronicDevice electronicDevice)
        {
            this.electronicDevice = electronicDevice;
        }
        public void Off()
        {
            electronicDevice.Stop();
        }

        public void On()
        {
            electronicDevice.Start();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Switch s = new Switch();
            s.SetDevice(new Bulb());
            s.On();
            s.Off();
            
            s.SetDevice(new Refrigerator());
            s.On();
            s.Off();

            Console.Read();
        }
    }
}
When do we need to use the Bridge Design Pattern in C#?

We need to use the Bridge Design Pattern when

  1. We want to avoid a tight coupling binding between an abstraction and its implementation. 
  2. We want both the abstractions and their implementation classes are extensible by subclasses.
  3. The changes in the implementation of an abstraction should have no impact on clients.
  4. When we want to share an implementation among multiple objects. 
  5. When we want to hide the implementation of an abstraction completely from clients. 

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 in C# step by step with some simple examples. I hope you understood the need and use of the Bridge Design Pattern.

Leave a Reply

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