Book review - Practical Design Patterns for Java Developers

This book is for sale on Amazon in Kindle and Paperback editions.

Introduction

I’ve been programming since I was 10y old but graduated from film school as a video editor, so I never learned the “official” way to be a programmer. It was only by following courses and, most importantly, learning from colleagues, that I started making “readable and maintainable” code, precisely the topic of this book. After many years of being the only Java developer in a team of hardware and embedded engineers, I was introduced to SOLID and other principles when more Java colleagues joined and helped me to turn my “ugly” code to clean and structured projects. The book of Miroslav Wengner is exactly the kind of book I was missing when I got more experienced in Java development and wanted to uplift my career.

On top of that, this book also provides a lot of extra information related to the recent changes in the OpenJDK, which brought a lot of evolutions into the Java programming language and make it once more the preferred language for many different use cases.

The goal of the book wants to break a returning pattern many programmers go through during their careers. When you start using a programming language like Python, JavaScript, or even Java, the platform allows you to create messy code. It’s only when you learn and understand patterns and debugging, and use a strongly typed language like Java, that you start writing “real” code. When someone starts a programming career with Java and has a solid understanding of design patterns, a lot of bad practices can be avoided.

About the Author

Miroslav Wengner is a Java Champion and JavaOne Rockstar with an impressive career. He contributes to OpenJDK and various open-source projects (e.g. Robo4J) and is an executive committee member of the Java Community Process (JCP). He works as a principal engineer at OpenValue and is a regular conference speaker and blogger.

Content

The book is divided in three parts:

The table of contents immediately shows how the description of each design pattern follows the same approach and is structured in a motivation, where to find it in OpenJDK, example code, and a conclusion.

There are a lot of references to the “Gang of Four (GoF)” book. This book from 1994, with code examples in C++ and Smalltalk, describes 23 software design patterns. In his book, Miroslav, extends the list to 42 practical design patterns, with a focus on how to implement and use them with Java code.

Part 1: Design Patterns and Java Platform Functionalities

This part covers the purpose of software design patterns and outlines the fundamental ideas of the APIE and SOLID design principles, with an introduction to the Java platform.

Chapter 1: Getting into Software Design Patterns

All principles in Object-Oriented Programming (OOP) are based on Abstraction, Polymorphism, Inheritance, and Encapsulation (APIE), so this is the first topic described in this chapter. It is further used to introduce the SOLID design principle, which is the basis for all the other principles further described.

Design principles are in line with the basic pillars of OOP and APIE, and promote the principles of SOLID. They also enforce the Don’t Repeat Yourself (DRY) principle.

Chapter 2: Discovering the Java Platform for Design Patterns

As a Java developer, it may be strange to get re-introduced to the platform, but this chapter dives deeper into Java history, how it works under the hoods, how memory is allocated and managed, and describes the basics of threading and concurrency. This is all the required info to be able to fully understand their impact on the principles. So it’s both a refresh for experienced Java developers and an introduction for non-Java developers who want to compare it to another language.

Furthermore, this chapter provides a quick review of the new Java features introduced since Java 11.

Part 2: Implementing Standard Design Patterns Using Java Programming

This main part of the book groups the design patterns into three categories: creational, behavioral, and structural.

Chapter 3: Working with Creational Design Patterns

By using the factory method, prototype pattern, dependency injection pattern,… code maintainability and readability can be achieved. They are vital for clean software composition. On top of that, better software may even help to keep Moore’s Law valid as they improve the performance of the software on top of the current hardware.

Eight creational design patterns are described.

Chapter 4: Applying Structural Design Patterns

Twelve structural design patterns focus on maintainable and flexible source code to create objects. They bring clarity to relationships between created instances, and make their purpose easier to understand. A few examples: bridge pattern, decorator pattern, proxy pattern,…

Chapter 5: Behavioral Design Patterns

Fourteen patterns are described in this chapter! They provide transparent communication between objects, resulting in efficient usage of memory allocation. These include the caching, command, iterator, observer, template,… pattern.

Part 3: Other Essential Patterns and Anti-Patterns

Chapter 6: Concurrency Design Patterns

With the evolution in hardware since the GoF book, business requirements shifted more and more into a concurrent and parallel world. The Java platform provides concurrency functionality to achieve this. Eight patterns, including active object pattern, read-write lock pattern, producer-consumer pattern,… are used to describe these functionalities.

In this chapter, multiple screenshots with the output of Java Flight Recorder results, are included to visualize the effect of, for instance, the use of threads.

Chapter 7: Understanding Common Anti-Patterns

The previous chapters described the “green paths”, and how to improve your code. But there are also “red paths” which will reduce the code quality or program performance. This is where we reach the patterns with funny names, even though their impact is anything but funny. Spaghetti code, cut and past programming, lava flow, input kludge, working in a minefield, poltergeists, etc. are further described here.

The unwanted autoboxing anti-pattern is one I need to remember when reviewing some of my old-but-still-used code…

Conclusion

I’m impressed with the knowledge Miroslav exposes in his book. For each principle, at least one implementation inside OpenJDK is referenced to illustrate they are part of the Java code base itself, revealing his engagement in the OpenJDK project and his experience with Java.

The long list of principles can be a bit frightening as it not only illustrates what you should understand and know to produce good code, but also what you can do wrong, or did wrong in the past. But personally, it was also a confirmation. You do grow as a developer in your career when you focus on clean code. Some of these principles, became a daily habit as I wanted to become a better coder, and started implementing them without even knowing of their existence, as they made my code better to maintain.

The Bad

Only a few minor notes here…

The Good