> For the complete documentation index, see [llms.txt](https://al-mamun.gitbook.io/al-mamun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://al-mamun.gitbook.io/al-mamun/design-patterns/bridge-pattern.md).

# Bridge Pattern

The **Bridge Design Pattern** is a **structural design pattern** that separates an abstraction from its implementation so that both can change independently.

It is useful when:

* You want to avoid a huge number of subclasses
* You want abstraction and implementation to evolve separately
* You need runtime switching between implementations

***

## Real Life Example

Think about a **Remote Control** and **TV**.

* Remote = Abstraction
* TV Brand = Implementation

A remote can work with:

* Sony TV
* Samsung TV
* LG TV

Instead of creating:

* SonyBasicRemote
* SonyAdvancedRemote
* SamsungBasicRemote
* SamsungAdvancedRemote

You separate them using Bridge Pattern.

***

## Structure

```
Abstraction
    |
    |----> Implementor
```

Main Parts:

1. Abstraction
2. Refined Abstraction
3. Implementor
4. Concrete Implementor

***

## Laravel / PHP Example

### Scenario

We want to send notifications through different channels:

* Email
* SMS
* WhatsApp

And notification types:

* Order Notification
* Payment Notification

Without Bridge Pattern, classes explode.

***

## Without Bridge Pattern

```
OrderEmailNotification
OrderSMSNotification
OrderWhatsappNotification

PaymentEmailNotification
PaymentSMSNotification
PaymentWhatsappNotification
```

Too many classes.

***

## With Bridge Pattern

We separate:

* Notification Type → Abstraction
* Sending Channel → Implementation

***

## Step 1: Implementor Interface

```
interface MessageSender
{
    public function send(string $message);
}
```

***

## Step 2: Concrete Implementations

### Email Sender

```
class EmailSender implements MessageSender
{
    public function send(string $message)
    {
        echo "Sending Email: {$message}";
    }
}
```

### SMS Sender

```
class SmsSender implements MessageSender
{
    public function send(string $message)
    {
        echo "Sending SMS: {$message}";
    }
}
```

### WhatsApp Sender

```
class WhatsAppSender implements MessageSender
{
    public function send(string $message)
    {
        echo "Sending WhatsApp: {$message}";
    }
}
```

***

## Step 3: Abstraction

```
abstract class Notification
{
    protected MessageSender $sender;

    public function __construct(MessageSender $sender)
    {
        $this->sender = $sender;
    }

    abstract public function notify();
}
```

***

## Step 4: Refined Abstractions

### Order Notification

```
class OrderNotification extends Notification
{
    public function notify()
    {
        $this->sender->send("Your order has been placed.");
    }
}
```

### Payment Notification

```
class PaymentNotification extends Notification
{
    public function notify()
    {
        $this->sender->send("Your payment was successful.");
    }
}
```

***

## Step 5: Usage

```
$emailSender = new EmailSender();
$smsSender = new SmsSender();

$orderNotification = new OrderNotification($emailSender);
$orderNotification->notify();

$paymentNotification = new PaymentNotification($smsSender);
$paymentNotification->notify();
```

***

## Output

```
Sending Email: Your order has been placed.
Sending SMS: Your payment was successful.
```

***

## How Bridge Works

Here:

| Part                  | Responsibility       |
| --------------------- | -------------------- |
| Notification          | Abstraction          |
| OrderNotification     | Refined Abstraction  |
| MessageSender         | Implementor          |
| EmailSender/SmsSender | Concrete Implementor |

Both sides can change independently.

You can:

* Add new notification types
* Add new sending channels

Without modifying existing code.

***

## Advantages

### 1. Reduces Class Explosion

Instead of:

```
3 notification types × 4 channels = 12 classes
```

You only maintain:

* Notification hierarchy
* Sender hierarchy

***

### 2. Follows SOLID Principles

Especially:

* Open/Closed Principle
* Composition over Inheritance

***

### 3. Runtime Flexibility

You can switch implementations dynamically.

```
$notification = new OrderNotification(new WhatsAppSender());
```

***

## Another Simple Example

### Device + Remote

```
interface Device
{
    public function on();
    public function off();
}
```

```
class TV implements Device
{
    public function on()
    {
        echo "TV ON";
    }

    public function off()
    {
        echo "TV OFF";
    }
}
```

```
class Radio implements Device
{
    public function on()
    {
        echo "Radio ON";
    }

    public function off()
    {
        echo "Radio OFF";
    }
}
```

***

### Remote Abstraction

```
class RemoteControl
{
    protected Device $device;

    public function __construct(Device $device)
    {
        $this->device = $device;
    }

    public function togglePower()
    {
        $this->device->on();
    }
}
```

***

### Usage

```
$tvRemote = new RemoteControl(new TV());
$tvRemote->togglePower();

$radioRemote = new RemoteControl(new Radio());
$radioRemote->togglePower();
```

***

## When to Use Bridge Pattern

Use Bridge Pattern when:

✅ Multiple dimensions vary independently\
✅ You want composition over inheritance\
✅ You want runtime implementation switching\
✅ Too many subclasses are being created

***

## Bridge vs Strategy

| Bridge                                    | Strategy                    |
| ----------------------------------------- | --------------------------- |
| Separates abstraction from implementation | Encapsulates algorithm      |
| Structural Pattern                        | Behavioral Pattern          |
| Focus on scalability                      | Focus on behavior switching |

***

## Short Interview Definition

> The Bridge Pattern separates abstraction from implementation so both can vary independently using composition instead of inheritance.
