Examples of GoF Design Patterns in Java's core libraries
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
GoF design patterns are easier to understand when you can point to real library code instead of textbook diagrams. Java’s core libraries contain many pattern-shaped APIs, although the examples are often practical approximations rather than perfect one-to-one implementations of the Gang of Four descriptions.
Factory Method in Calendar and NumberFormat
Factory Method appears whenever an API exposes a creation method that returns an appropriate implementation without the caller naming the concrete class directly.
The caller asks for an instance, and the library chooses the concrete implementation behind the scenes.
Singleton in Runtime
Runtime.getRuntime() is one of the clearest Java-library examples of the Singleton pattern.
There is one runtime object representing access to JVM-level capabilities. That fits the classic “single shared instance with global access point” idea closely.
Decorator in the I/O Streams
The java.io stream classes are a classic Decorator example. You wrap one stream in another to add behavior without changing the underlying source.
ByteArrayInputStream provides the raw data source. BufferedInputStream adds buffering. DataInputStream adds typed reads. That is decorator layering in everyday Java.
Adapter in Arrays.asList
Adapter appears when one interface is presented in a more useful or expected form. Arrays.asList adapts an array into a List view.
The returned list is backed by the original array, which is a reminder that this is an adapter-style view, not a brand-new general-purpose ArrayList.
Observer-Like Behavior in Event Listeners
Modern Java avoids the old deprecated Observable API, but listener models in GUI frameworks still show the Observer idea clearly: one subject notifies many interested listeners.
The button publishes events, and registered listeners react to them. That is the basic observer relationship.
Iterator Is a Pattern and an API
Some GoF patterns became so fundamental in Java that they are visible directly as interfaces. Iterator is a standard example.
This is useful to mention because Java’s libraries do not only use patterns internally. They often expose them directly as public API shapes.
Common Pitfalls
A common mistake is trying to force every library class into exactly one GoF box. Real APIs are usually pragmatic hybrids.
Another issue is using examples from deprecated parts of the JDK and assuming they represent current best practice.
A third problem is memorizing pattern names without noticing the motivation behind them, such as decoupled creation, behavior extension, or event notification.
Summary
- Java core libraries contain many practical examples of GoF-style patterns.
- '
Runtime.getRuntime()is a strong Singleton example.' - I/O stream wrappers are a classic Decorator example.
- Factory Method appears in APIs such as
Calendar.getInstance(). - Learn the pattern purpose, not just the label attached to a class.

