Book Image

Hands-On Design Patterns with Java

By : Dr. Edward Lavieri
2 (1)
Book Image

Hands-On Design Patterns with Java

2 (1)
By: Dr. Edward Lavieri

Overview of 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.
Table of Contents (15 chapters)
Free Chapter
1
Section 1: Introducing Design Patterns
4
Section 2: Original Design Patterns
8
Section 3: New Design Patterns

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