Skip to main content

🧩 Strategy Pattern

βœ… Intent​

  • Treat behavior as a strategy injected from the outside, rather than being determined by internal state
  • State transitions are managed externally

βœ… Motivation​

  • Useful when you want to completely separate state from behavior
  • Ideal when the focus is purely on switching behavior dynamically

βœ… When to Use​

  • When you don't need state management, and simply want to switch logic at runtime

βœ… Code Example​

// Strategy むンターフェース
interface PublishStrategy {
publish(): void;
}

// ε„ζˆ¦η•₯
class DraftStrategy implements PublishStrategy {
publish(): void {
console.log("レビγƒ₯ー依頼を送俑");
}
}

class ReviewStrategy implements PublishStrategy {
publish(): void {
console.log("ε…¬ι–‹γ—γΎγ—γŸ");
}
}

class PublishedStrategy implements PublishStrategy {
publish(): void {
console.log("γ™γ§γ«ε…¬ι–‹ζΈˆγΏγ§γ™");
}
}

// γ‚³γƒ³γƒ†γ‚­γ‚Ήγƒˆ
class Document {
constructor(private strategy: PublishStrategy) {}

setStrategy(strategy: PublishStrategy): void {
this.strategy = strategy;
}

publish(): void {
this.strategy.publish();
}
}

// εˆ©η”¨δΎ‹
const doc = new Document(new DraftStrategy());
doc.publish(); // β†’ レビγƒ₯ー依頼

doc.setStrategy(new ReviewStrategy());
doc.publish(); // β†’ ε…¬ι–‹

doc.setStrategy(new PublishedStrategy());
doc.publish(); // β†’ γ™γ§γ«ε…¬ι–‹ζΈˆγΏ

βœ… Explanation​

This code applies the Strategy pattern by extracting the document publishing process into a strategy (PublishStrategy)
that can be dynamically switched at runtime.
The Strategy pattern encapsulates algorithms or operations in separate classes, making them interchangeable.

1. Overview of the Strategy Pattern​

  • Strategy: Defines a common interface to allow interchangeable behavior

    • Represented by PublishStrategy
  • ConcreteStrategy: Implements the strategy interface with specific behavior

    • Represented by DraftStrategy, ReviewStrategy, and PublishedStrategy
  • Context: Uses the strategy and allows switching behavior dynamically

    • Represented by Document

2. Key Classes and Their Roles​

  • PublishStrategy

    • Common interface for publishing behavior
    • Declares the method publish(): void
  • DraftStrategy, ReviewStrategy, PublishedStrategy

    • Concrete strategy classes implementing PublishStrategy
    • Provide different publishing behaviors:
      • DraftStrategy: Sends a review request
      • ReviewStrategy: Publishes the document
      • PublishedStrategy: Indicates the document is already published
  • Document

    • Context class
    • Receives a PublishStrategy in its constructor and executes the strategy via the publish method
    • Supports dynamic strategy switching via setStrategy

3. UML Class Diagram​

4. Benefits of the Strategy Pattern​

  • Flexibility: Easily switch between different behaviors at runtime
  • Single Responsibility Principle: Each strategy is isolated in its own class, improving maintainability
  • Extensibility: New strategies can be added simply by implementing PublishStrategy

This design is highly effective when algorithms or operations need to be swapped flexibly,
providing strong support for code extensibility and maintainability.