Hands-On Design Patterns with Java

4.7 (3 reviews total)
By Dr. Edward Lavieri
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Unified Modeling Language Primer

About this book

Java design patterns are reusable and proven solutions to software design problems. This book covers over 60 battle-tested design patterns used by developers to create functional, reusable, and flexible software.

Hands-On Design Patterns with Java starts with an introduction to the Unified Modeling Language (UML), and delves into class and object diagrams with the help of detailed examples. You'll study concepts and approaches to object-oriented programming (OOP) and OOP design patterns to build robust applications. As you advance, you'll explore the categories of GOF design patterns, such as behavioral, creational, and structural, that help you improve code readability and enable large-scale reuse of software. You’ll also discover how to work effectively with microservices and serverless architectures by using cloud design patterns, each of which is thoroughly explained and accompanied by real-world programming solutions.

By the end of the book, you’ll be able to speed up your software development process using the right design patterns, and you’ll be comfortable working on scalable and maintainable projects of any size.

Publication date:
April 2019


Chapter 1. Unified Modeling Language Primer

This book features several design patterns and covers their implementation using Java. We can use Unified Modeling Language (UML) to help communicate class structures, objects, and interactions. This chapter provides an overview of UML, with a specific focus on diagrams applicable to the hands-on activities in this book.

The following topics will be covered in this chapter:

  • Introducing UML
  • Behavior diagrams
  • Structural diagrams

Technical requirements

This chapter does not have any technical requirements. In order to create UML diagrams, the following open source software tool is recommended:

Modelio can be run on systems with any of the following operating systems:

  • Debian/Ubuntu
  • macOS X
  • RedHat/CentOS
  • Windows 7
  • Windows 8
  • Windows 10

Introducing UML

UML was developed in 1994 to document object-oriented systems. Since that time, UML has become a standard tool for software engineers, software developers, and businesses. UML is used to design software and, after programming, also provides a visual record for the system which can be included as part of formal software documentation. 

There are two types of UML diagrams—behavioral and structural. Both are described in the sections that follow.


Understanding behavioral UML diagrams

Behavioral diagrams illustrate how system components interact to form a system. The four diagrams listed here are behavioral, and are briefly described in the subsections that follow:

  • Activity diagrams
  • Interaction diagrams
  • State machine diagrams
  • Use case diagrams

Activity diagrams

Activity diagrams illustrate the flow of processes in a system. This type of diagram is used to visually document activities within a system, also referred to as a system's procedures or dynamic components. 

The following activity diagram shows the activities involved when a coffee shop customer places an order. The starting point is the top black circle. From there, an order is received. Beneath the Receive Order node is a horizontal black bar referred to as a fork. There are three activities that take place in parallel after the fork—Make Drink, Get Bakery, and Collect Payment. All of those activities take place in parallel. Each of those activities feed into a second black bar, this time referred to as a join, which has multiple activities linked to it. Once all three activities are completed, the order can be completed and ends with the bottom black circle:

An activity diagram for a coffee shop order process

Interaction diagrams

Interaction diagrams visually document how system components interact with each other.

In the following interaction diagram, you can see that the flow starts with a decision on whether the customer is a new, or already existing, customer. In both cases, the interaction between the Customer object and Customer Database object is documented:

An interaction diagram for a coffee shop order



Interaction UML diagrams are robust and come in several different types. The interaction UML diagram types are listed here and will be covered in the subsequent sections:

  • Sequence diagram
  • Communication diagram
  • Timing diagram

Sequence diagrams

Sequence diagrams are used to show a specific use case scenario. So, these diagrams are representative of a typical behavior based on the given use case.

The following sequence diagram example visually documents the use case where a student logs on to an online book order system and enters their course code. The online system calculates a price and provides this total to the student. The student is then able to submit their payment, which goes through the online bookstore and informs the fulfillment center, which ships the books to the student:

An example sequence diagram

Communication diagrams

Communication diagrams are a special type of interaction diagrams. They focus on how system participants are linked to one another. 

The following sample communication UML diagram is a partial look at an online book-ordering system. System participants and their associations are provided:

A sample communication diagram

Timing diagrams

Timing UML diagrams provide a visual representation of a system's time constraints.

The following example shows two time constraints in a bank's vault security system. There are two objects:

  • Bio Security System
  • Vault

Each starts in the Off state. The first time constraint indicates that the vault must be opened within 15 seconds of the Bio Security System being activated. The second time constraint is that the vault can only be open for 20 minutes or less:

Sample timing diagram

State machine diagrams

State machine diagrams are used to visually describe a system's behavior. The key components of these diagrams include states and transitions. The sample state machine provided in the following diagram is for a bank vault. The solid circle is the initial pseudo state and indicates entry into the system. There are four states—Wait, Unlock, Enable, and Vault. 

In our bank vault example, the vault is enabled when two bank managers place their thumb on a thumbprint scanner. The vault is unlocked, contingent on a successful thumbprint scan, by entering the correct combination. When these conditions are met, the Vault state is reached and the managers can enter it:

State diagram for bank vault

Use case diagrams

Use case diagrams document the interactions between your users and your system. This is typically done with text, but UML does support use cases.

Let's start by looking at use cases in text, and then review a UML diagram representing the same use cases. We will use an example of a grade book for an online education institution. 

The student logs on to the system and selects their class. The student then selects the assignment and uploads their document. Next, the student enters text and selects the submit button. 

The instructor logs on to the system and selects their class. The instructor then selects the assignment, and the student. They grade the assignment, enter a grade, and select the submit button.

These use cases are pretty basic and easy to understand in text. There are only a few constructs for the use case diagram in UML:

A use case diagram for an online grade book system



There are several visual components to the UML use case diagram:

  • Actor: The stick figure is referred to as an actor. In our example, student and instructor were both actors. These are the users that use your system. Often, there are multiple user roles in a system.
  • Relationship: The solid lines indicate which actors interact with which use case items.
  • System: The overall system is represented by a rectangle. Actors are placed outside of the system and use case items are placed within the system.
  • Use Case Item: Use case items are represented in ovals, as seen in our preceding Online Grade Book example. These are the components of your use case.

These visual components are further illustrated as follows:

Another use case diagram for an online grade book system


Understanding structural UML diagrams

Structural diagrams illustrate components of a system. The six diagrams listed here are structural, and are briefly described in the subsections that follow:

  • Class diagrams
  • Component diagrams
  • Composite structure diagrams
  • Deployment diagrams
  • Object diagrams
  • Package diagrams

Class diagrams

The class diagram is the most commonly used UML diagram, as it provides a visual description of a system's objects. Consider that, in Java, everything is an object, so you can see the relevance and reason as to why this particular diagram is so widely used. Class diagrams do more than just display objects—they visually depict their construction and relationships with other classes. 

As you can see here, the basic component of the class diagram is a rectangle, divided into three sections. Each overall rectangle represents a class, and the class name appears using a bold typeface in the top section. The middle section contains attributes that correlate to variable fields. The third section contains operation data which, in Java, means functions and methods:

The class diagram structure

A simple example of a class diagram for a Kennel is displayed in the following diagram. The class name is Kennel, and there are three attributes (animal, breed, and name) and two operations (intake and discharge):

A class diagram of our Kennel class

We will further explore class diagrams using our Kennel class example later in this chapter.



Component diagrams

Component diagrams provide a visual representation of a system's physical components. The following example illustrates the three physical components of the system:

  • An inventory database
  • A customer database
  • An order component

The relationships between these components are annotated with dotted lines in the following component diagram:

An example component diagram

Composite structure diagrams

The Composite structure UML diagram shows the runtime structure of a system. This diagram can be used to show the internal components of a class. The following example shows a microwave with four structures:

A graphical composite structure diagram





We can also represent a composite structure with a rectangular box, as illustrated in the following diagram:

A textual composite structure diagram

Deployment diagrams

Deployment diagrams provide a visual representation of a system's hardware and software. Physical hardware components are illustrated, along with the particular software components that are on them. The hardware components are represented as nodes, and software is represented as an execution environment.

The nodes are drawn as three-dimensional rectangles and represent hardware for a software object, such as a database. As illustrated in the following example, associations are annotated with lines marked with the type of communication protocol used. Our example shows TCP/IP as the communication type:

A deployment diagram for a distributed system

Deployment diagrams can be more involved than the example provided here. Our example does, however, provide sufficient insight for you to progress through the proceeding chapters. 

Object diagrams

Object diagrams have an unfortunate name, as it does not aptly describe this UML diagram's purpose. The object diagram visually communicates a set of class instances. In fact, the instances have mostly optional components and are often only partially depicted. Therefore, a more apt name for this diagram might be a loosely defined instance UML diagram.

The following example depicts four objects and their hierarchy. Because each object is an instance of a class, it is underlined and followed by a colon and the class name:

A sample object diagram


Package diagrams

Package diagrams are used to provide a high-level visual depiction of large systems. These diagrams are simplistic and simply show how a system's components are grouped. The following example illustrates nested packages, starting with Java and drilling down to the ArrayList:

A nested package diagram



The Universal Modeling Language (UML) is used to create visual documentation of our systems. This can be used to design a system as well as document a system. UML is widely used by software engineers, software developers, and other professionals. 

Two of the 14 UML diagrams are behavioral and structural. Behavioral diagrams illustrate how system components interact to form a system and include activity diagrams, interaction diagrams, state machine diagrams, and use case diagrams. There are several types of interaction UML diagrams, including sequence diagrams, communication diagrams, and timing diagrams.

Structural diagrams illustrate components of a system and include class diagrams, component diagrams, composite structure diagrams, deployment diagrams, object diagrams, and package diagrams.



In the next chapter, Object-Oriented Design Patterns, we will explore intermediate and advanced concepts and approaches to object-oriented programming and their applicability to design patterns. A review of the fundamental concepts of object-oriented programming will help to ensure a deep conceptual understanding of object-oriented programming. An overview of object-oriented programming-related design pattern principles will also be provided.



  1. What are the two basic types of UML diagrams?
  2. Why was UML initially created?
  3. List four behavioral UML diagrams.
  4. Which UML diagram provides a system's process flow?
  5. Which UML diagram documents the interactions between a system and its users?
  6. What is a UML actor?
  7. What type of UML diagram illustrates system components?
  8. What is the most commonly used UML diagram?
  9. Which UML diagram shows the runtime structure of a system?
  10. Which UML diagram visually documents a system's hardware and software?

Further reading

About the Author

  • Dr. Edward Lavieri

    Dr. Edward Lavieri is a veteran software engineer and developer with a strong academic background. He earned a Doctorate of Computer Science from Colorado Technical University, an MS in Management Information Systems (Bowie State University), an MS in Education (Capella University), and an MS in Operations Management (University of Arkansas). He has been creating and teaching computer science courses since 2002. Edward retired from the U.S. Navy as a Command Master Chief after 25 years of active service. He is the founder and creative director of three19, a software design and development studio. Edward has authored more than a dozen technology books, including several on Java.

    Browse publications by this author

Latest Reviews

(3 reviews total)
I love books from Packt-publishing, every book I bought is easy to read and makes the subject easy to understand

Recommended For You

Book Title
Access this book, plus 7,500 other titles for FREE
Access now