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
This chapter does not have any technical requirements. In order to create UML diagrams, the following open source software tool is recommended:
- Modelio, which is available at https://www.modelio.org
Modelio can be run on systems with any of the following operating systems:
- macOS X
- Windows 7
- Windows 8
- Windows 10
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.
- Activity diagrams
- Interaction diagrams
- State machine diagrams
- Use case 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
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
- Sequence diagram
- Communication diagram
- Timing diagram
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
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
The following example shows two time constraints in a bank's vault security system. There are two objects:
- Bio Security System
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 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
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.
Another use case diagram for an online grade book system
- Class diagrams
- Component diagrams
- Composite structure diagrams
- Deployment diagrams
- Object diagrams
- Package 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.
- 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
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
A textual composite structure diagram
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 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.
A sample object diagram
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.
- What are the two basic types of UML diagrams?
- Why was UML initially created?
- List four behavioral UML diagrams.
- Which UML diagram provides a system's process flow?
- Which UML diagramÂ documents the interactions between a system and its users?
- What is a UML actor?
- What type of UML diagram illustrates system components?
- WhatÂ is the most commonly used UML diagram?
- Which UML diagramÂ shows the runtime structure of a system?
- Which UMLÂ diagram visually documents a system's hardware and software?
- UML 2.0 in Action: A project-based-tutorial (https://www.packtpub.com/hardware-and-creative/uml-20-action-project-based-tutorial)