Class Diagrams in Eclipse


Laurie Williams, Dright Ho, and Sarah Smith
CSC 326 - Software Engineering
Department of Computer Science
North Carolina State University


0.0 Outline
1.0 Background on UML, Eclipse UML, and Class Diagrams
2.0 Creating a Class Diagram
3.0 Creating Objects in a Class Diagram
4.0 Reverse Engineering a Class Diagram
5.0 Tips and Tricks
6.0 Exercise
7.0 Resources

1.0 Background on UML, Eclipse UML, and Class Diagrams

UML

UML (Unified Modeling Language) was developed by Grady Booch, James Rumbaugh, and Ivar Jacobson, at Rational Software Corporation in 1997, for a standard way to model the structure of large applications. The UML is a standard maintained by the Object Management Group (OMG). The current version of UML is 2.0. UML provides a standardized way of modeling software, so the everyone can understand a given model. UML has several different modeling types: class diagram, sequence diagrams, state charts, and use cases to name a few. This tutorial will introduce programmers to creating class diagrams in Eclipse UML.

Eclipse UML

Eclipse UML is an Eclipse plug-in developed my Omondo. There is a free edition and a studio edition of Eclipse UML. The free edition will cover the modeling needs for CSC 326. You can download Eclipse UML here. After you have downloaded Eclipse UML, double click the jar file, which will then run an installer program. This will install Eclipse UML as a plug-in. We are using Eclipse UML release 2.1.0.20050718. You need to have the Eclipse Modeling Framework (EMF), Graphical Editor Framework (GEF) and UML2 Eclipse plug-ins installed. These are all Eclipse projects, and you can install them by connecting to the Eclipse update site inside of Eclipse (see Installing Plugins in Eclipse tutorial)

Class Diagrams

Each class in a Class Diagram is represented by a box with the class name, attributes, and methods (or operations). Relationships between the classes are how the classes communicate with each other. A solid line represents an association, which is a property of a class. Associations are used to represent relationships between objects (when one object has an instance of another object) while attributes show smaller things like Strings, ints, booleans, that are properties of an object.

Dependencies are when the change of one class will affect the second class. The second class depends on the first class. Some examples of dependencies are when one class calls another classes method, a class is passed in as a parameter to a method, or if a class has another class as a variable.

Aggregation and Composition are special cases of Association. Aggregation is used with a whole-part relationship. Composition is a strong form of aggregation where an instance of the part is only associated with and instance of the whole, and the part lives or dies based on the whole. But another instance of the part may be aggregated with another type of whole.

Generalization is the combining of similar traits of two similar concepts into one class and then storing the differences in subclasses. The subclass must contain (i.e. inherit) all of the elements of the superclass, but may override some of the inherited methods and/or fields. Realization indicates the implementation of some interfaces.

For more detailed information about Class Diagrams, check out Chapters 3 and 5 in Martin Fowler's UML Distilled.

This tutorial is an introduction to creating, generating code from, and reverse engineering class diagrams.

Top | Contents

2.0 Creating a Class Diagram

All class diagrams in Eclipse UML are tied to the source code that you are writing. If you have already started writing a program, you can reverse engineer a class diagram from the existent source code. If you start by creating a class diagram, code will be generated for you. Any changes in the code will cause a change in the class diagram and vice versa.

The class diagram file is stored in the source directory or package of the associated source code. You can also create class diagrams in another folder of the project.

2.1 There are three ways to create a class diagram:

2.1.1 Select File > New > Other... > UML Diagrams > Class Diagrams ,

2.1.2 Select the arrow of the button in the upper left of the toolbar. Select Other... > UML Diagrams > Class Diagrams, or

2.1.3 Right click on the source folder of package that you want to create a class diagram in and select New > Other... > UML Diagrams > Class Diagrams.

2.2 Press Next. Give the class diagram a file name, or browse for the package that you want the class diagram to cover, and that will become the filename of the class diagram. All class diagrams end in a .ucd extension. Also, select the presentation mode that you want your class diagram to have:

  • Association: Shows all associations between classes
  • Inheritance: Shows class inheritance and hierarchy
  • Dependency: Shows dependencies between classes in the class diagram.

2.3 If there are already class files in the package that you are creating a class diagram for, you are able to select the files that you want to be reverse engineered into the class diagram. You can easily add onto the class diagram that is created.

Top | Contents
3.0 Creating Objects in a Class Diagram

Below is the class diagram tool bar. This is displayed at the top of the Class Diagram editor window. A discussion of each of the tools is also below.

  • : Selects components of a diagram
  • : Zooms in on diagram
  • : Adds a package to the diagram
  • : Adds a class to the diagram
  • : Adds a new interface to the diagram
  • : Adds an association between classes to the diagram
  • : Adds an inheritance relationship between classes to the diagram
  • : Adds a dependency relationship between classes to the diagram
  • : Adds an interface provider to the diagram
  • : Adds a required interface to the diagram
  • : Adds an interface connection to the diagram
  • : Adds an indication
  • : Adds a note to the diagram
  • : Adds a link to the diagram
  • : Adds a label to the diagram

To add one of these components to a diagram, select the component and drag the curser from a starting point to an ending point in the editor. When you are creating a package, class, or interface, you will need to drag a rectangle of the size you want the package, class, or interface to be in the diagram. The connector icons will need to be dragged from a source object, like a class, to a destination object, like another class.

When you have added a package, class, or interface to the diagram, the corresponding object will be created in your project.

Levels of Class Diagrams

You will most likely want to start your class diagram at the top most level, and define the packages that will be in your system. Then you can create a class diagram for each package in your system by double clicking on the package in your top level diagram. You can again choose what relationships you want displayed in the diagram. You can also choose the diagram scope (package, project, all) and the level.

Adding Attributes and Methods

You can add attributes and methods to a class in a class diagram by right clicking on the class or interface that you want to add an element too, and selecting New > Attribute, New > Method, New > Constructor, New > Inner Interface, New > Inner Class.

A dialog will display, providing you with places for all possible information about the element that you want to create. When you create a new attribute, getter and setter methods are automatically generated in the source code, but not displayed in the class.

Top | Contents
4.0 Reverse Engineering a Class Diagram

2.1 There are three ways to create a Java project:

2.1.1 Select File > New > Other... > UML Diagrams > Class Diagrams ,

2.1.2 Select the arrow of the button in the upper left of the toolbar. Select Other... > UML Diagrams > Class Diagrams, or

2.1.3 Right click on the source folder of package that you want to create a class diagram in and select New > Other... > UML Diagrams > Class Diagrams.

2.2 Press Next. Give the class diagram a file name, or browse for the package that you want the class diagram to cover, and that will become the filename of the class diagram. All class diagrams end in a .ucd extension. Also, select the presentation mode that you want your class diagram to have:

  • Association: Shows all associations between classes
  • Inheritance: Shows class inheritance and hierarchy
  • Dependency: Shows dependencies between classes in the class diagram.

2.3 If there are already class files in the package that you are creating a class diagram for, you are able to select the files that you want to be reverse engineered into the class diagram. You can easily add onto the class diagram that is created.

Top | Contents
5.0 Tips and Tricks

5.1 If you wish to see more of your class diagram you can zoom out using the zoom feature located in the toolbar.

Top | Contents
6.0 Exercise

Import the CoffeeMaker project into your workspace. Reverse engineer the class diagram of the project and show it to your TA.

Add a new class, method, or attribute to the class diagram (for example you could add a new ingredient to the inventory), and show your TA.

Top | Contents
7.0 Resources
Top | Contents

Class Diagrams in Eclipse Tutorial ©2003-2005 North Carolina State University, Laurie Williams, Dright Ho, Sarah Smith
Email Sarah Smith with any questions or comments about this tutorial.
Last Updated: Tuesday, September 20, 2005 2:57 PM