Session 13: Class Diagram
R2S Academy – Internal Use – Author: TUAN NGO
Table of Content
Class Diagram - Definition
Examples
Practice & Exercises
R2S Academy – Internal Use – Author: TUAN NGO
1
2
3
Objectives
Definition
Class Diagram
Class Diagram
Definition
Class vs Object
ü A Class is a blueprint for an object. Objects and classes go
hand in hand.
ü We use classes to create objects. So a class describes what an
object will be, but it isn't the object itself.
ü Classes describe the type of objects, while objects are usable
instances of classes. Each Object was built from the same set
of blueprints and therefore contains the same components
(properties and methods)
Class Diagram
Definition
What is Class diagram?
ü Class diagram is a graphical notation used to construct and
visualize object oriented systems. A class diagram describes
the structure of a system by showing the system's
§ classes
§ their attributes
§ operations (or methods)
§ and the relationships among objects
ü Class diagram is not only used for visualizing, describing, and
documenting different aspects of a system but also for
constructing executable code of the software application
Class Diagram
Definition
Use of Class diagram?
ü Describing the static view of the system
ü Showing the collaboration among the elements of the static view
ü Describing the functionalities performed by the system
ü Construction of software applications using object oriented
languages. Class diagrams are the only diagrams which can be
directly mapped with object-oriented languages and thus widely
used at the time of construction → the most popular UML
diagram in the coder community
Class Diagram
Definition
Elements
Class Diagram
Class Diagram
Elements
1. Main components of a Class 2. Relationships
Class Diagram
Elements
1. Vital components of a Class
1.1. Upper Section
1.2. Middle Section
1.3. Lower Section
2. Relationships
Class Diagram
Elements
1. Vital components of a Class
1.1. Upper Section: name of the class
ü Capitalize the initial letter of the class name
ü Place the class name in the center of the upper section
ü A class name must be written in bold format
1.2. Middle Section
1.3. Lower Section
2. Relationships
Class Diagram
Elements
1. Vital components of a Class
1.1. Upper Section
1.2. Middle Section: class attributes, with following characteristics:
ü A meaningful name assigned to the attribute, explain its usage
ü The accessibility of an attribute class is illustrated by the visibility factors
ü The attributes are written along with its visibility factors, which are:
public (+) | private (-) | protected (#) | and package (~)
1.3. Lower Section
2. Relationships
Class Diagram
Elements
1. Vital components of a Class
1.1. Upper Section
1.2. Middle Section: class attributes, with following characteristics:
1.3. Lower Section
2. Relationships
Class Diagram
Elements
1. Vital components of a Class
1.1. Upper Section
1.2. Middle Section
1.3. Lower Section: methods or operations. It demonstrates how a class
interacts with data. Each attribute has a type. Each operation has a signature
2. Relationships
Class Diagram
Elements
1. Vital components of a Class
1.1. Upper Section
1.2. Middle Section
1.3. Lower Section: Signatures (Data types)
2. Relationships
Class Diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association (Kết hợp)
2.2. Dependency (Phụ thuộc)
2.3. Generalization/ Inheritance (Kế thừa)
2.4. Realization (Hiện thực hóa)
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association (kết hợp)
ü A structural link between two peer classes
ü There is an association between Class1 and Class2
ü A solid line connecting two classes
ü It can form several types of associations: one-to-one, one-to-many, many-to-one
and many-to-many
ü Multiplicity: A multiplicity is a factor associated with an attribute. It specifies
how many instances of attributes are created when a class is initialized. If a
multiplicity is not specified, by default one is considered as a default multiplicity
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association - Examples
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association
4 types of association
ü unidirectional/ directed (một chiều)
ü bi-directional (hai chiều)
ü reflexive (phản xạ)
ü aggregation (tập hợp)
The mostly used associations are unidirectional and bi-directional.
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association ← Unidirectional/ directed
ü The directed association is related to the direction of flow
within association classes
ü In a directed association, the flow is directed. The
association from one class to another class flows in a single
direction only
It is denoted using a solid line with an arrowhead
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association ← Bi-directional
Two objects might store each other in fields
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association ← Reflexive
ü In a reflexive association, the instances of the
same class can be related to each other
ü Reflexive association states that a link or a
connection can be present within the objects of
the same class
ü This occurs when a class may have multiple
functions or responsibilities
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association ← Aggregation (has-a relationship)
ü It represents a "part of" relationship
ü Class2 is part of Class1 (collection)
ü Many instances (denoted by the *) of Class2 can be
associated with Class1
ü Objects of Class1 and Class2 have separate lifetimes
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.1. Association ← Aggregation ← Composition (part-of
relationship)
ü A special type of aggregation where parts are
destroyed when the whole is destroyed
ü Objects of Class2 live and die with Class1
ü Class2 cannot stand by itself
Class diagram
Elements
Aggregation relationship Composition relationship
Dependency In an aggregation relationship, a child can exist
independent of a parent
In a composition relationship, the child cannot
exist independent of the parent
Type of Relationship Has-a relationship Part-of relationship
Type of Association a weak association strong association
Examples A doctor has patients when the doctor gets
transfer to another hospital, the patients do not
accompany to a new workplace
A hospital and its wards. If the hospital is
destroyed, the wards also get destroyed
1. Vital components of a Class
2. Relationships
2.1. Association ← Aggregation ← Composition
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.2. Dependency
ü A special type of association
ü Exists between two classes if changes to the definition
of one may cause changes to the other (but not the
other way around)
ü Class1 depends on Class2
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.3. Generalization/ Inheritance
ü Represents an "is-a” / parent-child relationship
ü SubClass1 and SubClass2 are specializations of SuperClass
(Abstract Class)
Class diagram
Elements
1. Vital components of a Class
2. Relationships
2.4. Realization (hiện thực hóa)
ü One entity denotes some responsibility which is not
implemented by itself and the other entity that
implements them.
ü This relationship is mostly found in the case of interfaces
1. Vital components of a Class
2. Relationships
ü Exercise: Develop mind-map diagram for
Class Diagram Relationships
Class Diagram
Elements
1. Vital components of a Class
2. Relationships
Class Diagram
Elements
vs Use Case Diagram
Class Diagram
Class diagram
Use case diagram vs Class diagram
Use Case Diagram
Notations / Symbols
Relationships
ü Association: A line between actors and use cases represents
which actors are associated with which use cases
ü Include: defines that a use case contains the behavior
defined in another use case
ü Extend: specifies that the behavior of a use case may be
extended by the behavior of another use case
ü Generalization: represent inheritance relationship between
model elements of same type
Association Include Extend Generalization
Use case diagram Class diagram
What kind of? Behavioral diagram Structural diagram
What describe? Describes functional requirements Describes the structure of a system
How to describe?
By mentioning that who will perform what function and
which kind of association is exist between functions (use
cases)
By showing the system’s classes, their attributes,
functions and the relationships among objects
Supported
relationships
Association, includes, extends, Generalization Association, Dependency, Generalization, Realization
Specify Specify the behavior (what system will do?)
Specify the internal structure that will help to complete
a functionality mentioned in use cases
Representation
Use cases can be denoted both by textual (UC
description) and visual representation (UC diagram)
Only denoted by visual representation
What summarizes?
Only summarizes relationships between use cases,
actors, and system
Only summarizes relationships between classes of the
system
Order of execution
Does not show the order in which steps are performed
to achieve the goals of each use case
Does not show the order in which classes executes
Examples
Class Diagram
Class Diagram
Example 1 – Order System
Class Diagram
Example 2
Generalization/ Inheritance
Class Diagram
Example 3
ü Generalization/ Inheritance
ü Realization
Class Diagram
Example 4 - Package
Practice
Class Diagram
Session 13 Review
Class Diagram
Class Diagram
Session Review
Review content
1. What is multiplicity in Class diagram?
2. What is Reflexive association in Class diagram?
3. Aggregation relationship vs Composition relationship?
4. Use Case diagram vs Class diagram?
5. Mind-map diagram: develop a mind-map diagram to present all of Class diagram relationships types

Session13-Class Diagram (1).pdf

  • 1.
    Session 13: ClassDiagram R2S Academy – Internal Use – Author: TUAN NGO
  • 2.
    Table of Content ClassDiagram - Definition Examples Practice & Exercises R2S Academy – Internal Use – Author: TUAN NGO 1 2 3 Objectives
  • 3.
  • 4.
  • 5.
    Class vs Object üA Class is a blueprint for an object. Objects and classes go hand in hand. ü We use classes to create objects. So a class describes what an object will be, but it isn't the object itself. ü Classes describe the type of objects, while objects are usable instances of classes. Each Object was built from the same set of blueprints and therefore contains the same components (properties and methods) Class Diagram Definition
  • 6.
    What is Classdiagram? ü Class diagram is a graphical notation used to construct and visualize object oriented systems. A class diagram describes the structure of a system by showing the system's § classes § their attributes § operations (or methods) § and the relationships among objects ü Class diagram is not only used for visualizing, describing, and documenting different aspects of a system but also for constructing executable code of the software application Class Diagram Definition
  • 7.
    Use of Classdiagram? ü Describing the static view of the system ü Showing the collaboration among the elements of the static view ü Describing the functionalities performed by the system ü Construction of software applications using object oriented languages. Class diagrams are the only diagrams which can be directly mapped with object-oriented languages and thus widely used at the time of construction → the most popular UML diagram in the coder community Class Diagram Definition
  • 8.
  • 9.
    Class Diagram Elements 1. Maincomponents of a Class 2. Relationships
  • 10.
    Class Diagram Elements 1. Vitalcomponents of a Class 1.1. Upper Section 1.2. Middle Section 1.3. Lower Section 2. Relationships
  • 11.
    Class Diagram Elements 1. Vitalcomponents of a Class 1.1. Upper Section: name of the class ü Capitalize the initial letter of the class name ü Place the class name in the center of the upper section ü A class name must be written in bold format 1.2. Middle Section 1.3. Lower Section 2. Relationships
  • 12.
    Class Diagram Elements 1. Vitalcomponents of a Class 1.1. Upper Section 1.2. Middle Section: class attributes, with following characteristics: ü A meaningful name assigned to the attribute, explain its usage ü The accessibility of an attribute class is illustrated by the visibility factors ü The attributes are written along with its visibility factors, which are: public (+) | private (-) | protected (#) | and package (~) 1.3. Lower Section 2. Relationships
  • 13.
    Class Diagram Elements 1. Vitalcomponents of a Class 1.1. Upper Section 1.2. Middle Section: class attributes, with following characteristics: 1.3. Lower Section 2. Relationships
  • 14.
    Class Diagram Elements 1. Vitalcomponents of a Class 1.1. Upper Section 1.2. Middle Section 1.3. Lower Section: methods or operations. It demonstrates how a class interacts with data. Each attribute has a type. Each operation has a signature 2. Relationships
  • 15.
    Class Diagram Elements 1. Vitalcomponents of a Class 1.1. Upper Section 1.2. Middle Section 1.3. Lower Section: Signatures (Data types) 2. Relationships
  • 16.
    Class Diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association (Kết hợp) 2.2. Dependency (Phụ thuộc) 2.3. Generalization/ Inheritance (Kế thừa) 2.4. Realization (Hiện thực hóa)
  • 17.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association (kết hợp) ü A structural link between two peer classes ü There is an association between Class1 and Class2 ü A solid line connecting two classes ü It can form several types of associations: one-to-one, one-to-many, many-to-one and many-to-many ü Multiplicity: A multiplicity is a factor associated with an attribute. It specifies how many instances of attributes are created when a class is initialized. If a multiplicity is not specified, by default one is considered as a default multiplicity
  • 18.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association - Examples
  • 19.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association 4 types of association ü unidirectional/ directed (một chiều) ü bi-directional (hai chiều) ü reflexive (phản xạ) ü aggregation (tập hợp) The mostly used associations are unidirectional and bi-directional.
  • 20.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association ← Unidirectional/ directed ü The directed association is related to the direction of flow within association classes ü In a directed association, the flow is directed. The association from one class to another class flows in a single direction only It is denoted using a solid line with an arrowhead
  • 21.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association ← Bi-directional Two objects might store each other in fields
  • 22.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association ← Reflexive ü In a reflexive association, the instances of the same class can be related to each other ü Reflexive association states that a link or a connection can be present within the objects of the same class ü This occurs when a class may have multiple functions or responsibilities
  • 23.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association ← Aggregation (has-a relationship) ü It represents a "part of" relationship ü Class2 is part of Class1 (collection) ü Many instances (denoted by the *) of Class2 can be associated with Class1 ü Objects of Class1 and Class2 have separate lifetimes
  • 24.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.1. Association ← Aggregation ← Composition (part-of relationship) ü A special type of aggregation where parts are destroyed when the whole is destroyed ü Objects of Class2 live and die with Class1 ü Class2 cannot stand by itself
  • 25.
    Class diagram Elements Aggregation relationshipComposition relationship Dependency In an aggregation relationship, a child can exist independent of a parent In a composition relationship, the child cannot exist independent of the parent Type of Relationship Has-a relationship Part-of relationship Type of Association a weak association strong association Examples A doctor has patients when the doctor gets transfer to another hospital, the patients do not accompany to a new workplace A hospital and its wards. If the hospital is destroyed, the wards also get destroyed 1. Vital components of a Class 2. Relationships 2.1. Association ← Aggregation ← Composition
  • 26.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.2. Dependency ü A special type of association ü Exists between two classes if changes to the definition of one may cause changes to the other (but not the other way around) ü Class1 depends on Class2
  • 27.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.3. Generalization/ Inheritance ü Represents an "is-a” / parent-child relationship ü SubClass1 and SubClass2 are specializations of SuperClass (Abstract Class)
  • 28.
    Class diagram Elements 1. Vitalcomponents of a Class 2. Relationships 2.4. Realization (hiện thực hóa) ü One entity denotes some responsibility which is not implemented by itself and the other entity that implements them. ü This relationship is mostly found in the case of interfaces
  • 29.
    1. Vital componentsof a Class 2. Relationships ü Exercise: Develop mind-map diagram for Class Diagram Relationships Class Diagram Elements
  • 30.
    1. Vital componentsof a Class 2. Relationships Class Diagram Elements
  • 31.
    vs Use CaseDiagram Class Diagram
  • 32.
    Class diagram Use casediagram vs Class diagram
  • 33.
    Use Case Diagram Notations/ Symbols Relationships ü Association: A line between actors and use cases represents which actors are associated with which use cases ü Include: defines that a use case contains the behavior defined in another use case ü Extend: specifies that the behavior of a use case may be extended by the behavior of another use case ü Generalization: represent inheritance relationship between model elements of same type Association Include Extend Generalization
  • 34.
    Use case diagramClass diagram What kind of? Behavioral diagram Structural diagram What describe? Describes functional requirements Describes the structure of a system How to describe? By mentioning that who will perform what function and which kind of association is exist between functions (use cases) By showing the system’s classes, their attributes, functions and the relationships among objects Supported relationships Association, includes, extends, Generalization Association, Dependency, Generalization, Realization Specify Specify the behavior (what system will do?) Specify the internal structure that will help to complete a functionality mentioned in use cases Representation Use cases can be denoted both by textual (UC description) and visual representation (UC diagram) Only denoted by visual representation What summarizes? Only summarizes relationships between use cases, actors, and system Only summarizes relationships between classes of the system Order of execution Does not show the order in which steps are performed to achieve the goals of each use case Does not show the order in which classes executes
  • 35.
  • 36.
    Class Diagram Example 1– Order System
  • 37.
  • 38.
    Class Diagram Example 3 üGeneralization/ Inheritance ü Realization
  • 39.
  • 40.
  • 41.
  • 42.
    Class Diagram Session Review Reviewcontent 1. What is multiplicity in Class diagram? 2. What is Reflexive association in Class diagram? 3. Aggregation relationship vs Composition relationship? 4. Use Case diagram vs Class diagram? 5. Mind-map diagram: develop a mind-map diagram to present all of Class diagram relationships types