Java Omnibus
OOAD & Architecture / Reference & Downloads / GoF in Java Standard Library
Reference GoF Java Standard Library Examples
Reference & Downloads

GoF patterns become easier to remember when you can point to them in the standard library you already use.

One of the best ways to internalize classic patterns is to notice them in ordinary Java APIs. This page maps GoF ideas to familiar standard library classes so the patterns feel less theoretical and more like recurring design moves.

Why This Topic Matters

Why it matters in real Java systems

Developers often remember patterns more clearly when they connect them to classes they use every week. Iterator, Factory Method, Builder, Strategy, Template Method, and Decorator all become easier to spot once you see them in core Java APIs.

Core Explanation

The main reference idea

The goal here is not to claim every API is a perfect textbook pattern. Real libraries often blend multiple ideas. The more useful exercise is learning how pattern thinking sharpens your reading of APIs and your own design choices.

Concept Breakdown

Key ideas to keep in mind

Reference

Iterator

Collection traversal is one of the most obvious and durable standard-library pattern examples.

Reference

Strategy

Comparators and pluggable behavior objects are a familiar Java expression of strategy.

Reference

Decorator

Many IO wrappers layer behavior without changing the outer interface.

Library mapping table

Library mapping table

Pattern Java example Why it fits
IteratorIterator, enhanced forTraversal is separated from collection internals
StrategyComparator, PredicateBehavior varies through pluggable policy objects
Factory MethodList.of(), Optional.of()Creation is delegated to expressive factory methods
BuilderHttpRequest.BuilderStepwise construction with fluent configuration
DecoratorBufferedInputStream, DataInputStreamBehavior layers over a wrapped stream
Template MethodAbstractList, AbstractMapBase class defines algorithm skeleton while subclasses fill parts
Java Example

Java example in the Order Management domain

Java example: strategy through Comparator
package org.javaomnibus.reference;

public final class OrderSortingExamples {
    public void sort(List<Order> orders) {
        orders.sort(Comparator.comparing(Order::createdAt));
    }
}
Code Walkthrough

Step by step

  1. The collection stays the same while the comparison policy changes.
  2. That is why Comparator remains one of the cleanest Strategy examples in everyday Java.
  3. Reference pages like this help pattern recognition move from theory into normal API reading.
  4. Once that happens, pattern language becomes more useful and less ceremonial.
Real-World Usage

Where this helps in practice

  • Pattern revision
  • Teaching pattern recognition through familiar APIs
  • Preparing examples for Java design training
Trade-Offs

Trade-offs and when to be careful

  • Real libraries often blend patterns, so these mappings are instructive rather than absolute.
  • The danger is forcing every API into one label instead of learning what design move it actually uses.
Common Mistakes

Frequent mistakes to watch for

  • Expecting production frameworks to match textbook diagrams exactly
  • Confusing static factory methods with every kind of factory pattern
  • Forgetting that one API can reflect more than one pattern idea
  • Using mappings like this as proof instead of as learning aids
Related Pages

Related concepts


What To Read Next