🧩 State × Strategy
✅ Combination Intent​
- Use the
State
pattern to switch behavior based on the current state - Use the
Strategy
pattern to flexibly change how the behavior is executed
This combination allows separation and flexibility across both state transitions and execution strategy.
✅ Common Use Cases​
- When behavior changes depending on the current state and the algorithm (strategy) used
- For example, "Can payment be processed?" + "How should it be processed?"
- Useful in: payment flows, feature gating by user membership level, reservation logic, etc.
✅ UML Class Diagram​
✅ Code Example​
- TypeScript
- PHP
- Python
interface PaymentStrategy {
pay(amount: number): void;
}
class CreditCardPayment implements PaymentStrategy {
pay(amount: number): void {
console.log(`[CreditCard] Paid ${amount}`);
}
}
class PayPalPayment implements PaymentStrategy {
pay(amount: number): void {
console.log(`[PayPal] Paid ${amount}`);
}
}
interface PaymentState {
handle(amount: number): void;
setStrategy(strategy: PaymentStrategy): void;
}
class ReadyState implements PaymentState {
constructor(private strategy: PaymentStrategy) {}
handle(amount: number): void {
console.log("Ready to process payment");
this.strategy.pay(amount);
}
setStrategy(strategy: PaymentStrategy): void {
this.strategy = strategy;
}
}
class DisabledState implements PaymentState {
handle(amount: number): void {
console.log("Payment is currently disabled");
}
setStrategy(strategy: PaymentStrategy): void {
console.log("Cannot set strategy while disabled");
}
}
class PaymentContext {
constructor(private state: PaymentState) {}
setState(state: PaymentState) {
this.state = state;
}
setStrategy(strategy: PaymentStrategy) {
this.state.setStrategy(strategy);
}
pay(amount: number) {
this.state.handle(amount);
}
}
// Usage
const context = new PaymentContext(new ReadyState(new CreditCardPayment()));
context.pay(100);
context.setStrategy(new PayPalPayment());
context.pay(200);
context.setState(new DisabledState());
context.pay(300);
<?php
interface PaymentStrategy {
public function pay(int $amount): void;
}
class CreditCardPayment implements PaymentStrategy {
public function pay(int $amount): void {
echo "[CreditCard] Paid {$amount}\n";
}
}
class PayPalPayment implements PaymentStrategy {
public function pay(int $amount): void {
echo "[PayPal] Paid {$amount}\n";
}
}
interface PaymentState {
public function handle(int $amount): void;
public function setStrategy(PaymentStrategy $strategy): void;
}
class ReadyState implements PaymentState {
private PaymentStrategy $strategy;
public function __construct(PaymentStrategy $strategy) {
$this->strategy = $strategy;
}
public function handle(int $amount): void {
echo "Ready to process payment\n";
$this->strategy->pay($amount);
}
public function setStrategy(PaymentStrategy $strategy): void {
$this->strategy = $strategy;
}
}
class DisabledState implements PaymentState {
public function handle(int $amount): void {
echo "Payment is currently disabled\n";
}
public function setStrategy(PaymentStrategy $strategy): void {
echo "Cannot set strategy while disabled\n";
}
}
class PaymentContext {
private PaymentState $state;
public function __construct(PaymentState $state) {
$this->state = $state;
}
public function setState(PaymentState $state): void {
$this->state = $state;
}
public function setStrategy(PaymentStrategy $strategy): void {
$this->state->setStrategy($strategy);
}
public function pay(int $amount): void {
$this->state->handle($amount);
}
}
// Usage
$context = new PaymentContext(new ReadyState(new CreditCardPayment()));
$context->pay(100);
$context->setStrategy(new PayPalPayment());
$context->pay(200);
$context->setState(new DisabledState());
$context->pay(300);
from abc import ABC, abstractmethod
class PaymentStrategy(ABC):
@abstractmethod
def pay(self, amount: int) -> None:
pass
class CreditCardPayment(PaymentStrategy):
def pay(self, amount: int) -> None:
print(f"[CreditCard] Paid {amount}")
class PayPalPayment(PaymentStrategy):
def pay(self, amount: int) -> None:
print(f"[PayPal] Paid {amount}")
class PaymentState(ABC):
@abstractmethod
def handle(self, amount: int) -> None:
pass
@abstractmethod
def set_strategy(self, strategy: PaymentStrategy) -> None:
pass
class ReadyState(PaymentState):
def __init__(self, strategy: PaymentStrategy):
self.strategy = strategy
def handle(self, amount: int) -> None:
print("Ready to process payment")
self.strategy.pay(amount)
def set_strategy(self, strategy: PaymentStrategy) -> None:
self.strategy = strategy
class DisabledState(PaymentState):
def handle(self, amount: int) -> None:
print("Payment is currently disabled")
def set_strategy(self, strategy: PaymentStrategy) -> None:
print("Cannot set strategy while disabled")
class PaymentContext:
def __init__(self, state: PaymentState):
self.state = state
def set_state(self, state: PaymentState) -> None:
self.state = state
def set_strategy(self, strategy: PaymentStrategy) -> None:
self.state.set_strategy(strategy)
def pay(self, amount: int) -> None:
self.state.handle(amount)
# Usage
context = PaymentContext(ReadyState(CreditCardPayment()))
context.pay(100)
context.set_strategy(PayPalPayment())
context.pay(200)
context.set_state(DisabledState())
context.pay(300)
✅ Explanation​
PaymentContext
holds a reference to the current state (PaymentState
) and delegates logicReadyState
andDisabledState
implement state-specific behavior viahandle()
- The
Strategy
(e.g.,CreditCardPayment
,PayPalPayment
) handles the actual processing logic - Execution flow:
- In
ReadyState
, the context allows the strategy to run - In
DisabledState
, the operation is blocked
- In
✅ Summary​
- State pattern clearly separates logic based on the object’s state
- Strategy pattern allows for flexible substitution of processing logic
- The architecture supports high extensibility and maintainability, as each axis (state and strategy) is decoupled and independently swappable