Here, we will explain Bridge Design Pattern with example in C#
The Bridge Design Pattern is a structural pattern that decouples an abstraction from its implementation, allowing both to vary independently. This is useful when you want to avoid a permanent binding between an abstraction and its implementation, enabling flexibility in the system design.
Decoupling
Decoupling an abstraction means separating the high-level concepts (abstractions) of a system from their low-level details (implementations). In simpler terms, it allows the abstract part of the system (the general idea or interface) to operate independently of the concrete part (how it's actually done).
Example - let's say you have an interface for a Shape (like a circle or rectangle). The details of how each shape is drawn (e.g., with OpenGL, DirectX) are the implementation. By decoupling, you make sure that the Shape abstraction is independent of how the drawing happens, so the shape can use any drawing method without the need to change the shape code.
A real-world example could be a remote control and television of Bridge Design Pattern. We will explain in detail lator part of the article.
A real-world example could be a remote control and television. The remote control (abstraction) can work with different television brands (implementation). The remote control has standard operations (like TurnOn, TurnOff), and each brand’s television can have its unique implementation of how those operations are performed.
In this scenario, the remote control does not need to know how the television works internally. It interacts through a common interface, making it easy to extend both the remote control and the television independently.
+--------------------------+
| ITelevision |
|--------------------------|
| + On() |
| + Off() |
| + SwitchChannel(int) |
+--------------------------+
^ ^
| |
+-------------------+ +-------------------+
| SonyTelevision | | SamsungTelevision |
|-------------------| |-------------------|
| + On() | | + On() |
| + Off() | | + Off() |
| + SwitchChannel() | | + SwitchChannel() |
+-------------------+ +-------------------+
+--------------------------+
| RemoteControl |
|--------------------------|
| - television: ITelevision|
|--------------------------|
| + TurnOn() |
| + TurnOff() |
+--------------------------+
^
|
|
+-------------------------------+
| AdvancedRemoteControl |
+-------------------------------+
| + SetChannel(int channel) |
+-------------------------------+
| Inherits TurnOn() |
| Inherits TurnOff() |
+-------------------------------+
Here is a C# implementation of the Bridge Pattern using the example of a remote control (abstraction) and different television brands (implementation).
using System;
// Abstraction: Remote control
abstract class RemoteControl
{
protected ITelevision television;
public RemoteControl(ITelevision tv)
{
television = tv;
}
public abstract void TurnOn();
public abstract void TurnOff();
}
// Refined Abstraction: Advanced remote control with extra functionality
class AdvancedRemoteControl : RemoteControl
{
public AdvancedRemoteControl(ITelevision tv) : base(tv) { }
public override void TurnOn()
{
television.On();
}
public override void TurnOff()
{
television.Off();
}
public void SetChannel(int channel)
{
television.SwitchChannel(channel);
}
}
// Implementor: Interface for the television
interface ITelevision
{
void On();
void Off();
void SwitchChannel(int channel);
}
// Concrete Implementations: Different brands of televisions
class SonyTelevision : ITelevision
{
public void On()
{
Console.WriteLine("Sony TV is turned ON.");
}
public void Off()
{
Console.WriteLine("Sony TV is turned OFF.");
}
public void SwitchChannel(int channel)
{
Console.WriteLine($"Sony TV switched to channel {channel}.");
}
}
class SamsungTelevision : ITelevision
{
public void On()
{
Console.WriteLine("Samsung TV is turned ON.");
}
public void Off()
{
Console.WriteLine("Samsung TV is turned OFF.");
}
public void SwitchChannel(int channel)
{
Console.WriteLine($"Samsung TV switched to channel {channel}.");
}
}
// Client code
class Program
{
static void Main(string[] args)
{
// Creating a Sony TV with a remote control
ITelevision sonyTv = new SonyTelevision();
RemoteControl remoteSony = new AdvancedRemoteControl(sonyTv);
remoteSony.TurnOn(); // Output: Sony TV is turned ON.
((AdvancedRemoteControl)remoteSony).SetChannel(5); // Output: Sony TV switched to channel 5.
remoteSony.TurnOff(); // Output: Sony TV is turned OFF.
// Creating a Samsung TV with a remote control
ITelevision samsungTv = new SamsungTelevision();
RemoteControl remoteSamsung = new AdvancedRemoteControl(samsungTv);
remoteSamsung.TurnOn(); // Output: Samsung TV is turned ON.
((AdvancedRemoteControl)remoteSamsung).SetChannel(10); // Output: Samsung TV switched to channel 10.
remoteSamsung.TurnOff(); // Output: Samsung TV is turned OFF.
}
}
Output