Section 5.8. Class Diagrams

   

5.8 Class Diagrams

The purpose of a class diagram is to visually and simply represent the important aspects of your classes in a compact manner. The diagram of a class has three main parts : the class name , the attributes, and the methods . It also includes the visibility modifiers and, sometimes, datatypes.

Note

A constructor is a special method that is called when you create an instance of a class using the new keyword. Since a constructor is a method, include it as such 0in your class diagram.


Let's create the Employee class for our WWWBooks case study with Visio. To create a class diagram in Visio, choose File > New > Software > UML Model Diagram. This opens a new blank drawing along with a special tool bar that has many different components. You'll see several submenus in this tool bar that store the different components needed for each type of diagram. You'll notice submenus for UML Sequence, Activity, Collaborations, and so forth. You can choose the appropriate submenu for the kind of diagram you want to create, and the things you'll need for each drawing will appear. In general with components in Visio, you can drag and drop them onto your page, and then double-click on them to edit their properties. Visio refers to these components as stencils.

The Visio 2000 class editor is shown in Figure 5.1.

Figure 5.1. Visio 2000 class editor.

graphics/05fig01.jpg

If you do not see Java Data Types in the UML Navigator in Visio, you can make it appear (and remove others you may not want) by choosing UML from the Main Toolbar > Options and then clicking the UML Document tab. Check the boxes next to the datatype you want to have available.

5.8.1 Changing the Class Name

Under the UML Static Structure menu, you should see Class at the top. Drag a class onto the page and double-click on it. A UML Class Properties dialog box appears, which allows us to edit the properties on the class. In the Name field, type Employee. The name of the class changes at the top of the diagram.

5.8.2 Access Modifiers

In a class diagram, the access modifier is noted next to the attributes and methods defined. Access modifiers for your data and operations will play an increasingly important role in our discussion of classes. Therefore, let us take a quick detour to recap what we know about access modifiers.

An access modifier (also called visibility modifier ) is either private , protected , or public . You represent these visibility modifiers like this:

 - (private)  # (protected) + (public) 

Table 5.2 shows the different access levels and their descriptions. While we have discussed these access modifiers previously, they can be tricky to remember as they do not exist in ColdFusion. Covering them here reinforces their importance in your design.

Table 5.2. Description of Access Levels for Attributes and Operations

Access Level

Description

default

default is not a keyword that you explicitly use. If you don't type one of the other three modifiers, you get an access level of default . default basically means "package" ”only code within the same package can access it.

The following items can be default : classes, instance variables, methods, static variables , static methods, constructors, inner and nested classes, and interfaces.

private

private data is private to the class. Only other parts of the class can access data marked private . An instance variable marked private can only be accessed by methods inside its own class (with the exception of inner classes).

You can mark the following items private : instance variables, methods, static variables, static methods, constructors, and inner and nested classes.

protected

protected items extend default in a way; they are accessible at the default level and to any subclasses including those in a package different from the one in which the superclass (the parent class) resides. It is therefore less restrictive than default .

You can mark the following items protected : instance variables, methods, static variables, static methods, and interfaces.

public

The least restrictive access level, data marked public are accessible by any code that has a valid reference to the object marked public .

You can mark the following items public : classes, instance variables, methods, static variables, static methods, constructors, inner classes, and interfaces.

Note

The introduction of ColdFusion components (CFCs) in ColdFusion MX allows you to specify access modifiers ”this is only true for CFCs.


5.8.3 Adding Attributes

On the left-hand side of the screen, you'll see the UML Navigator. The UML Navigator appears when you open a new UML drawing file. It shows your model as a hierarchy, including the datatypes available. These are C++, BASIC, IDL, and Java.

In the UML Class Properties dialog, choose the Attributes tab and click New. In the Attribute field, type employeeID . You may need to expand the column width for the Type column in order to read all of the choices. You will notice that the type Employee is now available as an object. Let's choose int . Next, choose the visibility for the attribute.

Let's choose protected for our employeeID so that it is only visible to methods in the same package and subclass. If an application grows and you start defining things in many different packages, we would just need to make sure that we balance between the modularity, security, and dependability that comes with restrictive access, and the convenience and organization that comes with a more open access modifier.

We can leave the rest of the items (multiplicity and initial value) alone. Add the remaining attributes the same way. hireDate for now will be a protected Date object, and salary will be a protected double . To add another attribute, click New again. To view your work so far, click OK.

5.8.4 Adding Methods

Methods are added in much the same way as attributes. Click the Operations tab to add methods, and type the name using the Java naming conventions; you don't need to include the () . Click in the Return Type column header to choose what datatype the operation will return. getEmployeeID() returns an int . get-HireDate() returns a Date object of the java.util package; getSalary() returns a double , and we'll mark it as such in the dialog box. If you are writing it out or using a program other than Visio, the return type is marked with a colon , like this: getSalary(): double . We will leave all of these methods public so that they are accessible from anywhere . Now that we have created the accessor (get) methods, let's create the manipulator (set) methods.

The methods setHireDate(java.util.Date hd) and setSalary-(double s) are created in the same way that our accessor methods were, with a couple of differences. To begin with, we do not specify a return type, since these manipulator methods won't return anything (you could have a manipulator return a boolean specifying whether the operation was successful, or an error code). It also takes an extra step to specify the parameters that a method accepts. Let's choose to specify that these two manipulator methods are protected, so that only methods defined in the same package as the Employee class or subclasses of the Employee class can change the value of the hire date and the salary.

In order to add a parameter to a method, you have to take an extra step. In Visio, in the UML Navigator, double-click on the method to which you want to add a parameter. The UML Operations Properties dialog box appears. Click the Parameters tab and click New. Type the name of the parameter you want to add. For salary, this might be simply "s." Then choose the datatype that will get passed to the method. Note that Visio allows you to specify only the eight primitive datatypes by default; you have to set up different data types (such as java.util.Date ) separately.

So now our class diagram looks something like this:

 Employee  -employeeID #hireDate #salary +getEmployeeID() : int +getHireDate() : String +getSalary() : double #setHireDate(java.util.Date) #setSalary(double) 

The Java code corresponding to this class diagram looks like this:

5.8.5 Employee.java

 import java.util.Date;  public class Employee {     private static int employeeID;     protected Date hireDate;     protected double salary;     // default constructor     public Employee() {         employeeID = (int) (Math.random() * 1000);     } public int getEmployeeID() {         return employeeID;     } public Date getHireDate() {         return hireDate;     } public double getSalary() {         return salary;     } protected void setHireDate(Date hd) {         hireDate = hd;     } protected void setSalary(double s) {         salary = s;     } } 

Once we have created the UML class diagram, it is rather straightforward to write the code that makes up the class, as you can see above.

Note

Creating and manipulating the various UML document types in Visio is very similar to this process, so we won't belabor interacting with that particular software package as we identify more diagrams.


While we go into depth on writing classes in Chapter 7, I wanted to introduce this process now for a couple of reasons. First, classes are the fundamental aspect of OOP. It is not easy right out of the gate to start writing classes, so it's good to get some practice. I think it helps demystify the process a little. Also, it is most realistic to approach it from a design standpoint, rather than a syntactical standpoint ”that's what you'll be doing when you start writing more complex Java programs. Finally, it's more fun; look what we already know how to do:

5.8.6 MakeEmployee.java

 import java.util.Date;  public class MakeEmployee { public static void main(String[] args){ // create a new Employee object Employee myEmp = new Employee(); // call the getEmployeeID method on our new object System.out.println("Employee created with ID: " +         myEmp.getEmployeeID()); // set its salary myEmp.setSalary(75000.00); // get and print the salary System.out.println("Salary: " + myEmp.getSalary());     } } 

Using only the skills you have already acquired , we can put together a Java class with its own data and operations, create an object of that type in another class, and create and view its data. This represents the first truly object-oriented program we've written so far.

Class diagrams are the basis of just about all OO methods. You will likely use them more frequently than any other kind of UML diagram. Don't get bogged down with them too early on. Instead of writing out all of your class diagrams with the full notation, you might try instead writing a little code, and then writing out some class diagrams once you've got a grip on the project. Then they can aid you in late architectural issues, optimization, and refactoring.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net