How to Create Uml Class Diagrams

By Rachelle Reese

A Unified Modeling Language (UML) class diagram is used to identify and describe the classes that will be created when implementing an application. You can think of a class as a person or thing that plays a role in an application. A class often has data, which is represented by attributes. A class also has behaviors, which are represented by methods. Classes can also be related to other classes. For example, a class might inherit from a more general class, a class might contain another class, or a class might use the methods of different class. Let’s look at how to create a UML diagram to define three classes: a Product, a Book, and an Author.

Things You'll Need

  • Microsoft Office Visio 2007

Create a Visio Drawing

Step 1

Click the “Start” button. Choose the “All Programs” menu item. Choose the “Microsoft Office” menu item. Choose the “Microsoft Office Visio 2007” menu item.

Step 2

Choose the “Software and Database” template category in the left-hand pane.

Step 3

Choose the “UML Model Diagram” under “Featured Templates” and click the “Create” button. You have created a drawing and added the UML shapes to the toolbar. The “UML Static Structure” Shapes category should be expanded.

Define the Author Class

Step 1

Choose the “Class” shape and drag it onto the drawing. The default name of the object is “Class1”. Double-click “Class1” to open the “UML Class Properties” dialog. Type “Author” in the “Name” field.

Step 2

Choose the “Attributes” category. You will define four attributes: FirstName, MiddleName, LastName, and SSN.

Step 3

Type “FirstName” in the Attribute column. Select “C#::string” from the “Type” drop-down list. The Type defines the data type that will be used for the attribute. In this case, a data type of “C#::string” indicates that you plan to implement the class using the C# programming language and the attribute will store textual data. Notice that the visibility is set to “private”. When the visibility is private, the attribute can only be accessed within the class where it is defined.

Step 4

Type “MiddleName” in the next row of the Attribute column. Select “C#::string” from the “Type” drop-down list. Notice that the default multiplicity is “1”. The multiplicity determines how many instances of the attribute there will be for each instance of the class. Select “0..1” from the “Multiplicity” drop-down list to indicate that some authors might not have a middle name.

Step 5

Type “LastName” in the next row of the Attribute column. Select “C#::string” from the “Type” drop-down list.

Step 6

Type “SSN” in the next row of the Attribute column. Select “C#::string” from the “Type” drop-down list.

Step 7

Choose the “Operations” category. Type “new” in the first row of the Operation column. Keep the other options at default. Notice that the visibility is set to “public”. A public method can be accessed by other classes. Each class requires at least one public constructor that executes when a program creates an instance of the class. In C#, Java, and C++, the constructor is named “new”. In this case, you will create a constructor that accepts a social security number and a full name as a parameter.

Step 8

Make sure the first row is selected and click the “Properties” button. Choose the “Parameters” category. Type “SSN” in the first row of the Parameters column. Choose “C#::string” from the “Type” drop-down list. Type “FullName” in the next row of the Parameters column. Choose “C#::string” from the “Type” drop-down list. Click the “OK” button.

Step 9

Type “getName” in the second row of the “Operation” column. Choose “C#::string” from the “Return Type” drop-down list. This defines a public method named getName that returns a string.

Step 10

Click the “OK” button. You have now defined a simple class named Author. The class has four attributes, a constructor that accepts two parameters, and a method.

Define the Book Class

Step 1

Choose the “Class” shape and drag another class onto the drawing. Double-click “Class1” to display the properties. Type “Book” in the “Name” field.

Step 2

Choose the “Attributes” category. Type “Authors” in the first row of the “Attribute” column. Click the vertical bar to the right of the “Type” column and drag it to the right to expand the “Type” column. Choose “Top Package::Author” from the “Type” drop-down list. This indicates that the attribute will be an instance of the Author class. Choose “1..*” from the “Multiplicity” drop-down list to indicate that a book will always have at least one author, but might have multiple authors.

Step 3

Click the “OK” button to update the class shape on the drawing.

Step 4

Choose the “Composition” arrow from the “UML Static Structure” shapes. Drag it to the drawing and connect the Author and Book classes. The Author side of the connection should be marked with an asterisk (*). The Book side of the connection should be marked with a “1”. The composition arrow indicates that there is a relationship between Book and Author.

Define the Product Class

Step 1

Choose the “Class” shape and drag another class onto the drawing. Double-click “Class1” to display the properties. Type “Product” in the “Name” field.

Step 2

Choose the “Attributes” category. Type “ProductID” in the first row of the “Attribute” column. Choose “C#::string” from the “Type” drop-down list.

Step 3

Type “Price” in the second row of the “Attribute” column. Choose “C#::decimal” from the “Type” drop-down list. A decimal data type is one data type that can be used to store monetary values.

Step 4

Type “Name” in the third row of the “Attribute” column. Click the “OK” button.

Step 5

Choose the “Generalization” arrow from the “UML Static Structure” shapes and drag it onto the drawing. Connect the “Product” class to the “Book” class in such a way that the arrowhead points to the Product class. You have modified the UML diagram to indicate that the Book class inherits from the Product class. This means that the Book class also has the ProductID, Price, and Name attributes.

Step 6

Click the “File” menu and choose “Save”. Type “MyUML” in the “File name” field and click the “Save” button.

Tips & Warnings

  • You will usually define attributes with private visibility because object-oriented programming practices encourage data hiding.
  • In a real-world application, the classes would have additional attributes and methods to provide the necessary functionality of the application.