Book Image

Practical Design Patterns for Java Developers

By : Miroslav Wengner
Book Image

Practical Design Patterns for Java Developers

By: Miroslav Wengner

Overview of this book

Design patterns are proven solutions to standard problems in software design and development, allowing you to create reusable, flexible, and maintainable code. This book enables you to upskill by understanding popular patterns to evolve into a proficient software developer. You’ll start by exploring the Java platform to understand and implement design patterns. Then, using various examples, you’ll create different types of vehicles or their parts to enable clarity in design pattern thinking, along with developing new vehicle instances using dedicated design patterns to make the process consistent. As you progress, you’ll find out how to extend vehicle functionalities and keep the code base structure and behavior clean and shiny. Concurrency plays an important role in application design, and you'll learn how to employ a such design patterns with the visualization of thread interaction. The concluding chapters will help you identify and understand anti-pattern utilization in the early stages of development to address refactoring smoothly. The book covers the use of Java 17+ features such as pattern matching, switch cases, and instances of enhancements to enable productivity. By the end of this book, you’ll have gained practical knowledge of design patterns in Java and be able to apply them to address common design problems.
Table of Contents (14 chapters)
1
Part 1: Design Patterns and Java Platform Functionalities
4
Part 2: Implementing Standard Design Patterns Using Java Programming
8
Part 3: Other Essential Patterns and Anti-Patterns

Changing object behavior with the state pattern

The behavior state pattern defines the procedure for influencing an object’s internal processes based on the mutation of its internal state. This pattern is part of the GoF’s book.

Motivation

Object states can be thought of as representing the concept of a finite machine. A pattern allows an object to change its behavior when its internal state changes. The state pattern enforces that an object describes its internal states with specific classes, and maps responses to those states to specific instances.

Finding it in the JDK

Usage of the state pattern can be found in the implementation of the jlink plugin, the jdk.jlink module, and the jdk.tools.jlink.plugin package. The interface plugin defines a nested enum class, State, whose values are references to the states in question.

Sample code

The next example considers that each vehicle has different states that are well identified (Example 5.20):

public...