loader

Ever been frustrated that your remote control and TV seem to speak different languages? With the Bridge Pattern, you can decouple the remote (the abstraction) from the TV (the implementation) so they can evolve independently—just like a universal remote that works with any TV!

Why Bother with It?

  • Separation of Concerns: Just like you don’t want to mess with the internals of your TV every time you want to change the channel, the Bridge Pattern keeps the high-level control (the remote) separate from the low-level implementation (the TV).
  • Independent Variation: Want to upgrade your remote without buying a new TV? Or vice versa? With Bridge, you can modify either side without breaking the other.
  • Enhanced Flexibility: Your remote can work with any TV—Sony, Samsung, or even that vintage set in the attic. The Bridge Pattern makes your code adaptable to different implementations.

How It Works (In Plain English)

  1. The Abstraction: Think of this as your remote control. It defines the high-level operations like turning the TV on/off or setting a channel.
  2. The Implementation: This represents the actual TV, with its own specific way of turning on, turning off, and changing channels.
  3. The Bridge: The remote doesn’t care what kind of TV it’s controlling. It talks to the TV through a common interface, allowing you to mix and match remotes and TVs as needed.

C# Code Example

C#
using System;

// The Implementation Interface: Represents a generic TV.
public interface IDevice
{
    void TurnOn();
    void TurnOff();
    void SetChannel(int channel);
}

// Concrete Implementation: Sony TV.
public class SonyTV : IDevice
{
    public void TurnOn() => Console.WriteLine("Sony TV turned on.");
    public void TurnOff() => Console.WriteLine("Sony TV turned off.");
    public void SetChannel(int channel) => Console.WriteLine("Sony TV set to channel " + channel);
}

// Concrete Implementation: Samsung TV.
public class SamsungTV : IDevice
{
    public void TurnOn() => Console.WriteLine("Samsung TV turned on.");
    public void TurnOff() => Console.WriteLine("Samsung TV turned off.");
    public void SetChannel(int channel) => Console.WriteLine("Samsung TV set to channel " + channel);
}

// The Abstraction: Defines the remote control interface.
public abstract class RemoteControl
{
    protected IDevice device;
    public RemoteControl(IDevice device)
    {
        this.device = device;
    }
    
    public abstract void TurnOn();
    public abstract void TurnOff();
    public abstract void SetChannel(int channel);
}

// Refined Abstraction: Basic Remote.
public class BasicRemote : RemoteControl
{
    public BasicRemote(IDevice device) : base(device) { }
    
    public override void TurnOn() => device.TurnOn();
    public override void TurnOff() => device.TurnOff();
    public override void SetChannel(int channel) => device.SetChannel(channel);
}

// Extended Abstraction: Advanced Remote with extra features.
public class AdvancedRemote : RemoteControl
{
    public AdvancedRemote(IDevice device) : base(device) { }
    
    public override void TurnOn() => device.TurnOn();
    public override void TurnOff() => device.TurnOff();
    public override void SetChannel(int channel) => device.SetChannel(channel);
    
    public void Mute() => Console.WriteLine("Device muted.");
}

// Client Code
public class Program
{
    public static void Main()
    {
        // Use BasicRemote with a Sony TV
        IDevice sony = new SonyTV();
        RemoteControl basicRemote = new BasicRemote(sony);
        basicRemote.TurnOn();
        basicRemote.SetChannel(10);
        basicRemote.TurnOff();
        
        Console.WriteLine();

        // Use AdvancedRemote with a Samsung TV
        IDevice samsung = new SamsungTV();
        AdvancedRemote advancedRemote = new AdvancedRemote(samsung);
        advancedRemote.TurnOn();
        advancedRemote.SetChannel(20);
        advancedRemote.Mute();
        advancedRemote.TurnOff();
    }
}

The Takeaway

The Bridge Pattern is like having a universal remote that works with any TV, regardless of brand. It separates the “what” from the “how,” allowing you to change the remote or the TV independently. This keeps your code flexible, maintainable, and ready to adapt as new technologies emerge.

Happy coding, and may your remotes always find the right channel!