Practical Design Patterns for Java Developers: Hone your software design skills by implementing popular design patterns in Java

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Unravel the power of Java design patterns by learning where to apply them effectively to solve specific software design and development problems

Key Features

  • Decouple logic across objects with dependency injection by creating various vehicles with features
  • Finalize vehicle construction by chaining handlers using the Chain of Responsibility Pattern
  • Plan and execute an advanced vehicle sensor initiation with the Scheduler Pattern

Book Description

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.

What you will learn

  • Understand the most common problems that can be solved using Java design patterns
  • Uncover Java building elements, their usages, and concurrency possibilities
  • Optimize a vehicle memory footprint with the Flyweight Pattern
  • Explore one-to-many relations between instances with the observer pattern
  • Discover how to route vehicle messages by using the visitor pattern
  • Utilize and control vehicle resources with the thread-pool pattern
  • Understand the penalties caused by anti-patterns in software design

Who this book is for

If you are an intermediate-level Java developer or software architect looking to learn the practical implementation of software design patterns in Java, then this book is for you. No prior knowledge of design patterns is required, but an understanding of Java programming is necessary.

Table of Contents

  1. Getting Into Software Design Patterns
  2. Discovering the Java Platform for Design Patterns
  3. Working with Creational Design Patterns
  4. Applying Structural Design Patterns
  5. Behavioral Design Patterns
  6. Concurrency Design Patterns
  7. Understanding Common Anti-Patterns

Author(s): Miroslav Wengner
Edition: 1
Publisher: Packt Publishing
Year: 2023

Language: English
Pages: 266
City: Birmingham
Tags: Design Patterns; Object-Oriented Software; Software Reusability; Software Patterns; Behavioral Design Patterns; Anti-Patterns; Java; Java Platform

Cover
Title Page
Copyright and Credits
Foreword
Contributors
Table of Contents
Preface
Part 1: Design Patterns and Java Platform Functionalities
Chapter 1: Getting into Software Design Patterns
Technical requirements
Code – from symbols to program
Examining OOP and APIE
Only exposing what’s required – encapsulation
Inevitable evolution – inheritance
Behavior on demand – polymorphism
Standard features – abstraction
Gluing parts to APIE
Understanding the SOLID design principles
The single-responsibility principle (SRP) – the engine is just an engine
The open-closed principle (OCP)
The Liskov Substitution Principle (LSP) – substitutability of classes
The interface segregation principle (ISP)
The dependency inversion principle (DIP)
Significance of design patterns
Reviewing what challenges design patterns solve
Summary
Questions
Further reading
Chapter 2: Discovering the Java Platform for Design Patterns
Technical requirements
Knocking on Java’s door
Exploring the model and functionality of the Java platform
The JDK
The JRE
The JVM
Reviewing GC and the Java memory model
The JMM
GC and automatic memory management
Examining the core Java APIs
Primitive data types and wrappers
Working with the String API
Introducing arrays
Discovering a collection framework
Math APIs
Functional programming and Java
Introducing lambdas and functional interfaces
Using functional interfaces in lambda expressions
Getting to grips with the Java Module System
A quick review of Java features from 11 to 17+
The local variable syntax for lambda parameters (Java SE 11, JEP-323)
Switch expressions (Java SE 14, JEP-361)
Text blocks (Java SE 15, JEP-378)
Pattern matching for instanceof (Java SE 16, JEP-394)
Records (Java SE 16, JEP-395)
Sealed classes (Java SE 17, JEP-409)
UTF-8 by default (Java SE 18, JEP-400)
Pattern matching for switch (Java SE 18, Second Preview, JEP-420)
Understanding Java concurrency
From a basic thread to executors
Executing tasks
Summary
Questions
Further reading
Part 2: Implementing Standard Design Patterns Using Java Programming
Chapter 3: Working with Creational Design Patterns
Technical requirements
It all starts with a class that becomes an object
Creating objects based on input with the factory method pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Creating objects from different families using the abstract factory pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Instantiating complex objects with the builder pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Cloning objects with the prototype pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Ensuring only one instance with the singleton pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Improving performance with the object pool pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Initiating objects on demand with the lazy initialization pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Reducing class dependencies with the dependency injection pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Summary
Questions
Further reading
Chapter 4: Applying Structural Design Patterns
Technical requirements
Incompatible object collaboration with the adapter pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Decoupling and developing objects independently with the bridge pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Treating objects the same way using the composite pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Extending object functionality by using the decorator pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Simplifying communication with the facade pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Using conditions to select desired objects with the filter pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Sharing objects across an application with the flyweight pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Handling requests with the front-controller pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Identifying instances using the marker pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Exploring the concept of modules with the module pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Providing a placeholder for an object using the proxy pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Discovering multiple inheritance in Java with the twin pattern
Motivation
Sample code
Conclusion
Summary
Questions
Further reading
Chapter 5: Behavioral Design Patterns
Technical requirements
Limiting expensive initialization using the caching pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Handling events using the chain of responsibility pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Turning information into action with the command pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Giving meaning to the context using the interpreter pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Checking all the elements with the iterator pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Utilizing the mediator pattern for information exchange
Motivation
Finding it in the JDK
Sample code
Conclusion
Restoring the desired state with the memento pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Avoiding a null pointer exception state with the null object pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Keeping all interested parties informed using the observer pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Dealing with instance stages by using the pipeline pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Changing object behavior with the state pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Using the strategy pattern to change object behavior
Motivation
Finding it in the JDK
Sample code
Conclusion
Standardizing processes with the template pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Executing code based on the object type using the visitor pattern
Motivation
Finding it in the JDK
Sample code
Conclusion
Summary
Questions
Further reading
Part 3: Other Essential Patterns and Anti-Patterns
Chapter 6: Concurrency Design Patterns
Technical requirements
Decoupling a method execution with an active object pattern
Motivation
Sample code
Conclusion
Non-blocking tasks using async method invocation pattern
Motivation
Sample code
Conclusion
Delay execution until the previous task is completed with the balking pattern
Motivation
Sample code
Conclusion
Providing a unique object instance with a double-checked locking pattern
Motivation
Sample code
Conclusion
Using purposeful thread blocking via a read-write lock pattern
Motivation
Sample code
Conclusion
Decoupling the execution logic with a producer-consumer pattern
Motivation
Sample code
Conclusion
Executing isolated tasks with the scheduler pattern
Motivation
Sample code
Conclusion
Effective thread utilization using a thread-pool pattern
Motivation
Sample code
Conclusion
Summary
Questions
Further reading
Answers
Chapter 7: Understanding Common Anti-Patterns
Technical requirements
What anti-patterns are and how to identify them
Theoretical principles challenges
Collecting technical debt as a bottleneck
Inappropriately squeezing the capabilities of the Java platform
Selecting the right tool
Conclusion of the code smell anti-pattern
Examining typical software anti-patterns
Spaghetti code
Cut and paste programming
Blob
Lava flow
Functional decomposition
Boat anchor
Conclusion
Understanding software architecture anti-patterns
Golden hammer
Continuous obsolescence
Input kludge
Working in a minefield
Ambiguous viewpoint
Poltergeists
Dead end
Conclusion
Summary
Further reading
Assessments
Index
Other Books You May Enjoy