Java Omnibus
OOAD & Architecture / GoF patterns / Behavioral / Strategy vs State vs Command
GoF Behavioral Decision Guide Java
Pattern Comparison

These three behavioral patterns look related because they all move behavior into objects, but the force behind each one is different.

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.

Why This Topic Matters

Why this behavioral choice matters

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.

Core Explanation

How to separate these behavioral roles

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.

Concept Breakdown

The forces behind the choice

Force

Policy choice

Strategy is about choosing among interchangeable algorithms or policies.

Force

Lifecycle behavior

State is about behavior changing because the object itself is in a different phase.

Force

Executable request

Command is about packaging an action so it can be invoked, stored, or retried.

Java Example

Behavioral decision sketch in Java

Java example: three different reasons behavior might become an object
package org.javaomnibus.ecommerce.gof.behavioral;

public final class BehavioralPressures {
    DiscountPolicy discountPolicy() { return null; }
    OrderState currentOrderState() { return null; }
    OrderCommand capturePaymentCommand() { return null; }
}
Code Walkthrough

Step by step

  1. discountPolicy suggests interchangeable policy, which points to Strategy.
  2. currentOrderState suggests lifecycle-driven behavior, which points to State.
  3. capturePaymentCommand suggests an executable request, which points to Command.
  4. All three use objects for behavior, but not for the same reason.
Real-World Usage

Where this comparison helps

  • Refactoring discussions where several behavioral patterns look plausible
  • Teaching teams to distinguish policy, lifecycle, and action objects
  • Architecture reviews around workflow and orchestration code
Comparison Table

Comparison Table

Pattern Main force What varies Typical Java example
StrategyPolicy or algorithm choiceAlgorithmDiscount or routing policy
StateLifecycle-driven behaviorBehavior by current stateOrder lifecycle transitions
CommandExecutable request objectAction object and invocation timingQueued payment capture
Trade-Offs

How to use this guidance responsibly

Comparison pages speed up choice, but the real code context still matters. The goal is sharper judgment, not replacing design thinking with a chart.

Common Mistakes

Where teams still get confused

  • Calling policy objects commands
  • Using state objects when the problem is not really lifecycle behavior
  • Treating every queued action as a strategy
  • Failing to state the behavioral force clearly before choosing the pattern
Related Pages

Compare the detailed behavioral pages


What To Read Next