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 = age; }

    public Job getJob( ) { return job; }
    public void setJob(Job job) { this.job = job; }
}

Example 3-3. The Job bean

public class Job {
    private String title;
    private BigDecimal salary;

    public String getTitle( ) { return title; }
    public void setTitle(String title) { this.title = title; }

    public BigDecimal getSalary( ) { return salary; }
    public void setSalary(BigDecimal salary) { this.salary = salary; }
}

When an object contains an attribute of type List, it is helpful to know the type of object that List contains. To convey the type of object contained in a List, every list attribute will be followed by the type it is intended to contain surrounded by brackets: List<Integer>. Similarly, if an object contains a Map, the type of the key and value will be separated by a comma and surrounded by brackets: Map<String,Integer>. If the Person object had contained a list of Job objects instead of a single Job object, the attribute type would be written List<Job>, as in Figure 3-2.

Representing a list of Job objects

Figure 3-2. Representing a list of Job objects

Tip

The angle bracket syntax is actually the standard means of representing these objects in the newest version of Java, JDK 1.5. For more on JDK 1.5, check out Java 1.5 Tiger: A Developer’s Notebook, by David Flanagan and Brett McLaughlin(O’Reilly).

See Also

Don’t confuse UML with object-oriented design, as this is a mistake all too often made; UML captures the form and function of a system, but it is no substitute for a good design. All the drawings in the world won’t make a bad design good, and spending a day drawing a big UML diagram is simply a creative form of procrastination. That said, if you insist on drawing UML diagrams, check out some of the following tools:

ArgoUML

ArgoUML is a UML editor from Tigris.org, an open source community hosted by CollabNet (http://www.collabnet.com). ArgoUML supports reverse engineering and code generation to Java, C++, C#, and PHP. For more information about ArgoUML, see the project page at http://argouml.tigris.org/. To launch ArgoUML via Java Web Start, click on http://argouml.tigris.org/files/documents/4/383/ArgoUML-stable.jnlp.

Poseidon for UML

Gentleware AG has taken ArgoUML and extended it, providing additional features, such as language export options and improved usability. A community edition is free to download and use. For more information about this product, see the Gentleware AG site at http://www.gentleware.com. If you have Java Web Start installed, you can launch the community edition of Poseidon through a browser by clicking on http://www.gentleware.com/products/webstart.php4.

SmartDraw

SmartDraw, from SmartDraw.com, offers a very usable general purpose tool for creating diagrams. This tool stands out from the rest because it is simple, and, unlike other tools, it is solely concerned with the creation of diagrams; it makes no attempt to get involved with your application, and it has an attractive price of around $129. Details are at http://www.smartdraw.com.

Other Tools

It seem that almost every software company has attempted to create a UML modeling tool, some of the well-known commercial products are IBM’s Rational Rose, Microsoft’s Visio, and Borland’s Together. The URL http://www.objectsbydesign.com/tools/umltools_byCompany.html contains an exhaustive list of other UML tools.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.42.134