π§© 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β
- TypeScript
- PHP
- Python
// 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(); // β γγ§γ«ε
¬ιζΈγΏ
<?php
interface PublishStrategy {
public function publish(): void;
}
class DraftStrategy implements PublishStrategy {
public function publish(): void {
echo "γ¬γγ₯γΌδΎι ΌγιδΏ‘\n";
}
}
class ReviewStrategy implements PublishStrategy {
public function publish(): void {
echo "ε
¬ιγγΎγγ\n";
}
}
class PublishedStrategy implements PublishStrategy {
public function publish(): void {
echo "γγ§γ«ε
¬ιζΈγΏγ§γ\n";
}
}
class Document {
public function __construct(private PublishStrategy $strategy) {}
public function setStrategy(PublishStrategy $strategy): void {
$this->strategy = $strategy;
}
public function publish(): void {
$this->strategy->publish();
}
}
// ε©η¨δΎ
$doc = new Document(new DraftStrategy());
$doc->publish(); // β γ¬γγ₯γΌδΎι Ό
$doc->setStrategy(new ReviewStrategy());
$doc->publish(); // β ε
¬ι
$doc->setStrategy(new PublishedStrategy());
$doc->publish(); // β γγ§γ«ε
¬ιζΈγΏ
from abc import ABC, abstractmethod
class PublishStrategy(ABC):
@abstractmethod
def publish(self):
pass
class DraftStrategy(PublishStrategy):
def publish(self):
print("γ¬γγ₯γΌδΎι ΌγιδΏ‘")
class ReviewStrategy(PublishStrategy):
def publish(self):
print("ε
¬ιγγΎγγ")
class PublishedStrategy(PublishStrategy):
def publish(self):
print("γγ§γ«ε
¬ιζΈγΏγ§γ")
class Document:
def __init__(self, strategy: PublishStrategy):
self._strategy = strategy
def set_strategy(self, strategy: PublishStrategy):
self._strategy = strategy
def publish(self):
self._strategy.publish()
# ε©η¨δΎ
doc = Document(DraftStrategy())
doc.publish() # β γ¬γγ₯γΌδΎι Ό
doc.set_strategy(ReviewStrategy())
doc.publish() # β ε
¬ι
doc.set_strategy(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
- Represented by
-
ConcreteStrategy: Implements the strategy interface with specific behavior
- Represented by
DraftStrategy
,ReviewStrategy
, andPublishedStrategy
- Represented by
-
Context: Uses the strategy and allows switching behavior dynamically
- Represented by
Document
- Represented by
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 requestReviewStrategy
: Publishes the documentPublishedStrategy
: Indicates the document is already published
- Concrete strategy classes implementing
-
Document
- Context class
- Receives a
PublishStrategy
in its constructor and executes the strategy via thepublish
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.