Fundamentals 6 min read

Mastering the Strategy Pattern: When and How to Replace Complex if/else

This article explains the Strategy (Policy) pattern, detailing its definition, class diagram, typical use cases, advantages and drawbacks, compares it with Factory and State patterns, and provides a practical Java implementation with sample code and analysis of related JDK components.

JavaEdge
JavaEdge
JavaEdge
Mastering the Strategy Pattern: When and How to Replace Complex if/else

Introduction

Strategy pattern (also called Policy pattern) defines a family of algorithms, encapsulates each one, and makes them interchangeable.

Definition

According to Wikipedia, an object may have a behavior that has different implementations in different contexts, e.g., personal income tax calculation differs between the US and China.

Define a family of algorithms, encapsulate each one, and make them interchangeable.

Types

Common if/else structure.

Main Idea

Define a generic problem, implement it with different algorithms, and encapsulate these algorithms behind a unified interface. The pattern relies on OOP inheritance and polymorphism.

Main Roles

Context : encapsulates the client and hides the strategy.

Strategy : abstract strategy interface defining required methods.

ConcreteStrategy : concrete implementations of the algorithm.

Class Diagram

Source Code Overview

Abstract strategy role: a normal interface defining one or more algorithms.

Applicable Scenarios

When an object has some behavior that can vary while other behavior remains stable.

When the system needs to choose dynamically among several algorithms.

Advantages

Conforms to the Open/Closed principle.

Eliminates complex conditional statements, reducing coupling.

Improves algorithm confidentiality and security; client only knows the business function.

Disadvantages

Client must know all concrete strategy classes to decide which to use.

Can lead to a proliferation of strategy classes.

Differences with Related Patterns

Vs. Factory Pattern

Both are behavioral, but Factory creates objects while Strategy changes behavior of existing objects.

Vs. State Pattern

If a class has multiple possible behaviors that change with state, State pattern handles automatic transitions without client awareness, unlike Strategy which requires explicit selection.

Practical Example (Java)

Implementation of promotion strategies (discount, cashback, full reduction) using the Strategy pattern combined with a Factory to avoid excessive if/else statements.

Includes interfaces, concrete strategy classes, a strategy factory, and test classes demonstrating the output.

JDK Comparator Interface

Comparator is a strategy interface used by methods like Arrays.sort to apply different sorting strategies.

TreeMap in JDK

TreeMap uses a comparator object similarly to promotion strategies, with its compare method driving ordering.

Spring Resource Access Strategies

Spring's Resource abstraction represents different access strategies.

Spring Bean Initialization Strategies

Two strategies for bean initialization are illustrated.

References

Java Design Patterns Detailed Explanation

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsJavaSoftware ArchitectureStrategy PatternOOP
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.