You are on page 1of 28

Lecture 4

Class Diagram
Chapter 4, 5 & 8
The Unified Modeling Language User Guide
SECOND EDITION
By Grady Booch, James Rumbaugh, Ivar Jacobson

Session 9, 10, 11 & 12


UML Weekend Crash Course
Thomas A. Pender
Introduction
• A class diagram shows a set of classes, interfaces, and
collaborations and their relationships.
• These diagrams are the most common diagram found in
modeling object-oriented systems.
• Class diagrams address the static design view of a system.
• Class diagrams that include active classes address the static
process view of a system.
• Component diagrams are variants of class diagrams.
Class Diagram - Elements
• The Class diagram represents classes,
• Their component parts, and
• the way in which classes are related to one another.
Class
• A class is a definition for a type of object.
• A class is a template of similar types of objects.
• The class defines the rules; the objects express the facts.
• The class defines what can be; the object describes what is.
• Classes can be abstract
• Example:
– If Student is a class;
– student ID: 12-34567-1 is an instance object of that class
Elements of a Class in Class Diagram
• Attributes describe the appearance and knowledge of a class of objects.
• Operations define the behavior that a class of objects can manifest.
• Stereotypes help you understand this type of object in the context of other
classes of objects with similar roles within the system’s design.
• Properties provide a way to track the maintenance and status of the class
definition.
• Association is just a formal term for a type of relationship that this type of
object may participate in. Associations may come in many variations,
including simple, aggregate and composite, qualified, and reflexive.
• Inheritance allows you to organize the class definitions to simplify and
facilitate their implementation.
Elements of a Class Definition
• The name compartment uniquely defines a class (a type of object) within a
package. Consequently, classes may have the same name if they reside in
different packages.
• The attribute compartment contains all the data definitions.
• The operations compartment contains a definition for each behavior
supported by this type of object.
Attribute/ Operation visibility
• Public (+) visibility allows access to objects of all other classes.
• Private (-) visibility limits access to within the class itself. For
example, only operations of the class have access to a private
attribute.
• Protected (#) visibility allows access by subclasses. In the case of
generalizations (inheritance), subclasses must have access to the
attributes and operations of the superclass, or they cannot be
inherited.
• Package (~) visibility allows access to other objects in the same
package.
Modeling an Attribute
visibility / attribute name : data type = default value {constraints}

• Visibility (+, -, #, ~): Required before code generation


• Slash (/): The derived attribute indicator is optional. Derived values may be computed or figured out
using other data and a set of rules or formulas.
• Attribute name: Required. Must be unique within the class.
• Data type: Required. This is a big subject. During analysis, the data type should reflect how the client
sees the data. You could think of this as the external view.
• Assignment operator and default value: Optional. Default values serve two valuable purposes. First,
default values can provide significant ease-of-use improvements for the client. Second and more
importantly, they protect the integrity of the system from being corrupted by missing or invalid
values.
• Constraints: Constraints express all the rules required to be fulfilled to access this attribute.
• Class level attribute (underlined attribute declaration): Optional. Denotes that all objects of the
class share a single value for the attribute. (This is called a static value in Java.)
Modeling an Operation
visibility operationName (argname : data type {constraints}, . . .) :
return data type {constraints}
• Operation name: Required.
• Arguments/parameters: Any number of arguments is allowed. Each argument requires an
identifier and a data type.
• Return data type: Required for a return value, but return values are optional.
• Visibility (+, -, #, ~): Required before code generation.
• Class level operation (underlined operation declaration): Optional. Denoted as an
operation accessible at the class level;
• Argument name: Required for each parameter, but parameters are optional. Any number of
arguments is allowed.
• Argument data type: Required for each parameter, but parameters are optional.
• Constraints: Optional. In general, constraints express rules that must be enforced in the
execution of the operation.
Example
Class Design – Case Study 1
Name of the class: Student
Attributes
Name Data type Visibility Remarks
studentID Integer Private Assigned by system
studentName String Private Maximum 30 characters long
studentAddress String Private
CGPA Real Private Calculated from grades
Notice String Private Static

Operations
Name Arguments visibility Return datatype Remarks
getStudentID Public Integer
setStudentName Name (Data Type: string) Public
getStudentAddress Public String
getCGPA Public Real
setNotice Note (Data Type: String) Public
Class Design – Case Study 1: Solution

Student
- studentID: integer {assigned by system}
- studentName: String {Maximum 30 characters}
- studentAddress: String
- /CGPA: Real
- Notice: String
+ getStudentID ( ): integer
+ setStudentName (Name : String)
+ getStudentAddress ( ): String
+ getCGPA ( ): Real
+ setNotice (Note: String)
Class Design – Case Study 2: Practice
Name of the class: Employee
Type of the Class: User

Attributes
Name Data type Visibility Remarks
EmpID Integer Private
EmpName String Private
EmpSalary Integer Private Derived from Grade
EmpDOB Date Private
Note String Private Static

Operations
Name Arguments visibility Return datatype Remarks
getID Public Integer
setName EmpName (Data Type: String) Public
setID EmpID (Data Type: Integer) Public
setNote Note (Data Type: String) Public Static
Relationship - Association
• Association name: The purpose of the association can be
expressed in a name, a verb or verb phrase that describes how
objects of one type (class) relate to objects of another type
(class).
Relationship - Association
• Association multiplicity: Multiplicity is the UML term for the rule that defines the
number of participating objects.
• Most common is a range defining the minimum number of objects allowed and the
maximum number of objects allowed in the format Minimum . . Maximum
• Association roles: Association multiplicity:
Relationship - Association
• Association constraints:

• Association Class:
Relationship - Association
• Reflexive association

• Qualified association
Relationship – Association
Aggregation & Composition
Aggregation
• Aggregation is a special type of association used to indicate that the participating
objects are not just independent objects that know about each other.
• Instead, they are assembled or configured together to create a new, more complex
object.
• Whole-part relationship
Composition
• Composition is used for aggregations where the life span of the
part depends on the life span of the aggregate.
Example
Problem statement: “Our Company maintains a group of race cars. Our cars use some of our new 8-cylinder engines
and new transmissions. Once the engines are assembled, the pistons, carburetor, and plugs cannot be swapped
between engines due to changes caused by the high temperatures.
“We want to keep records of the performance achieved by each engine in each car and each transmission in
combination with each engine. Our drivers evaluate each car to give us their assessment of the handling. We need a
system to track the configurations and the drivers’ assessments.”
Generalization
• Generalization and Inheritance are used synonymously
• A generalization is not an association
• Generalization and Specialization
• Generalization is bottom-up and Specialization is top-down
Example
• “Our system is designed to inventory and ship uniquely identified products. These products may be purchased directly from vendors and resold
as is, or we can package vendor products together to make our own custom product. Customers place orders for one or more items, but we
acknowledge interested customers in the system whether they have purchased yet or not. Each item corresponds to a product. We identify each
product using a unique serial number. The Customer may inquire on the status of his Orders using the order number.”
• “Shipments of products from vendors are received and placed into inventory. Each product is assigned to a location so that we can easily find it
later when filling orders. Each location has a unique location identifier. Customer orders are shipped as the products become available, so there
may be more than one shipment to satisfy a single customer order. But a single shipment may contain products from multiple orders. Any items
that have not been shipped are placed on a backorder with a reference to the original order.”
Case Study: Solution
Case 1:
• One Student may attend any number of courses * attend *
Student Course
• One course may have any number of students
• Instructors teach courses 0 .. * 0 .. *

teach
has
• For every course there is at least one instructor
• Every instructor may teach zero or more courses 1 .. *
1 .. *
• A school has zero or more students 1
• Each student may be a registered member of one School Instructor
or more school
1 .. 1 1 .. *
• A school has one or more departments

has

chair
• Each department belongs to exactly one school
• Every instructor is assigned to one or more 1 .. *
departments Department
• Each department has one or more instructors
• For every department there is exactly one 1 .. *
instructor acting as the department chair assigned to
Case Study: Solution
Case 2:
• A building is owned by one person
• A person may own more than one building
• Each building is either a house or an apartment
• An apartment contains two or more Rental Units
• A rental unit only belongs to one and only one apartment block

1 owns *
Person Building

Rental Unit
2 .. *
Apartment House
belongs to
1 .. 1
Case Study: Solution
Case 3: Affiliated with
Organization
A nonprofit organization depends on a
number of different types of persons for its Person
successful operation. Persons are affiliated * Section
works in
with organizations. Three types of persons
are of greatest interest: employees,
volunteers, and donors. Employees and Employee

supervise
volunteers work in various projects. One Project
project consists of many sections. A
Volunteer
volunteer works under the supervision of an

sponsors
employee. To become a recognized donor, a
works in
donor must have donated one or more
items, and an item may have no donors, or
one or more donors. Projects are sponsored Donor Sponsor
by sponsors. Sometimes a single project is *
* donate
sponsored by multiple sponsors. For 1 .. *
sponsorship, an agreement must be signed.
Item Agreement
Case Study: Practice
Case 4:
• A company has one or more divisions
• A division has one or more departments
• A company can have one or more persons working as employees
• A person works in a company as an employee
• A department has one or more persons assigned to it
• A person is assigned to one department
Case 5:
• There are members in the library. Members can borrow books. There can be
short-term borrowing or long-term borrowing. There are also journals in the
library. There is a special type of member- Member of Staff, who can borrow
the journals. The system must keep track when books and journals are
borrowed.
Case Study: Practice
Case 6:
• A member can be of three types- potential member, past member and club
member. Club members place orders. One order must include at least one
product. One product can be in many orders. There are two types of products -
club souvenirs and club facilities. Each member must sign an agreement. One
agreement can be made for more than one member. Club facilities are courts,
tables and halls. Courts are Badminton court and Tennis Court. Tables are for
Tennis and Pool. Club members can also book club facilities. For each booking,
date and duration of booking must be stored.

You might also like