Iterator
Collection traversal is one of the most obvious and durable standard-library pattern examples.
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.
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.
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.
Collection traversal is one of the most obvious and durable standard-library pattern examples.
Comparators and pluggable behavior objects are a familiar Java expression of strategy.
Many IO wrappers layer behavior without changing the outer interface.
| Pattern | Java example | Why it fits |
|---|---|---|
| Iterator | Iterator, enhanced for | Traversal is separated from collection internals |
| Strategy | Comparator, Predicate | Behavior varies through pluggable policy objects |
| Factory Method | List.of(), Optional.of() | Creation is delegated to expressive factory methods |
| Builder | HttpRequest.Builder | Stepwise construction with fluent configuration |
| Decorator | BufferedInputStream, DataInputStream | Behavior layers over a wrapped stream |
| Template Method | AbstractList, AbstractMap | Base class defines algorithm skeleton while subclasses fill parts |
package org.javaomnibus.reference;
public final class OrderSortingExamples {
public void sort(List<Order> orders) {
orders.sort(Comparator.comparing(Order::createdAt));
}
}