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 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