Pattern · Core

Strategy

Define a family of interchangeable algorithms, put each in its own class, and let the client swap them at runtime — instead of a big if/else picking behaviour.

Asked atAmazonStripeUber
step 1 / 7
1/2if/else picks the algorithm
Checkout
+ pay(amt)
if/else picks the algorithm
1class Checkout {
2 void pay(int amt){
3 if (method == CARD) { ... }
4 else if (method == PAYPAL) { ... } // add one → edit here
5 }
6}
State
pay()if/else chain

One Checkout class, one pay() method — and inside it a chain of if/else, each branch a whole payment algorithm. Card here, PayPal there, crypto next.