0% found this document useful (0 votes)
122 views

Lecture 4 Class Diagrams

This document discusses the elements of a class diagram, including classes, attributes, operations, and relationships. A class diagram shows the structure of a system using classes, their attributes, operations, and relationships. Key elements include: 1. Classes which define objects and their characteristics 2. Attributes which describe class properties 3. Operations which define class behaviors 4. Relationships between classes including association, generalization, and dependency.

Uploaded by

alolmani_oop
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Lecture 4 Class Diagrams

This document discusses the elements of a class diagram, including classes, attributes, operations, and relationships. A class diagram shows the structure of a system using classes, their attributes, operations, and relationships. Key elements include: 1. Classes which define objects and their characteristics 2. Attributes which describe class properties 3. Operations which define class behaviors 4. Relationships between classes including association, generalization, and dependency.

Uploaded by

alolmani_oop
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

IS223D: OBJECT-ORIENTED DESIGN

Information Systems
Department Lecture 4: Class Diagrams
Objectives
2

 To explain what is class diagram and its essential


elements.

13-Feb-16 Information Systems Department


Introduction
3

 Classes and objects describe the elements in the


system, while the relationships reveal communication
and interaction.

13-Feb-16 Information Systems Department


What is Class Diagram
4

 A class diagram describes the static view (structure)


of a system.
 A class diagram shows only classes, not objects.
 It shows the system's:
 classes

 attributes

 operations

 relationships among the classes.

13-Feb-16 Information Systems Department


Element of Class Diagram
5

1. Class

5. Constraint
Rules & 2. Attribute
Note

4. 3.
Relationship Operation

13-Feb-16 Information Systems Department


1. Class
6

 A class is a description of a set of


objects that share the same attributes, Name
operations, relationships, and
semantics.
attributes
 Classes are templates for creating
instances or objects.
 A class is drawn with a rectangle, operations
usually, divided into three
compartments:
 Name
 Attributes
 Operations

13-Feb-16 Information Systems Department


1. Class (cont.)
7

 The top compartment of the class


Name
rectangle contains the name of
the class.
attributes
 It is capitalized and centered in
boldface.
operations
 The name should be derived from
the problem domain and should
be as unambiguous as possible.
 Therefore, it should be a noun, for
example, Invoice or Debt.
13-Feb-16 Information Systems Department
2. Attribute
8

 An attribute is a named property Person


of a class that describes the
characteristics of the object being name
modeled. address
birthdate
 In the class diagram, attributes ssn
appear in the second compartment
just below the name-compartment.
 Attribute names typically begin
with a lowercase letter.

13-Feb-16 Information Systems Department


2. Attribute (cont.)
9

 UML has formal syntax for the description


of an attribute:

visibility / name : type [multiplicity] = default-value {


propertystring}

 You must have a name, but all other parts


are optional.

13-Feb-16 Information Systems Department


2. Attribute (cont.)
10

 The attributes can have different visibility.


 Visibility describes whether the attribute can Person
be referenced from classes other than the one
in which they are defined.
 + public: it can be used and viewed outside
that class. + name
 - private: it cannot be accessed from other # address
classes. # birthdate
 # protected: it is private to the class, but visible - ssn
to any subclasses through generalization and
specialization.
 If no sign is displayed, this means that the
visibility is undefined (there is no default
visibility).

13-Feb-16 Information Systems Department


2. Attribute (cont.)
11

 A “/” indicates that the


Person
attribute is derived. For
example, the age of a person + name
might be derived as the current # address
# birthdate
date minus the birthdate. / age
- ssn

13-Feb-16 Information Systems Department


2. Attribute (cont.)
12

 An attribute has a type, Person


which tells you what kind of
attribute it is.
+ name : String
 Typical attribute types are # address : String
integer, Boolean, string, date, # birthdate : Date
real, floating point, and - ssn : Integer
enumeration, which are
called data types.

13-Feb-16 Information Systems Department


2. Attribute (cont.)
13

 Multiplicity shows the number of


instances of the attribute in square Person
brackets.
 Eg: + name : String
 [0..1] – this attribute can have a # address : String [*]
value of null
# birthdate : Date
 [1..*] – this attribute's value is a
- ssn : Integer
collection that contains at least one
value
 [*] – this attribute's value is a
collection of values
 It is omitted if the multiplicity is 1.

13-Feb-16 Information Systems Department


2. Attribute (cont.)
14

 An attribute also can have a Person


default value.
 Eg: The default value of date + name : String
is Current date. # address : String [*]
# birthdate : Date
+date : Date = Current date
- ssn : Integer

13-Feb-16 Information Systems Department


2. Attribute (cont.)
15

 A property-string can be
Person
used to further describe an
attribute.
 A property-string is written + name : String
# address : String [*]
within curly braces; it is a # birthdate : Date
comma-separated list of +date : Date = Current date
- ssn : Integer {readOnly}
property values that apply to
the attribute.
 Eg: {readOnly}, {ordered},
and {sequence}

13-Feb-16 Information Systems Department


3. Operation
16

 Operation describe the class Person


behavior and appear in the third
compartment. name : String
address : String
 Operations are normally called birthdate : Date
functions, but they are inside a ssn : int
class and can be applied only to
eat()
objects of that class. sleep()
work()
play()

13-Feb-16 Information Systems Department


3. Operation
17

 The formal syntax for an operation is:

visibility name ( parameter-list ) : return-type-expression { propertystring}

Person

+name : String
address : String
birthdate : Date
ssn : int
+eat(name:String):String{readOnly}
sleep()
work()
play()
13-Feb-16 Information Systems Department
4. Relationship
18

 Association
 Generalization
 Dependency

13-Feb-16 Information Systems Department


4.1 Association
19

 If two classes in a model need to communicate with


each other, there must be link between them.
 An association denotes that link.
 Eg: Student learns from instructor.

Student Instructor

13-Feb-16 Information Systems Department


4.1 Association (cont.)
20

 We can indicate the multiplicity of an


association by adding multiplicity adornments to
the line denoting the association.
 Eg: A student learns from one or more

Instructors

Student Instructor
1..*

13-Feb-16 Information Systems Department


4.1 Association (cont.)
21

 Eg: Every instructor teaches one or more Students

Student Instructor
1..*

13-Feb-16 Information Systems Department


4.1 Association (cont.)
22

 Multiplicity
 the number of objects that participate in the association.
Multiplicity Indicators
Exactly one 1
Zero or more (unlimited) * (0..*)
One or more 1..*
Zero or one (optional association) 0..1
Specified range 2..4

13-Feb-16 Information Systems Department


4.1 Association (cont.)
23

 We can also name the association.

learn from
Student Instructor
teach

13-Feb-16 Information Systems Department


4.1 Association (cont.)
24

 We can also indicate the behavior of an object


in an association (i.e., the role of an object)
using rolenames attached to the end of the
association path.

* drive *
Car company car driver Person

13-Feb-16 Information Systems Department


4.1 Association (cont.)
25

 We can constrain the association relationship by defining the


navigability of the association.
 Eg: A navigable association says that a person can own many
cars, but it does not say anything about how many people can
own a car.
 The direction of the association indicates that the car has no
knowledge of the person.

owns *
Person Car

13-Feb-16 Information Systems Department


4.1 Association (cont.)
26

 A class can have a self association.

prerequisite

Course

13-Feb-16 Information Systems Department


4.1 Association (cont.)
27

 We can model objects that contain other objects by way of


special associations called aggregations and compositions.
 An aggregation specifies a whole-part relationship between
an aggregate (a whole) and a component part, where the
part can exist independently from the aggregate.
Aggregations are denoted by a hollow-diamond adornment
on the association.

Engine
Car
Door
13-Feb-16 Information Systems Department
4.1 Association (cont.)
28

 A composition indicates a strong ownership and coincident


lifetime of parts by the whole (i.e., they live and die as a
whole). Compositions are denoted by a filled-diamond
adornment on the association.

Scrollbar
1 1

Window Titlebar
1 1

Menu
1 1 .. *

13-Feb-16 Information Systems Department


4.2 Generalization
29

 A generalization connects a subclass to its superclass.


 It denotes an inheritance of attributes and behavior
from the superclass to the subclass and indicates a
specialization in the subclass of the more general
superclass.
Shape

Square

13-Feb-16 Information Systems Department


4.2 Generalization (cont.)
30

 A sub-class inherits from its super-class


 Attributes
 Operations
 Relationships

 A sub-class may
 Add attributes and operations
 Add relationships
 Refine (override) inherited operations

13-Feb-16 Information Systems Department


4.3 Dependency
31

 A dependency indicates a semantic relationship between two or


more elements.
 The dependency from CourseSchedule to Course exists because
Course is used in both the add and remove operations of
CourseSchedule.

CourseSchedule
Course
add(c : Course)
remove(c : Course)

13-Feb-16 Information Systems Department


5. Constraint Rules and Notes
32

 Constraints and notes annotate among other things


associations, attributes, operations and classes.
 Constraints are semantic restrictions noted as Boolean
expressions.
 UML offers many pre-defined constraints.

Customer
1 * may be
Order
canceled
id: long { value > 0 }

Constraint Note

13-Feb-16 Information Systems Department


Example
33

Draw a class diagram that fulfills the following statements:


 An insurance company has insurance contracts (zero or
more), which refer to one or more customers.
 A customer has insurance contracts (zero or more), which
refer to one insurance company.
 An insurance contract is between an insurance company
and one or more customers. The insurance contract
refers to both a customer (or customers) and an
insurance company.
 The insurance contract is expressed in an (zero or one)
insurance policy (a written contract of insurance).
 The insurance policy expresses an insurance contract.

13-Feb-16 Information Systems Department


Solution
34

13-Feb-16 Information Systems Department


Class Activity: University Courses
35

Draw a class diagram based on the following


requirements:
 All instructors consist of professor, associate professor,
and assistant professor.
 Departments offer many courses, but a course is
offered by one or more department.
 Courses are taught by instructors, who may teach up to
three courses.
 Instructors are assigned to one department.

 One instructor may chair a department.

13-Feb-16 Information Systems Department


Class Activity: University Courses
36

 Extend the class diagram to include the following


attributes:
a department includes the department ID (private),
department name (public), and description about the
department (public)
 an instructor includes the instructor ID (private),
instructor name (public) and department
 a course includes the course ID (private), course name
(public), course description (public), and prerequisite
course (protected)
13-Feb-16 Information Systems Department
Class Activity: University Courses
37

 Extend the class diagram to include the following


operations:
 Instructors can
 Selectcourse
 Prepare course plan

13-Feb-16 Information Systems Department


References
38

 “UML 2 Toolkit”, Hans-Erik Eriksson , Magnus Penker,


Brian Lyons, and David Fado, Chapter 4

13-Feb-16 Information Systems Department

You might also like