Java Omnibus
OOAD & Architecture / GoF patterns / How to Spot GoF Patterns in Real Java Code
GoF Patterns Java Order Management
GoF Patterns

You spot patterns best by noticing recurring forces and structures in code, not by hunting for exact textbook shapes.

Real code rarely arrives with neat textbook labels. Patterns are often partial, adapted, or blended with framework conventions. The skill is not pattern trivia. The skill is recognizing the structural and behavioral cues that indicate a design is solving a familiar problem in a familiar way.

Why This Topic Matters

Why it matters in real Java systems

Java teams often work in frameworks and legacy codebases where patterns are present but not explicitly named. Learning to spot them improves code reading, refactoring, and architecture discussions without forcing every design into a catalog label.

Core Explanation

The design lens behind this page

Spotting patterns in real code starts with reading the force the code is responding to. If behavior varies cleanly behind one interface, Strategy may be present. If object creation has been localized and delegated, Factory Method may be in play. If wrappers preserve the same interface while layering behavior, Decorator may be the right lens.

For Java developers, this skill matters because most codebases are not written as teaching examples. Patterns show up as practical adaptations shaped by frameworks, domain constraints, and team conventions.

The point is not to prove that a pattern is “really there.” The point is to improve your reading of the design so you can modify it more safely.

Concept Breakdown

Key ideas to hold onto

Concept

Look for forces first

Conditional creation, swappable policies, state-driven behavior, and wrapper layering are better clues than names.

Concept

Expect framework-shaped variants

A Spring bean factory or strategy registry may embody familiar pattern ideas without textbook packaging.

Concept

Partial patterns still count

A codebase may show a strong Strategy or Builder direction without implementing every classic role exactly.

Java Example

Java example in the Order Management domain

Java example: a strategy-like shape in a real service
package org.javaomnibus.ecommerce.gof;

public interface DiscountPolicy {
    Money apply(Order order, Money subtotal);
}

public final class LoyaltyDiscountPolicy implements DiscountPolicy {
    @Override
    public Money apply(Order order, Money subtotal) {
        return subtotal.multiply(95).divide(100);
    }
}

public final class CheckoutPricingService {
    private final DiscountPolicy discountPolicy;

    public CheckoutPricingService(DiscountPolicy discountPolicy) {
        this.discountPolicy = discountPolicy;
    }

    public Money finalTotal(Order order) {
        return discountPolicy.apply(order, order.total());
    }
}
Code Walkthrough

Step by step

  1. The force is behavioral variation in discount policy.
  2. The interface plus swappable implementation shape is a strong Strategy signal.
  3. The code may not name the pattern explicitly, but the design move is recognizable.
  4. This is the level of pattern recognition that helps in real maintenance work.
Real-World Usage

Where this helps in practice

  • Reading unfamiliar codebases
  • Reviewing framework-heavy Java code that still embodies pattern ideas
  • Refactoring without over-labeling every design choice
Trade-Offs

When to be careful

Pattern recognition can become confirmation bias if you force a label too early. It is better to say a design is strategy-like or builder-like when the structure is partial or hybrid.

Common Mistakes

Frequent mistakes to watch for

  • Expecting textbook role names in production code
  • Equating any interface with Strategy
  • Calling every wrapper a Decorator without checking the force
  • Using pattern labels to avoid discussing the actual trade-offs in the code
Related Pages

Related concepts


What To Read Next