Advanced 25 min read

Strategy Pattern

By the end of this lesson, you'll be able to:

  • Define the strategy pattern and how it differs from the factory pattern
  • Implement interchangeable strategies behind a shared interface
  • Recognize that Lesson 7's IOverdraftPolicy interface was already an example of this pattern

Prerequisites: "Factory Pattern"

The pattern Lesson 7 already used, named explicitly

public interface IOverdraftPolicy {
    Boolean allows(Decimal balance, Decimal withdrawalAmount);
}

This interface, with PersonalOverdraftPolicy and BusinessOverdraftPolicy as two interchangeable implementations, is a textbook strategy pattern: calling code depends only on the IOverdraftPolicy interface, entirely unaware of which concrete strategy is actually running underneath.

Strategy versus factory: two different jobs

The strategy pattern is about shape — defining an interface so multiple interchangeable implementations can exist side by side, each satisfying the same contract. The factory pattern (Lesson 7) is about selection — deciding which of those interchangeable strategies to use at a given moment. They're frequently used together, as Lesson 7 already showed, but they answer different questions.

A second strategy example: interest calculation

public interface IInterestCalculator {
    Decimal calculateMonthlyInterest(Decimal balance);
}

public class StandardInterestCalculator implements IInterestCalculator {
    public Decimal calculateMonthlyInterest(Decimal balance) {
        return balance * 0.001; // 0.1% monthly
    }
}

public class PremiumInterestCalculator implements IInterestCalculator {
    public Decimal calculateMonthlyInterest(Decimal balance) {
        return balance * 0.002; // 0.2% monthly
    }
}

TransactionBalanceService (Lesson 6) could accept an IInterestCalculator via its constructor — dependency injection supplying whichever strategy a factory decided was appropriate for a given account, the same three patterns from Lessons 6-8 working together on a genuinely new piece of business logic.

Exercise

As a comment, explain the difference in responsibility between the strategy pattern and the factory pattern, using IOverdraftPolicy and OverdraftPolicyFactory as the concrete example.

Show hint

One is about the shape of interchangeable implementations, the other is about choosing between them.

APEX

Strategy Pattern Quiz

1. Which lesson's example was already an instance of the strategy pattern, even before this lesson named it explicitly?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

The strategy pattern defines a family of interchangeable algorithms behind a shared interface, letting the algorithm used at a given moment vary independently of the code that calls it — Lesson 7's IOverdraftPolicy was already an instance of this pattern, used alongside a factory to select the right one.