Skip to main content

🧩 Bridge Pattern

✅ Intent

  • Decouple abstraction from implementation so they can vary independently
  • Enables the composition of independent dimensions (e.g., notification type × delivery method)

✅ Motivation

  • You want to vary what is being done (e.g., normal vs urgent notification) separately from how it’s done (e.g., Email vs Slack)
  • Avoid class explosion like UrgentEmailNotification, NormalEmailNotification, UrgentSlackNotification, etc.

✅ When to Use

  • When you have two or more independent dimensions of variability
  • When you want to extend abstraction and implementation hierarchies separately

✅ Code Example

// 実装クラス(通知手段)
interface Notifier {
sendMessage(to: string, content: string): void;
}

class EmailNotifier implements Notifier {
sendMessage(to: string, content: string): void {
console.log("件名: お知らせ");
console.log(`本文: ${content}`);
console.log(`宛先: ${to}@example.com`);
}
}

class SlackNotifier implements Notifier {
sendMessage(to: string, content: string): void {
console.log(`Slack宛: ${to}`);
console.log(`内容: ${content}`);
}
}

// 抽象クラス(通知処理)
abstract class Notification {
constructor(protected notifier: Notifier) {}

abstract notify(user: string, message: string): void;
}

// 拡張された抽象クラス
class NormalNotification extends Notification {
notify(user: string, message: string): void {
this.notifier.sendMessage(user, message);
}
}

class UrgentNotification extends Notification {
notify(user: string, message: string): void {
this.notifier.sendMessage(user, `[緊急] ${message}`);
}
}

// 利用例
const emailUrgent = new UrgentNotification(new EmailNotifier());
emailUrgent.notify("hiroshi", "サーバーが落ちました");

const slackNormal = new NormalNotification(new SlackNotifier());
slackNormal.notify("hiroshi", "定例ミーティングは15時です");

✅ Explanation

This code uses the Bridge pattern to separate how notifications are sent (Notifier) from how they're formatted or triggered (Notification).

This allows you to mix and match behavior without creating deeply nested inheritance hierarchies.

1. Bridge Pattern Overview

  • Abstraction: Defines the abstract logic and holds a reference to the implementation
    Notification

  • RefinedAbstraction: Subclasses of the abstraction with specific behavior
    NormalNotification, UrgentNotification

  • Implementor: Interface for platform-specific logic
    Notifier

  • ConcreteImplementor: Concrete implementations of delivery mechanisms
    EmailNotifier, SlackNotifier

2. Key Classes and Responsibilities

  • Notifier

    • Defines how to send messages (sendMessage(to, content))
  • EmailNotifier, SlackNotifier

    • Send messages through different platforms
  • Notification

    • Defines the notification logic and delegates sending to Notifier
  • NormalNotification, UrgentNotification

    • Customize message formats or urgency levels

3. UML Class Diagram

4. Benefits of the Bridge Pattern

  • Independent evolution: Add new delivery mechanisms or notification formats independently
  • Avoid class explosion: No need for one class per combination
  • Improved flexibility: Behavior can be composed at runtime

This design is particularly useful when you're dealing with multiple variations that shouldn't be hardcoded. The bridge enables modular design and better separation of concerns.