Lab 3-Class Diagram

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Lab 3:

Class Diagram

15-Nov-20 T.A GHADEER AL-HOMYANI 1


Objectives
-To explain what is class diagram and its essential
elements.

15-Nov-20 T.A GHADEER AL-HOMYANI 2


Introduction
Classes and objects describe the elements in the system,
while the relationships reveal communication and
interaction.

15-Nov-20 T.A GHADEER AL-HOMYANI 3


What is Class Diagram
- 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.

15-Nov-20 T.A GHADEER AL-HOMYANI 4


Element of Class Diagram
1. Class

5. Constraint
2. Attribute
Rules & Note

4.
3. Operation
Relationship

15-Nov-20 T.A GHADEER AL-HOMYANI 5


1. Class
- A class is a description of a set of objects
that share the same attributes, operations, Name
relationships, and semantics.
attributes
- Classes are templates for creating instances
or objects.
operations
- A class is drawn with a rectangle, usually, divided into three
compartments:
◦ Name
◦ Attributes
◦ Operations
15-Nov-20 T.A GHADEER AL-HOMYANI 6
1. Class (cont.)
-The top compartment of the class rectangle
contains the name of the class. Name
-It is capitalized and centered in boldface.
attributes
-The name should be derived from the
problem domain and should be as
unambiguous as possible. operations

-Therefore, it should be a noun, for


example, Invoice or Debt.

15-Nov-20 T.A GHADEER AL-HOMYANI 7


2. Attribute
- An attribute is a named property of a
class that describes the characteristics of
the object being modeled. Person

- In the class diagram, attributes appear in name


the second compartment just below the address
birthdate
name-compartment. ssn
- Attribute names typically begin with a
lowercase letter.

15-Nov-20 T.A GHADEER AL-HOMYANI 8


2. Attribute (cont.)

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.

15-Nov-20 T.A GHADEER AL-HOMYANI 9


2. Attribute (cont.)
-The attributes can have different visibility. Person
-Visibility describes whether the attribute can be
referenced from classes other than the one in
which they are defined. + name
◦ + public: it can be used and viewed outside that class. # address
◦ - private: it cannot be accessed from other classes. # birthdate
◦ # protected: it is private to the class, but visible to any - ssn
subclasses through generalization and specialization.
-If no sign is displayed, this means that the
visibility is undefined (there is no default
visibility).
15-Nov-20 T.A GHADEER AL-HOMYANI 10
2. Attribute (cont.)
Person
-A “/” indicates that the attribute
is derived. For example, the age + name
of a person might be derived as # address
the current date minus the # birthdate
birthdate. / age
- ssn

15-Nov-20 T.A GHADEER AL-HOMYANI 11


2. Attribute (cont.)
Person
-An attribute has a type, which tells you what
kind of attribute it is.
+ name : String
-Typical attribute types are integer, Boolean, # address : String
string, date, real, floating point, and # birthdate : Date
enumeration, which are called data types. - ssn : Integer

15-Nov-20 T.A GHADEER AL-HOMYANI 12


2. Attribute (cont.)
-Multiplicity shows the number of
instances of the attribute in square Person
brackets.
Eg: + name : String
◦ [0..1] – this attribute can have a value of null # address : String [*]
◦ [1..*] – this attribute's value is a collection # birthdate : Date
that contains at least one value - ssn : Integer
◦ [*] – this attribute's value is a collection of
values
-It is omitted if the multiplicity is 1.
15-Nov-20 T.A GHADEER AL-HOMYANI 13
2. Attribute (cont.)
-An attribute also can have a default value. Person
Eg: The default value of date is Current
date. + name : String
# address : String [*]
# birthdate : Date
+date : Date = Current date
- ssn : Integer

15-Nov-20 T.A GHADEER AL-HOMYANI 14


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

15-Nov-20 T.A GHADEER AL-HOMYANI 15


3. Operation
Person
-Operation describe the class behavior and
appear in the third compartment. name : String
address : String
-Operations are normally called functions, birthdate : Date
but they are inside a class and can be applied ssn : int
only to objects of that class or public
attributes of other class. display()
create()
modify()
delete()

15-Nov-20 T.A GHADEER AL-HOMYANI 16


3. Operation
The formal syntax for an operation is:
visibility name ( parameter-list ) : return-type-expression { propertystring}

Person

+name : String
address : String
birthdate : Date
ssn : int
+display(ssn:Integer):String{readOnly}
Create()
modify()
delete()

15-Nov-20 T.A GHADEER AL-HOMYANI 17


4. Relationship

- Association
- Generalization
- Dependency

15-Nov-20 T.A GHADEER AL-HOMYANI 18


4.1 Association
-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

15-Nov-20 T.A GHADEER AL-HOMYANI 19


4.1 Association (cont.)
-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..*

15-Nov-20 T.A GHADEER AL-HOMYANI 20


4.1 Association (cont.)
Eg: Every instructor teaches one or more Students

Student Instructor
1..*

15-Nov-20 T.A GHADEER AL-HOMYANI 21


4.1 Association (cont.)
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

15-Nov-20 T.A GHADEER AL-HOMYANI 22


4.1 Association (cont.)
-We can also name the association.

learn from
Student Instructor
teach

15-Nov-20 T.A GHADEER AL-HOMYANI 23


4.1 Association (cont.)
-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

15-Nov-20 T.A GHADEER AL-HOMYANI 24


4.1 Association (cont.)
-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

15-Nov-20 T.A GHADEER AL-HOMYANI 25


4.1 Association (cont.)
-A class can have a self association.
prerequisite

Course

15-Nov-20 T.A GHADEER AL-HOMYANI 26


4.1 Association (cont.)
-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

15-Nov-20 T.A GHADEER AL-HOMYANI 27


4.1 Association (cont.)
-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.

Book Page

15-Nov-20 T.A GHADEER AL-HOMYANI 28


4.2 Generalization
-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

15-Nov-20 T.A GHADEER AL-HOMYANI 29


4.2 Generalization (cont.)
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

15-Nov-20 T.A GHADEER AL-HOMYANI 30


4.3 Dependency
-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)

15-Nov-20 T.A GHADEER AL-HOMYANI 31


5. Constraint Rules and Notes
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

15-Nov-20 T.A GHADEER AL-HOMYANI 32


Example 1
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.

15-Nov-20 T.A GHADEER AL-HOMYANI 33


Solution

15-Nov-20 T.A GHADEER AL-HOMYANI 34


Example 2

15-Nov-20 T.A GHADEER AL-HOMYANI 35

You might also like