How does Strategy help get rid of many conditional operators?
The Strategy pattern helps get rid of many if-else and switch statements,
because each behavior variant or algorithm is moved into a separate class,
and the choice of strategy becomes delegated rather than hardcoded inside a method.
1. The Problem Without the Pattern
When you need to support different ways of performing the same task, developers often use long conditional blocks:
class PaymentService {
public void pay(String method, int amount) {
if (method.equals("CARD")) {
System.out.println("Paying by card: " + amount);
} else if (method.equals("PAYPAL")) {
System.out.println("Paying via PayPal: " + amount);
} else if (method.equals("CRYPTO")) {
System.out.println("Paying with crypto: " + amount);
}
}
}The problems:
- Adding a new payment method means changing existing code.
- The class grows and loses readability.
- The Open/Closed principle is violated (the code isn't open for extension, only for modification).
2. How Strategy Does It
Each payment method turns into a separate class implementing a common interface:
interface PaymentStrategy {
void pay(int amount);
}
class CardPayment implements PaymentStrategy {
public void pay(int amount) {
System.out.println("Paying by card: " + amount);
}
}
class PayPalPayment implements PaymentStrategy {
public void pay(int amount) {
System.out.println("Paying via PayPal: " + amount);
}
}
class CryptoPayment implements PaymentStrategy {
public void pay(int amount) {
System.out.println("Paying with crypto: " + amount);
}
}The context simply delegates execution to the chosen strategy:
class PaymentService {
private PaymentStrategy strategy;
public PaymentService(PaymentStrategy strategy) {
this.strategy = strategy;
}
public void pay(int amount) {
strategy.pay(amount);
}
}3. Usage
PaymentService service = new PaymentService(new PayPalPayment());
service.pay(300); // The needed strategy is called
service = new PaymentService(new CryptoPayment());
service.pay(200); // Behavior changes without ifNow, when adding a new strategy (for example, ApplePay):
- you don't need to touch the old code;
- you just add a new
ApplePayPaymentclass and use it.
4. What Exactly Gets Eliminated
Problem in if-else | Solution via Strategy |
|---|---|
| One method manages all the algorithms | Each algorithm lives in its own class |
| You have to edit old code | We add a new strategy class |
| Code is hard to read | An interface + small independent classes |
| Hard to test | Each strategy can be tested separately |
| Rigid coupling | The context knows nothing about the implementation details |
5. Why This Works
The context calls the strategy's method through an interface, so it doesn't care what's actually under the hood. Conditions are no longer needed, you simply pass the needed object:
context.setStrategy(new CryptoPayment());Conclusion
The Strategy pattern eliminates many conditional operators, turning the choice of algorithm from "branching" into an object structure.
Summary:
Instead of
javaif (...) doA(); else if (...) doB();now it's simply
javastrategy.doAlgorithm();
- and each strategy lives separately, without a single
if.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.