behavioral
Strategy
Encapsulate a family of algorithms or behaviors behind a common contract so they can be selected and swapped cleanly.
Problem
A system needs to switch between interchangeable behaviors without hardcoding branching logic everywhere.
Integration notes
Strategies can cross codebases through shared contracts, API selection rules, or runtime configuration.
Tradeoffs
- Adds more moving parts than a direct conditional approach
- Works best when behavior variations are real and likely to grow
Languages
typescript, java, python, angular, react, react-native, csharp, dotnet
Platforms
Web, Backend, Services
Variants
Scenarios
Payment processing
A checkout flow delegates payment behavior to a selected strategy so the order flow stays stable while payment methods vary.
Language examples
Core languages
Frameworks and libraries
Payment processingtypescript
The checkout service depends on a payment contract instead of concrete payment logic, so payment methods can vary without changing the caller.
TypeScript
interface PaymentStrategy {
pay(amount: number): void;
}
class CreditCardPayment implements PaymentStrategy {
pay(amount: number): void {
console.log(`Paid $${amount} with credit card`);
}
}
class PayPalPayment implements PaymentStrategy {
pay(amount: number): void {
console.log(`Paid $${amount} with PayPal`);
}
}
class CheckoutService {
constructor(private strategy: PaymentStrategy) {}
checkout(amount: number): void {
this.strategy.pay(amount);
}
}
const checkout = new CheckoutService(new PayPalPayment());
checkout.checkout(250);Real-world examples
- Checkout payment providers: Choose Stripe, PayPal, or bank transfer behavior behind a stable checkout flow.
- Shipping rate engines: Switch between standard, express, and international pricing algorithms.
- Messaging channels: Select email, SMS, or push delivery based on user preference or channel availability.