Policy choice
Strategy is about choosing among interchangeable algorithms or policies.
Strategy, State, and Command all replace some form of direct branching or direct calls with object-based behavior. The clean distinction is the reason. Strategy models interchangeable policy. State models lifecycle-driven behavior. Command models an action as an object that can be passed around, queued, or audited.
In Java systems, pricing, order lifecycle, and queued operations can all look like behavior objects on the surface. Picking the wrong one makes the design explanation fuzzy and the code harder to evolve.
The similarity between these patterns is that they all move behavior into dedicated objects. The difference is the force that makes that move necessary. If the problem is “which policy should I use?” think Strategy. If the problem is “how should this object behave in its current lifecycle state?” think State. If the problem is “how do I represent and execute this request as a first-class action?” think Command.
In Java work, confusing these patterns often leads to unclear naming. A command named like a strategy or a state object acting like a policy object usually signals that the design force was never stated clearly enough.
Choose based on why the behavior is moving into objects, not simply because behavior has moved.
Strategy is about choosing among interchangeable algorithms or policies.
State is about behavior changing because the object itself is in a different phase.
Command is about packaging an action so it can be invoked, stored, or retried.
package org.javaomnibus.ecommerce.gof.behavioral;
public final class BehavioralPressures {
DiscountPolicy discountPolicy() { return null; }
OrderState currentOrderState() { return null; }
OrderCommand capturePaymentCommand() { return null; }
}
| Pattern | Main force | What varies | Typical Java example |
|---|---|---|---|
| Strategy | Policy or algorithm choice | Algorithm | Discount or routing policy |
| State | Lifecycle-driven behavior | Behavior by current state | Order lifecycle transitions |
| Command | Executable request object | Action object and invocation timing | Queued payment capture |
Comparison pages speed up choice, but the real code context still matters. The goal is sharper judgment, not replacing design thinking with a chart.