3.1. Representing Beans Graphically

Problem

You need to draw a diagram of a simple bean that shows the relationships between different objects.

Solution

Use a simplified variant of a Unified Modeling Language (UML) class diagram that contains only class names, attribute names, and attribute types. Figure 3-1 describes two related beans—Person and Job.

Structure of Person and Job beans

Figure 3-1. Structure of Person and Job beans

Discussion

Many of the recipes in this book deal with an object model consisting of JavaBeans, and, to save paper, I’ve devised this simple shorthand for describing a collection of related beans. Every time you see a diagram like Figure 3-1, mentally translate it to a set of classes, one for each box; each class attribute in that box is a private member variable, with a getter method and a setter method. The beans represented by these diagrams are simply objects with attributes, and the only operations on a bean are getter and setter methods and no-argument constructors. To help you get used to translating these diagrams into code, the Person and Job classes are defined in Examples Example 3-2 and Example 3-3.

Example 3-2. The Person bean

public class Person { private String name; private Integer age; private Job job; public String getName( ) { return name; } public void setName(String name) { this.name = name; } public Integer getAge( ) { return age; } public void setAge(Integer age) { this.age ...

Get Jakarta Commons Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.