KEMBAR78
Object and Class Diagrams | PDF | Inheritance (Object Oriented Programming) | Class (Computer Programming)
0% found this document useful (0 votes)
6 views85 pages

Object and Class Diagrams

The document outlines the software specification for 2IX20, focusing on object and class diagrams in UML. It discusses structural design principles, including the importance of traceability and the relationships between objects and classes. Additionally, it covers various notations, attributes, operations, and associations, emphasizing the significance of aggregation and composition in modeling class relationships.

Uploaded by

schoolvandongen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views85 pages

Object and Class Diagrams

The document outlines the software specification for 2IX20, focusing on object and class diagrams in UML. It discusses structural design principles, including the importance of traceability and the relationships between objects and classes. Additionally, it covers various notations, attributes, operations, and associations, emphasizing the significance of aggregation and composition in modeling class relationships.

Uploaded by

schoolvandongen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

2IX20 Software Specification

OBJECT DIAGRAMS

Based on the slides provided by M.Seidl,


M.Scholz, C. Huemer and G.Kappel,
authors of the book UML@Classroom
Under the Creative Commons Attribution-
NonCommercial 4.0 International licence.
See http://www.uml.ac.at/en/lernen.

Jeroen Keiren

Mathematics & Computer Science / Formal System Analysis


Structural design

• How will we build it? (design, not implementation!)


• Focus on how to meet the requirements
• Traceability: ensure that we address all requirements
For all design features: make explicit from what requirement this design feature comes

2 2IX20 Object diagrams


What is structure?

• Structure: the aggregate of elements of an entity in their relationships


to each other. Merriam-Webster

• Structural modelling: discover the key data contained in the problem


domain and to build a structural model of the objects

3 2IX20 Object diagrams


So, what is a structure diagram?

A diagram that identifies modules, activities, or other entities in a system


or computer program and shows how larger or more general entities
break down into smaller, more specific entities.

- IEEE Standard Glossary of Software Engineering Terminology 610.12 1990

4 2IX20 Object diagrams


Object diagrams

Diagram

Structure Behavior
Diagram Diagram

Class Package Profile Object Activity State Machine Interaction Use Case
Diagram Diagram Diagram Diagram Diagram Diagram Diagram Diagram

Deployment Composition Component Sequence Communication Interaction Timing


Diagram Structure Diagram Diagram Diagram Overview Diagram
Diagram Diagram

5 2IX20 Object diagrams


six possible ways of notating it: with/without attributes with their current value(s) etc.

Object
the object is an instance of a class

• Individuals of a system Object name Class


• Alternative notations: We typically underline it,
so that it is clear that it is
an instance

Anonymous objects
Attribute Current value = no object name

6 2IX20 Object diagrams


Object Diagram :Not only the objects, but also the relationships between them

• Objects of a system and their relationships (links)


• Snapshot of objects at a specific moment in time

7 2IX20 Object diagrams


Link
From Object to Class

• Individuals of system often have identical characteristics and Class


behavior
• Class is construction plan for set of similar objects of a system

• Objects are instances of classes


• Attributes: structural characteristics of a class Object of that class
• Different value for each instance (= object)
• Operations: behavior of a class
• Identical for all objects of a class
à not depicted in object diagram , only in the class diagram

8 2IX20 Object diagrams


Notation Elements
Name Notation Description

Object Instance of a class

Link Relationship between objects

9 2IX20 Object diagrams


Summary

10 2IX20 Object diagrams


2IX20 Software Specification
CLASS DIAGRAMS: CLASSES

Based on the slides provided by M.Seidl,


M.Scholz, C. Huemer and G.Kappel,
authors of the book UML@Classroom
Under the Creative Commons Attribution-
NonCommercial 4.0 International licence.
See http://www.uml.ac.at/en/lernen.

Jeroen Keiren

Mathematics & Computer Science / Formal System Analysis


Class diagrams

Diagram

Structure Behavior
Diagram Diagram

Class Package Profile Object Activity State Machine Interaction Use Case
Diagram Diagram Diagram Diagram Diagram Diagram Diagram Diagram

Deployment Composition Component Sequence Communication Interaction Timing


Diagram Structure Diagram Diagram Diagram Overview Diagram
Diagram Diagram

12 2IX20 Class diagrams: classes


Recall: V-model

List requirements

Use case modelling

Class diagrams

13 2IX20 Class diagrams: classes


Class

Class name

Attributes

Operations

14 2IX20 Class diagrams: classes


Attribute Syntax

15 2IX20 Class diagrams: classes


Attribute Syntax - Visibility

Who is permitted to access the attribute


• + ... public: everybody
• - ... private: only the object itself
• # ... protected: class itself and subclasses
• ~ ... package: classes that are in the same package

16 2IX20 Class diagrams: classes


Attribute Syntax - Derived Attribute

Attribute value is derived from other attributes


• age: calculated from the date of birth

17 2IX20 Class diagrams: classes


Attribute Syntax - Name

Name of the attribute

18 2IX20 Class diagrams: classes


Attribute Syntax - Type

Type
• User-defined classes
• Data type
• Primitive data type
• Pre-defined: Boolean, Integer, UnlimitedNatural, String
• User-defined: «primitive»
• Composite data type: «datatype»
• Enumerations: «enumeration»

19 2IX20 Class diagrams: classes


Attribute Syntax - Multiplicity

• Number of values an attribute may contain


• Default value: 1
• Notation: [min..max]
• no upper limit: [*] or [0..*]

20 2IX20 Class diagrams: classes


Attribute Syntax – Default Value

Default value
• Used if the attribute value is not set explicitly by the user

21 2IX20 Class diagrams: classes


Attribute Syntax – Properties

§ Pre-defined properties
§ {readOnly} … value cannot be changed
§ {unique} … no duplicates permitted
§ {non-unique} … duplicates permitted
§ {ordered} … fixed order of the values
§ {unordered} … no fixed order of the values
§ Attribute specification
§ Set: {unordered, unique}
§ Multi-set: {unordered, non-unique}
§ Ordered set: {ordered, unique}
§ List: {ordered, non-unique}

22 2IX20 Class diagrams: classes


Operation Syntax - Parameters

• Notation similar to attributes


• Direction of the parameter
• in … input parameter
• When operation is used, value is expected from parameter
• out … output parameter
• After execution of operation, parameter has adopted new value
• inout : combined input/output parameter

23 2IX20 Class diagrams: classes


Operation Syntax - Type

Type of the return value

24 2IX20 Class diagrams: classes


Class Variable and Class Operation
Class
• Instance variable (= instance attribute): variable
• attributes defined on instance level
Class
• Class variable (= class attribute, static attribute) operation
• Defined only once per class, i.e., shared by all
instances of the class
• E.g. counters for the number of instances of a class,
constants, etc. class Person {

• Class operation (= static operation) public String firstName;


• Can be used if no instance of the corresponding class public String lastName;
private Date dob;
was created protected String[] address;
• E.g. constructors, counting operations, math. private static int pNumber;
public static int getPNumber() {…}
functions (sin(x)), etc. public Date getDob() {…}
}
• Notation: underlining name of class variable / class operation
25 2IX20 Class diagrams: classes
Class attributes and operations - Summary
Visibilities are used to realize information hiding, an important concept in computing
Marking the attributes that represent the state of an object as private protects this state
against unauthorized access. Access is therefore only possible via a clearly defined
E.g.: age derived from date of birth
interface, such as via operations that are declared public. Page 58/59 UML
• Name
• Type
Person
• Visibility: + (public), - (private), # (protected), ~ (package) +firstName: String
• Derived attributes: / we can calculate this value based on other values
of this instance
+lastName: String
+dob: Date
• Multiplicity: [min..max] at least .. (Min value) and at most .. (Max value) #address: String[1..*] {unique,ordered}
-ssNo: String {readOnly}
• Default value: = … If there is no limit we use asterisk: *
Note that [0..*] is equivalent to [*]
-/age: int
-password: String = "pw123"
• Properties: unique, ordered, readOnly,Note that [1] … is default multiplicity -personsNumber: Int
• Direction of parameters: in, out, inout +getName(out fn: String, out ln: String): void
+updateLastName(newName: String): boolean
• Class variables/operations: underlined; static +getPersonsNumber(): int

Page 59 of UML@Classroom

See page 56 of UML@Classroom about unique, non-unique etc.


See page 57 of UML@Classroom about in, out, inout

26 2IX20 Class diagrams: classes


Specification of Classes: Different Levels of Detail

coarse-grained fine-grained

You want a high level design: keep it simple you want to work towards an implementation: go for a more detailed
design
27 2IX20 Class diagrams: classes
Summary

28 2IX20 Class diagrams: classes


2IX20 Software Specification
CLASS DIAGRAMS: ASSOCIATIONS

Based on the slides provided by M.Seidl,


M.Scholz, C. Huemer and G.Kappel,
authors of the book UML@Classroom
Under the Creative Commons Attribution-
NonCommercial 4.0 International licence.
See http://www.uml.ac.at/en/lernen.

Jeroen Keiren

Mathematics & Computer Science / Formal System Analysis


Summary

30 2IX20 Class diagrams: associations


Association

Models possible relationships between instances of classes

31 2IX20 Class diagrams: associations


Binary Association

Models possible relationships between instances of classes


Association name Reading direction
Navigability Multiplicity

Non-navigability
Visibility Role From student we can go to professor but not the other way
around.

32 2IX20 Class diagrams: associations


Binary Association - Navigability

• Navigability: an object knows its partner objects and can therefore


access their visible attributes and operations
• Indicated by open arrow head
• Non-navigability
• Indicated by cross
• Example
• A can access the visible attributes and operations of B
• B cannot access any attributes and operations of A
• Navigability undefined
• Bidirectional navigability is assumed

33 2IX20 Class diagrams: associations


Navigability – UML Standard vs. Best Practice
UML standard Best practice

34 2IX20 Class diagrams: associations


Binary Association as Attribute

Preferable

class Professor {…}

Java-like notation: class Student{


public Professor[] lecturer;

}

35 2IX20 Class diagrams: associations


Binary Association – Multiplicity and Role

• Multiplicity: Number of objects that may be associated with exactly one


object of the opposite side

• Role: describes the way in which an object is involved in an association


relationship

36 2IX20 Class diagrams: associations


Binary Association – xor constraint

• “exclusive or” constraint


• An object of class A is to be associated with an object of class B or an
object of class C but not with both.

37 2IX20 Class diagrams: associations


Unary Association - Example

38 2IX20 Class diagrams: associations


n-ary Association (1/2)

• More than two partner objects are involved in the relationship.


• No navigation directions

Ternary
association

39 2IX20 Class diagrams: associations


n-ary Association (2/2)
Example
• (Student, Exam) à (Lecturer)
• One student takes one exam with one or no lecturer
• (Exam, Lecturer) à (Student)
• One exam with one lecturer can be taken by any number of students
• (Student, Lecturer) à (Exam)
• One student can be graded by one Lecturer for any number of exams

40 2IX20 Class diagrams: associations


Association Class

• Assign attributes to the relationship between classes rather than to a


class itself

41 2IX20 Class diagrams: associations


Association Class
In a many-to-many relationship a association class it is necessary to
include when such information is there

• Assign attributes to the relationship


between classes rather than to a
class itself
Association class
• Necessary when modeling n:m
Associations
• With 1:1 or 1:n possible but not
necessary
When we have 1:n, the information can just be included on the many side

42 2IX20 Class diagrams: associations


Association Class vs. Regular Class

A Student can enroll for one A Student can have mutiple


particular StudyProgram only Enrollments for one and the
once same StudyProgram

43 2IX20 Class diagrams: associations


Association Class – unique/non-unique (1/2)
• Default: no duplicates • non-unique: duplicates allowed

A student can only be granted an A student can have more than one
exam meeting for a specific exam exam meetings for a specific exam.
once.

44 2IX20 Class diagrams: associations


Association Class – unique/non-unique (2/2)

45 2IX20 Class diagrams: associations


Notation Elements
Name Notation Description

Description of the structure and behavior


Class
of a set of objects

oder

Abstract class Class that cannot be instantiated

Relationship between classes:


navigability unspecified,
Association
navigable in both directions,
not navigable in one direction

46 2IX20 Class diagrams: associations


Notation Elements
Name Notation Description

n-ary association Relationship between n (here 3) classes

More detailed description of an


Association class
association

An object of C is in a relationship with an


xor relationship object of A or with an object of B but not
with both

47 2IX20 Class diagrams: associations


2IX20 Software Specification
CLASS DIAGRAMS: AGGREGATION, COMPOSITION AND GENERALIZATION

Based on the slides provided by M.Seidl,


M.Scholz, C. Huemer and G.Kappel,
authors of the book UML@Classroom
Under the Creative Commons Attribution-
NonCommercial 4.0 International licence.
See http://www.uml.ac.at/en/lernen.

Jeroen Keiren

Mathematics & Computer Science / Formal System Analysis


Aggregation

• Special form of association


• Used to express that a class is part of another class
• Properties of the aggregation association:
• Transitive: if B is part of A and C is part of B, C is also part of A Rules out
• Asymmetric: it is not possible for A to be part of B and B to be part of A cyclic
relationships
simultaneously.

• Two types: (difference: lifespan that they have)


• Shared aggregation
• Composition

49 2IX20 Classes: aggregation, composition and generalization


Shared Aggregation
A is a part of B: An instance of A can also exist without the existence of class B

• Expresses a weak belonging of the parts to a whole


• = Parts also exist independently of the whole
• Multiplicity at the aggregating end may be >1 If the labclass is
removed, the student
• = One element can be part of multiple other elements simultaneously will still exist.

• Spans a directed acyclic graph


• Syntax: Hollow diamond at the aggregating end

Example:
• Student is part of LabClass
• Course is part of StudyProgram

50 2IX20 Classes: aggregation, composition and generalization


Composition
Composition: If the composite object B is deleted, then its part A is also deleted.

• Existence dependency between the composite object and its parts


• One part can only be contained in at most one composite object at one
specific point in time
• Multiplicity at the aggregating end max. 1 -> The composite objects form a tree
• If the composite object is deleted, its parts are also deleted. So every Beamer can only
be part of one LectureHall
• Syntax: Solid diamond at the aggregating end in this case.

Example: Beamer is part of LectureHall is part of Building

If the Building is deleted, The Beamer can exist without the LectureHall,
the LectureHall is also deleted but if it is contained in the LectureHall while it is
deleted, the Beamer is also deleted
Note: if the Beamer was NOT contained in a LectureHall, then it is not deleted.
51 2IX20 Classes: aggregation, composition and generalization
Shared Aggregation and Composition

Which model applies?

52 2IX20 Classes: aggregation, composition and generalization


Shared Aggregation and Composition

Which model applies?

----Yes---
A Tire can exist without a Car. A
Tire belongs to one Car at most.

---------No--------
A Tire cannot exist without a Car.

A Tire can belong to multiple Cars

----Yes---
A Car has one or two types of Tires.
Several Cars may have the same Type
of Tires.

53 2IX20 Classes: aggregation, composition and generalization


Generalization

• Characteristics (attributes and operations), Superclass


associations, and aggregations specified for
superclass are passed on to its subclasses.
• Every instance of subclass is indirect instance of
superclass Subclasses
• Subclass inherits all characteristics, associations, and …inherit
characteristics,
aggregations of the superclass except private ones associations, and
aggregations
• Subclass may have further characteristics,
associations, and aggregations. A Secretary is
an Employee and
• Generalizations are transitive. a Person

54 2IX20 Classes: aggregation, composition and generalization


Generalization – Abstract Class

• Used to highlight common characteristics of subclasses


• Used to ensure there are no direct instances of the superclass
• Only non-abstract subclasses can be instantiated
• Useful in context of generalization relationships
• Notation: keyword {abstract} or class name in italic font.
No Person-object possible

Two types of Person: Man and Woman

55 2IX20 Classes: aggregation, composition and generalization


Generalization – Multiple Inheritance

• UML allows multiple inheritance.


• A class may have multiple superclasses.

• Example:

A Tutor is both an Employee and a Student

56 2IX20 Classes: aggregation, composition and generalization


With and Without Generalization

57 2IX20 Classes: aggregation, composition and generalization


Question

For classes A,B,C,D which class diagram(s) is/are illegal?

A) C)

B) Illegal (cycle) Illegal (cycle)


D)

58 2IX20 Classes: aggregation, composition and generalization


Dependency relations

• Directed relationship
• Indicates one element
requires/needs/depends on
other elements

• Less frequently shown than


other relations

Source: https://www.uml-diagrams.org/dependency.html

59 2IX20 Classes: aggregation, composition and generalization


Dependencies - Example

• Course and Person realize interface


Printable

• Printer uses Printable interface.

Dependencies are indicated by dashed lines

Here both Course and Person use/realize the Printable interface


and hence they depend on the Printable interface.

60 2IX20 Classes: aggregation, composition and generalization


Example – Composite pattern

61 2IX20 Classes: aggregation, composition and generalization


Example – Instantiation of composite pattern

62 2IX20 Classes: aggregation, composition and generalization


Notation Elements Page 70 - 72, write these down in notebook: complete/incomplete etc.

Name Notation Description

Shared aggregation Parts-whole relationship (A is part of B)

Strong aggregation Existence-dependent parts-whole


= composition relationship (A is part of B)

Inheritance relationship (A inherits from


Generalization
B)

63 2IX20 Classes: aggregation, composition and generalization


2IX20 Software Specification
CLASS DIAGRAMS: CREATING A CLASS DIAGRAM

Based on the slides provided by M.Seidl,


M.Scholz, C. Huemer and G.Kappel,
authors of the book UML@Classroom
Under the Creative Commons Attribution-
NonCommercial 4.0 International licence.
See http://www.uml.ac.at/en/lernen.

Jeroen Keiren

Mathematics & Computer Science / Formal System Analysis


Creating a Class Diagram

• Not possible to completely extract classes, attributes and associations


from a natural language text automatically
• Guidelines
• Nouns often indicate classes
• Adjectives and nouns often indicate attribute values (or attributes)
• Verbs indicate operations

Question: What about Ann Foster?


65 2IX20 Classes: creating a class diagram
Creating a Class Diagram - Example

• The library management system stores users with their unique ID,
name and address as well as books with their title, author and ISBN
number. Ann Foster wants to use the library.

Question: What about Ann Foster? Ann Foster is an instance of the class User.

66 2IX20 Classes: creating a class diagram


Example – University Information System

• A university consists of multiple faculties which are composed of various


institutes. Each faculty and each institute has a name. An address is known for
each institute.
• Each faculty is led by a dean, who is an employee of the university.
• The total number of employees is known. Employees have a social security
number, a name, and an email address. There is a distinction between
research and administrative personnel.
• Research associates are assigned to at least one institute. The field of study of
each research associate is known. Furthermore, research associates can be
involved in projects for a certain number of hours, and the name, starting
date, and end date of the projects are known. Some research associates hold
courses. Then they are called lecturers.
• Courses have a unique number (ID), a name, and a weekly duration in hours.
67 2IX20 Classes: creating a class diagram
Example – Step 1: Identifying Classes We model the system
„University“
• A university consists of multiple faculties which are composed of
various institutes. Each faculty and each institute has a name. An
address is known for each institute.
• Each faculty is led by a dean, who is an employee of the university.
• The total number of employees is known. Employees have a social
security number, a name, and an email address. There is a distinction
between research and administrative personnel.
• Research associates are assigned to at least one institute. The field of
study of each research associate is known. Furthermore, research
associates can be involved in projects for a certain number of hours,
and the name, starting date, and end date of the projects are known.
Some research associates hold courses. Then they are called
lecturers.
• Courses have a unique number (ID), a name, and a weekly duration
in hours. Dean has no further attributes
than any other employee

68 2IX20 Classes: creating a class diagram


Example – Step 2: Identifying the Attributes
We delete university as we that is the system we
are modelling and no instance of university will
ever be needed,
• A university consists of multiple faculties which are composed of
various institutes. Each faculty and each institute has a name. An
address is known for each institute. a role/association between
employee and faculty
• Each faculty is led by a dean, who is an employee of the university.
• The total number of employees is known. Employees have a social
security number, a name, and an email address. There is a distinction
between research and administrative personnel. shown by static attribute
• Research associates are assigned to at least one institute. The field of
study of each research associate is known. Furthermore, research
associates can be involved in projects for a certain number of hours,
and the name, starting date, and end date of the projects are known.
Some research associates hold courses. Then they are called
lecturers.
• Courses have a unique number (ID), a name, and a weekly duration in
hours.

69 2IX20 Classes: creating a class diagram


Example – Step 3: Identifying Relationships (1/6)
Abstract, i.e., no other types
of employees
• Three kinds of relationships:
• Association
• Generalization
• Aggregation

• Indication of a generalization
• “There is a distinction between research
and administrative personnel.”
• “Some research associates hold courses. Then they
are called lecturers.”

70 2IX20 Classes: creating a class diagram


Example – Step 3: Identifying Relationships (2/6)

“A university consists of multiple faculties which are composed of various


institutes.”

Composition to show existence


dependency

71 2IX20 Classes: creating a class diagram


Example – Step 3: Identifying Relationships (3/6)

“Each faculty is led by a dean, who is an employee of the university”

In the leads-relationship, the


Employee takes the role of a dean.

72 2IX20 Classes: creating a class diagram


Example – Step 3: Identifying Relationships (4/6)

“Research associates are assigned to at least one institute.”

Shared aggregation to show that ResearchAssociates


are part of an Institute,
but there is no existence dependency
As research associates are allowed to be part of/assigned to more than one institute. This suggests that a
research associate can exist without an institute. There is no institute of which it is an inherit part, so no existence
dependency.

Remember: if we use composition, we could have at most one institute here (not 1..* at the institute side)

73 2IX20 Classes: creating a class diagram


Example – Step 3: Identifying Relationships (5/6)

“Furthermore, research associates can be involved in projects for a certain


number of hours.”

Association class enables to store


the number of hours for every
single Project of every single
ResearchAssociate
research association - project is a many-to-many relationship, so
this suggests that we can use an association class to store the
number of hours for a project of every single research associate
per project.

74 2IX20 Classes: creating a class diagram


Example – Step 3: Identifying Relationships (6/6)

“Some research associates hold courses. Then they are called lecturers.”

Lecturer inherits all characteristics,


associations, and aggregations from
ResearchAssociate.
In addtion, a Lecturer has an association
teaches to Course.

75 2IX20 Classes: creating a class diagram


Example – Complete Class Diagram

76 2IX20 Classes: creating a class diagram


77 2IX20 Classes: creating a class diagram
2IX20 Software Specification
CLASS DIAGRAMS: GENERATING CODE

Based on the slides provided by M.Seidl,


M.Scholz, C. Huemer and G.Kappel,
authors of the book UML@Classroom
Under the Creative Commons Attribution-
NonCommercial 4.0 International licence.
See http://www.uml.ac.at/en/lernen.

Jeroen Keiren

Mathematics & Computer Science / Formal System Analysis


Code Generation

• Class diagrams often created with intention of implementing modeled


elements in object-oriented programming language
• Often, translation is semi-automatic and requires minimal manual
intervention

Q: Do we still call such a class diagram a specification?


design or implementation? Somewhere in the middle

79 2IX20 Lecture 3
Code Generation – Example (1/6)

class Course {
public int courseNo;
}

80 2IX20 Lecture 3
Code Generation – Example (2/6)

abstract class UniversityMember {


public String firstName;
public String lastName;
public int ssNo;
}

81 2IX20 Lecture 3
Code Generation – Example (3/6)

Enumeration ESemester {
winter,
summer
}

Enumeration ERole {
lecturer,
tutor,
examiner
}

82 2IX20 Lecture 3
Code Generation – Example (4/6)

class Student extends UniversityMember


{ UniversityMember {
public int matNo;
public CourseExecution []
completedCourses;
completedCourses;
completes
}

83 2IX20 Lecture 3
Code Generation – Example (5/6)

class
class Employee
Employee extends
extends UniversityMember
UniversityMember {{
private
private int
int acctNo;
acctNo;
public
public int
int getAcctNo
getAcctNo ()
{ {
return
return acctNo;
acctNo;
}}
public
public CourseExecution
CourseExecution [][] courseExecutions;
courseExecution;
}}

84 2IX20 Lecture 3
Code Generation – Example (6/6)

class CourseExecution {
public int year;
public ESemester semester;
public Student [] student;
public Course course;
public Hashtable support;
// Key: employee
// Value: (role, hours)
}

85 2IX20 Lecture 3

You might also like