Design Patterns in the Java Development Kit: A Deep Dive

Master Spring Ter
3 min readAug 19, 2024

Design patterns are tried-and-true solutions to common problems in software design. The Java Development Kit (JDK) extensively uses these patterns to provide robust, flexible, and efficient APIs. In this article, we’ll explore some of the most prominent design patterns used in the JDK and how they contribute to its architecture.

1. Singleton Pattern

The Singleton pattern ensures a class has only one instance and provides a global point of access to it. In the JDK, this pattern is used in several places:

  • java.lang.Runtime: Represents the runtime environment and is accessed using Runtime.getRuntime().
  • java.awt.Desktop: Allows Java applications to interact with the host operating system's desktop.

Example usage:

Runtime runtime = Runtime.getRuntime();
long memory = runtime.freeMemory();

2. Factory Method Pattern

The Factory Method pattern defines an interface for creating an object but lets subclasses decide which class to instantiate. The JDK uses this pattern in:

  • java.util.Calendar: The getInstance() method returns different calendar systems based on the locale.
  • java.text.NumberFormat: The getInstance() method returns locale-specific number formatters.

Example:

Calendar calendar = Calendar.getInstance();
NumberFormat numberFormat = NumberFormat.getInstance();

3. Observer Pattern

The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. In the JDK:

  • java.util.Observable and java.util.Observer (deprecated in Java 9)
  • Event handling in Swing (javax.swing.event package)

Example using Swing:

JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});

4. Decorator Pattern

The Decorator pattern attaches additional responsibilities to an object dynamically. The Java I/O streams heavily use this pattern:

  • java.io.BufferedInputStream decorates java.io.InputStream
  • java.io.DataInputStream adds data reading methods to java.io.InputStream
InputStream fileInputStream = new FileInputStream("file.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

5. Iterator Pattern

The Iterator pattern provides a way to access elements of a collection sequentially without exposing its underlying representation. In Java:

  • java.util.Iterator interface
  • Enhanced for-loop in Java 5+

Example:

List<String> list = Arrays.asList("Java", "Python", "C++");
for (String language : list) {
System.out.println(language);
}

6. Adapter Pattern

The Adapter pattern allows incompatible interfaces to work together. In the JDK:

  • java.util.Arrays.asList(): Adapts an array to a List
  • java.io.InputStreamReader: Adapts a byte stream to a character stream

Example:

String[] array = {"a", "b", "c"};
List<String> list = Arrays.asList(array);

7. Strategy Pattern

The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. In Java:

  • java.util.Comparator: Different sorting strategies can be passed to sorting methods

Example:

List<Integer> numbers = Arrays.asList(5, 2, 8, 1, 9);
Collections.sort(numbers, new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return b.compareTo(a); // Descending order
}
});

Conclusion

The Java Development Kit demonstrates the power and flexibility of design patterns. By leveraging these patterns, the JDK provides a robust and extensible foundation for Java applications. Understanding these patterns not only helps in effectively using the JDK but also in designing better Java applications.

As a Java developer, recognizing these patterns in the JDK can inspire you to apply them in your own code, leading to more maintainable and flexible software designs. Remember, while design patterns are powerful tools, they should be used judiciously and only when they provide clear benefits to your specific use case.

written/generated by: ChatGPT — Master Spring TER / https://claude.ai

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Master Spring Ter
Master Spring Ter

Written by Master Spring Ter

https://chatgpt.com/g/g-dHq8Bxx92-master-spring-ter Specialized ChatGPT expert in Spring Boot, offering insights and guidance for developers.

No responses yet

Write a response