Skip to main content

🧩 Strategy × Template Method

✅ Combined Intent

  • Use Template Method to define the fixed flow of a notification process
  • Delegate the variable part—the delivery method—to a Strategy

This design centralizes a shared notification flow (e.g., logging, message formatting, pre/post-processing), while allowing the delivery mechanism (email, Slack, etc.) to be easily swapped through strategy injection.

✅ Common Use Cases

  • When the overall process is consistent (e.g., logging or validation), but the way messages are delivered or formatted needs to be flexible
  • When you want to send real emails in production but use mock notifications in testing
  • When you want to reuse formatting logic or logging behavior across different types of notifications

✅ UML Class Diagram

✅ Code Example

interface NotificationStrategy {
sendMessage(to: string, content: string): void;
}

class EmailStrategy implements NotificationStrategy {
sendMessage(to: string, content: string): void {
console.log(`[Email] To: ${to}, Message: ${content}`);
}
}

class SlackStrategy implements NotificationStrategy {
sendMessage(to: string, content: string): void {
console.log(`[Slack] To: ${to}, Message: ${content}`);
}
}

abstract class Notifier {
constructor(private strategy: NotificationStrategy) {}

send(user: string, message: string): void {
console.log("--- Sending Start ---");
const content = this.formatMessage(user, message);
this.strategy.sendMessage(user, content);
console.log("--- Sending End ---");
}

protected abstract formatMessage(user: string, message: string): string;
}

class AlertNotifier extends Notifier {
protected formatMessage(user: string, message: string): string {
return `[ALERT] ${user}: ${message}`;
}
}

// 使用例
const notifier = new AlertNotifier(new EmailStrategy());
notifier.send("taro@example.com", "サーバー障害が発生しました");

✅ Explanation

  • Notifier provides the shared template (logging, formatting, dispatching)
  • formatMessage is left abstract for subclasses like AlertNotifier to implement (Template Method)
  • The delivery strategy (EmailStrategy, SlackStrategy) is injected, allowing dynamic switching (Strategy)

By fixing the overall process with a template and externalizing variable behavior as strategies, this design achieves both consistency and flexibility.

✅ Summary

  • Template Method enforces a consistent flow for notifications
  • Strategy decouples and enables flexible delivery behavior
  • The design is extensible, testable, reusable, and easy to maintain