Skip to main content

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:

java
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:

java
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:

java
class PaymentService { private PaymentStrategy strategy; public PaymentService(PaymentStrategy strategy) { this.strategy = strategy; } public void pay(int amount) { strategy.pay(amount); } }

3. Usage

java
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 if

Now, when adding a new strategy (for example, ApplePay):

  • you don't need to touch the old code;
  • you just add a new ApplePayPayment class and use it.

4. What Exactly Gets Eliminated

Problem in if-elseSolution via Strategy
One method manages all the algorithmsEach algorithm lives in its own class
You have to edit old codeWe add a new strategy class
Code is hard to readAn interface + small independent classes
Hard to testEach strategy can be tested separately
Rigid couplingThe 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:

java
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

java
if (...) doA(); else if (...) doB();

now it's simply

java
strategy.doAlgorithm();
  • and each strategy lives separately, without a single if.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.