๐Ÿ“ Lecture 05 ยท 70 slides ยท The biggest exam topic (Question 2)

UML Class Diagrams I

ุฃู‡ู… ุฑุณู… ููŠ UMLุŒ ูˆุจูŠูŠุฌูŠ ุบุงู„ุจุงู‹ ููŠ Question 2 ู…ู† ุงู„ุงู…ุชุญุงู† (20 ู…ุงุฑูƒ). ู‡ู†ุบุทูŠ ุฃู†ูˆุงุน ุงู„ุนู„ุงู‚ุงุช: Associations, Multiplicity, Generalization, Aggregation, CompositionุŒ ูˆูƒู„ relations ู…ู…ูƒู† ุชูŠุฌูŠ.

8
Chapters
13
Slide Exercises
5
Relation Types
8
Exam Qs
01

What is UML?

UML ุงุฎุชุตุงุฑ ู„ู€ Unified Modeling Language: ู„ุบุฉ ุฑุณูˆู…ูŠุฉ ู…ุนูŠุงุฑูŠุฉ ู„ู†ู…ุฐุฌุฉ ุจุฑู…ุฌูŠุงุช OO.

๐Ÿ“– ุงู„ุฎู„ููŠุฉ
  • ุทูˆู‘ุฑู‡ุง Rumbaugh, Booch, JacobsonุŒ ูˆุงู„ู…ุนุฑูˆููŠู† ุจุงุณู… Three Amigos.
  • ุฃุณู‘ุณูˆุง Rational Software CorporationุŒ ูˆุงู„ู„ูŠ ุงุดุชุฑุชู‡ุง IBM ุจุนุฏ ูƒุฏู‡.
  • ููŠ 1997ุŒ ุจุฏุฃุช ู…ู†ุธู…ุฉ Object Management Group (OMG) ุนู…ู„ูŠุฉ standardization.
๐Ÿ“š ุฃู†ูˆุงุน UML Diagrams

๐Ÿ—๏ธ Static View

  • Class diagrams โ€” describe classes & relationships.
  • Component diagrams
  • Deployment diagrams

๐ŸŽฌ Dynamic View

  • Sequence diagrams โ€” object interactions.
  • Communication diagrams
  • State diagrams (internal behavior)
  • Activity diagrams
๐Ÿ’ก ููŠ ุงู„ู…ุญุงุถุฑุฉ ุฏูŠ ู‡ู†ุฑูƒุฒ ุนู„ู‰ Class Diagrams (static structure). Sequence diagrams ูˆ State diagrams ููŠ ู…ุญุงุถุฑุงุช ุฌุงูŠุฉ.
02

Class Diagram Essentials

5 ุฑู…ูˆุฒ ุฃุณุงุณูŠุฉ: Classes ยท Attributes ยท Operations ยท Associations ยท Generalizations.

๐Ÿ“ฆ Class Box โ€” 3 ุฃู‚ุณุงู…
Rectangle
โ†’
Rectangle
height
width
getArea()
resize()
โ†’
Rectangle
โˆ’ height: int
โˆ’ width: int
+ getArea(): int
+ resize(int, int)
  • ุงุณู… ุงู„ู€ class ูู‚ุท (ู…ุณุชูˆู‰ ู…ุฌุฑุฏ).
  • Attributes + Operations (ู…ุณุชูˆู‰ ู…ุชูˆุณุท).
  • Full signature ู…ุน ุงู„ู€ visibility (+ = public, โˆ’ = private) ูˆุงู„ู€ types (ู…ููŠุฏ ู„ู„ู€ implementation).
โœ๏ธ Operation Signature
operationName(parameterName: parameterType, ...): returnType

ู…ุซุงู„: resize(width: int, height: int): void

๐Ÿ”„ Class Diagrams ุฎู„ุงู„ lifecycle

ุฑุณูˆู…ุงุช Class diagram ุจุชุฎุชู„ู ุญุณุจ ุงู„ู…ุฑุญู„ุฉ:

  • Domain Analysis โ€” ู…ุณุชูˆู‰ ุนุงู„ูŠุŒ ูˆู…ูุงู‡ูŠู… business (ู…ู† ุบูŠุฑ methodsุŒ ุบุงู„ุจุงู‹ attributes ุจุณ).
  • Requirements Specification โ€” ุจุชุถูŠู ุงู„ู€ operations ุงู„ู„ูŠ ุธู‡ุฑุช ู…ู† ุงู„ู€ use cases.
  • Implementation โ€” ุดุจู‡ ุฎุฑูŠุทุฉ ู…ุจุงุดุฑุฉ ู„ู„ูƒูˆุฏุŒ ูˆููŠู‡ุง visibility ูˆ types ูˆ library classes.

ุจุชุชุทูˆุฑ ุจุดูƒู„ iterative ู…ุน ุงู„ูˆู‚ุช.

03

Associations & Multiplicity

ุงู„ู€ Associations ุจุชุฑุจุท ุงู„ู€ classes ุจุจุนุถู‡ุง. ูˆุงู„ู€ Multiplicity ุจุชูˆุถุญ ูƒุงู… instance ู…ุณู…ูˆุญ ู…ู† ูƒู„ side.

๐Ÿ”ข Multiplicity Reference
1exactly one
*zero or many
0..1zero or one (optional)
1..*at least one
3..8between 3 and 8 inclusive
0, 3..8either zero, or between 3 and 8
๐Ÿ”— Many-to-One Example
Employee
* โ”โ”โ” worksFor โ”โ”โ” 1
Company
  • A company has many employees (*).
  • An employee works only for 1 company.
  • A company can have zero employees (e.g. shell company).
  • An employee must work for a company (multiplicity 1, not 0..1).
๐Ÿ”— Many-to-Many Example
Assistant
* โ”โ”โ” supervisor โ”โ”โ” 1..*
Manager
  • An assistant can work for many managers (1..*).
  • A manager can have many (or 0) assistants.
  • Question: is it possible for assistant to have zero managers temporarily?
โš ๏ธ Avoid Unnecessary 1-to-1

โŒ Avoid this

Person
name
1 โ”โ” 1
PersonInfo
address

โœ… Do this

Person
name
email
birthdate
address

ู„ูˆ ุนู„ุงู‚ุฉ ุงู„ู€ 1:1 ุฅู„ุฒุงู…ูŠุฉ ุฏุงูŠู…ุงู‹ุŒ ุฎู„ู‘ูŠู‡ุง attribute ุฏุงุฎู„ ู†ูุณ ุงู„ู€ class ุจุฏู„ ู…ุง ุชุนู…ู„ relation ู…ู†ูุตู„ุฉ.

04

Special Associations

Association classes ยท Reflexive associations ยท Directionality.

๐Ÿ“Ž Association Class

ุฃุญูŠุงู†ุงู‹ ุจูŠูƒูˆู† ููŠู‡ attribute ูŠุฎุต ุงู„ุนู„ุงู‚ุฉ ุจูŠู† 2 classesุŒ ู…ุด ุงู„ู€ classes ู†ูุณู‡ุง.

ู…ุซุงู„: grade ุงู„ุทุงู„ุจ ููŠ course. ุงู„ุฏุฑุฌุฉ ู…ุด property ู„ู„ู€ student ู„ุฃู†ู‡ ุนู†ุฏู‡ ุฏุฑุฌุงุช ูƒุชูŠุฑุŒ ูˆู…ุด property ู„ู„ู€ course ู„ุฃู†ู‡ ููŠู‡ ุทู„ุงุจ ูƒุชูŠุฑ. ู‡ูŠ property ู„ู„ู€ registration.

Student
* โ”โ” *
CourseSection
โ†“ Association class
Registration
grade

ุฏู‡ ู…ูƒุงูุฆ ู„ู€:

Student
1 โ” *
Registration
grade
* โ” 1
CourseSection
๐Ÿ”„ Reflexive Associations

ุงู„ู€ Class ู…ู…ูƒู† ุชุฑุจุท ู†ูุณู‡ุง ุจู†ูุณู‡ุง. ู…ุซุงู„: Course:

Course
โ†ฉ successor (* to *)
โ†ฉ prerequisite (* to *)
โ†ฉ isMutuallyExclusiveWith (* to *)

ู…ุซุงู„ ุนู„ู‰ ุงู„ู€ instances:

CS251 (prerequisite) โ†’ CS352 (successor)

โžก๏ธ Directionality

ุงู„ู€ Associations ุงูุชุฑุงุถูŠุงู‹ bi-directional. ุชู‚ุฏุฑ ุชุญุฏุฏ ุงู„ู€ direction ุจุณู‡ู… ุนู†ุฏ ุงู„ู€ end.

Day
1 โ”โ”โ†’ *
Note

ูŠุนู†ูŠ Day ุจูŠุนุฑู ุงู„ู€ Notes ุงู„ู„ูŠ ุชุฎุตู‡ุŒ ู„ูƒู† ุงู„ู€ Note ู…ุง ูŠุนุฑูุด ุงู„ู€ Day ุจุชุงุนู‡.

05

Generalization (Inheritance)

ุงู„ู€ Superclass ู‡ูˆ ุงู„ุฃุจุŒ ูˆุงู„ู€ Subclasses ุชุฑุซ ู…ู†ู‡. ุณู‡ู… ุงู„ู€ open triangle ุจูŠุดูŠุฑ ู†ุงุญูŠุฉ ุงู„ู€ superclass.

๐ŸŒณ Basic Generalization
Animal
โ–ณ
discriminator: habitat
AquaticAnimal
LandAnimal

ุงู„ู€ Subclasses ุจุชุฑุซ ุงู„ู€ attributes ูˆุงู„ู€ operations ู…ู† ุงู„ู€ superclass.

Generalization Set = ุชุฌู…ูŠุน ู„ู„ู€ subclasses ุจู†ุงุกู‹ ุนู„ู‰ discriminator (ู…ุนูŠุงุฑ ุงู„ุชุฎุตุต).

๐Ÿ”€ Multiple Discriminators

ุงู„ู€ Animals ู…ู…ูƒู† ูŠุชู‚ุณู…ูˆุง ุจุฃูƒุชุฑ ู…ู† ู…ุนูŠุงุฑ: habitat (aquatic/land) ูˆ typeOfFood (carnivore/herbivore). ุนู†ุฏู†ุง ุญู„ูŠู†:

Option 1: Higher-level Generalization

Animal โ†’ AquaticAnimal โ†’ AquaticCarnivore + AquaticHerbivoreุŒ ูˆู†ูุณ ุงู„ููƒุฑุฉ ู…ุน Land.

โš ๏ธ ุจูŠุนู…ู„ classes ูƒุชูŠุฑุฉ (4 leaves).

Option 2: Multiple Inheritance

ุงู„ู€ AquaticCarnivore ุชุนู…ู„ inherits from both ู…ู† AquaticAnimal ูˆ Carnivore.

โš ๏ธ ู…ุด ูƒู„ ุงู„ู„ุบุงุช ุจุชุฏุนู…ู‡ุง (Java ู„ุงุŒ C++ ูŠุฏุนู…ู‡ุง).

๐Ÿšซ Avoid Instances Changing Class

ู„ูˆ ุงู„ู€ instance ู…ู…ูƒู† ูŠุชุบูŠุฑ ู…ู† type ู„ู€ type ุชุงู†ูŠุŒ ู…ุง ุชุณุชุฎุฏู…ุด inheritance. ุงุณุชุฎุฏู… attribute ุจุฏุงู„ู‡ุง.

โŒ Avoid

Student
โ–ณ
FullTimeStudent
PartTimeStudent

ุทุงู„ุจ ู…ู…ูƒู† ูŠุชุญูˆู„ ู…ู† part-time ู„ู€ full-time โ†’ ู…ุดูƒู„ุฉ

โœ… Do

Student
name
attendance: enum

ุงู„ู€ attendance ูƒู€ attribute ูŠุชุบูŠุฑ ุจุณู‡ูˆู„ุฉ

06

Aggregation vs Composition โญ

ุฏูŠ ุนู„ุงู‚ุงุช part-whole. ุงู„ูุฑู‚ ุงู„ุฃุณุงุณูŠ ุจูŠู†ู‡ู…: ู‡ู„ ุงู„ู€ part ุชู‚ุฏุฑ ุชุนูŠุด ุจุฏูˆู† ุงู„ู€ wholeุŸ

๐Ÿ’  Aggregation (Open Diamond โ—‡)

ุงู„ู€ Aggregation ุนู„ุงู‚ุฉ part-wholeุŒ ู„ูƒู† ุงู„ู€ parts ุชู‚ุฏุฑ ุชุนูŠุด ู…ุณุชู‚ู„ุฉ ุนู† ุงู„ู€ whole.

Vehicle
1 โ—‡โ”โ” *
VehiclePart
  • ุงู„ู€ Vehicle parts ู…ู…ูƒู† ุชูŠุฌูŠ ู…ู† cars ู…ุฎุชู„ูุฉ ูˆุชูุณุชุฎุฏู… ุชุงู†ูŠ (reused).
  • ู„ูˆ ุงู„ู€ Vehicle ุงุชุฏู…ุฑุŒ ุงู„ู€ parts ู…ู…ูƒู† ุชุณุชุฎุฏู… ููŠ Vehicle ุชุงู†ูŠ.

โœ… When to use Aggregation:

  • ุชู‚ุฏุฑ ุชู‚ูˆู„ ุฅู† ุงู„ู€ parts are part of ุงู„ู€ aggregateุŒ ุฃูˆ ุฅู† ุงู„ู€ aggregate is composed of ุงู„ู€ parts.
  • ู„ู…ุง ุดูŠุก ูŠู…ู„ูƒ ุฃูˆ ูŠุชุญูƒู… ููŠ ุงู„ู€ aggregateุŒ ูู‡ูˆ ุบุงู„ุจุงู‹ ูŠู…ู„ูƒ ุฃูˆ ูŠุชุญูƒู… ููŠ ุงู„ู€ parts ูƒู…ุงู†.
โ™ฆ๏ธ Composition (Filled Diamond โ—†)

ุงู„ู€ Composition ู‡ูŠ strong aggregation. ู„ูˆ ุงู„ู€ aggregate ุงุชุฏู…ุฑุŒ ุงู„ู€ parts ุงุชุฏู…ุฑุช ู…ุนุงู‡. ุงู„ู€ part ู…ุง ุชู‚ุฏุฑุด ุชุนูŠุด ุจุฏูˆู† ุงู„ู€ whole.

Building
1 โ—†โ”โ” *
Room
  • ุงู„ู€ Rooms ู…ุง ุชู‚ุฏุฑุด ุชูˆุฌุฏ ุจุฏูˆู† ุงู„ู€ Building.
  • ู„ูˆ ุงู„ู€ Building ุงุชู‡ุฏุŒ ุงู„ู€ Rooms ุฑุงุญุช ู…ุนุงู‡.
โš–๏ธ Aggregation vs Composition โ€” ุฌุฏูˆู„ ู…ู‚ุงุฑู†ุฉ
AspectAggregation โ—‡Composition โ—†
SymbolOpen (hollow) diamondFilled (solid) diamond
Part lifecycleIndependentTied to whole
If whole destroyedParts surviveParts destroyed
Part can be reusedYes (in other wholes)No (one whole only)
ExampleVehicle - VehiclePart ยท Country - RegionBuilding - Room ยท Book - Chapter (if Chapter can't exist alone)
๐Ÿ“ Address: 2 Alternatives

As Composition

Employee
โ—†โ”
Address
street
city
country

As Attribute

Employee
address: Address

ุฃุจุณุท ู„ูˆ ุงู„ู€ Address ู…ุง ุนู†ุฏู‡ุงุด complexity ูƒุจูŠุฑุฉ

๐Ÿ”„ Propagation

Mechanism: ุงู„ู€ operation ููŠ ุงู„ู€ aggregate ุจุชุชู†ูุฐ ุจุฃู† ุงู„ู€ aggregate ูŠุนู…ู„ perform ู„ู†ูุณ ุงู„ู€ operation ุนู„ู‰ ุงู„ู€ parts ุจุชุงุนุชู‡.

ู…ุซุงู„: Polygon.draw() ุจุชุชู†ูุฐ ุจุฃู† ุงู„ู€ Polygon ูŠุทู„ุจ ู…ู† ูƒู„ LineSegment ุฅู†ู‡ุง ุชุนู…ู„ draw().

ูˆุนูƒุณูŠุงู‹: ุงู„ู€ properties ุงู„ุฎุงุตุฉ ุจุงู„ู€ parts ู…ู…ูƒู† ุชู†ุชุดุฑ ู„ู„ู€ aggregate.

07

Object Diagrams

ุงู„ู€ Object ู‡ูˆ instance ู…ู† class. ูˆุงู„ู€ Link ู‡ูˆ instance ู…ู† association.

๐Ÿงฌ Class vs Object Diagram
Class DiagramObject Diagram
ShowsTypes and relationshipsSpecific instances at runtime
NamingEmployeePat: Employee (instance:class)
Underline?NoYes (object name underlined)
Generalizationsโœ“ ShownโŒ Don't appear in object diagrams
AssociationsClass-levelBecome links between objects
๐Ÿ“Š Example Object Diagram
Pat: Employee
Wayne: Employee
โ”โ”โ†’
OOCorp: Company
  • Pat ูˆ Wayne instances ู…ู† Employee.
  • OOCorp instance ู…ู† Company.
  • ุงู„ู€ Links ุจุชุนูƒุณ ุนู„ุงู‚ุฉ worksFor ุงู„ู„ูŠ ู…ูˆุฌูˆุฏุฉ ููŠ ุงู„ู€ class diagram.
08

Full Example: Problem Report Tool

ุฃุฏุงุฉ CASE tool ู„ุชุฎุฒูŠู† ูˆุชุชุจุน ุงู„ู€ problem reports. ุฏู‡ ู…ุซุงู„ ุดุงู…ู„ ู„ูƒู„ ุงู„ู€ relations.

๐Ÿ“‹ The Requirements
  1. Employees are either developers or managers (generalization).
  2. Each developer has a manager (1 manager per developer).
  3. Project assigned a manager + a number of developers.
  4. Developer/manager may be assigned to multiple projects or to none (0..*).
  5. Project consists of artifacts (domain analysis, SRS, code, test cases...) โ€” aggregation.
  6. Problem reports can be made on any artifact.
  7. Each report contains: problem description and status.
  8. Each problem can be assigned to someone by the project manager.
  9. Report has history entries (action/update logs) โ€” aggregation.
  10. A special kind of reports: Code/Bug Reports with extra info (location, circumstances, failed tests) โ€” generalization.
๐Ÿ“ The Class Diagram (high-level)
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ Employee โ”‚ (name: String)
        โ””โ”€โ”€โ”€โ”€โ–ณโ”€โ”€โ”€โ”€โ”€โ”˜
             โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”
   โ”‚Managerโ”‚      โ”‚Developer โ”‚
   โ””โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚              โ”‚   * managed by 1
       โ”‚ 1            โ””โ”€โ”€โ”€โ†‘โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚  responsibleFor  โ”‚            Manager
       โ”‚  0..*            โ”‚ * assigned 0..*
   โ”Œโ”€โ”€โ”€โ†“โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ†“โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚ Problem Rep. โ”‚  โ”‚  Project    โ”‚ (name)
   โ”‚ + status     โ”‚  โ””โ”€โ”€โ—‡โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ† aggregation
   โ”‚ + desc       โ”‚     โ”‚ 1
   โ”‚              โ”‚     โ”‚ 0..*
   โ””โ”€โ”€โ–ณโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”Œโ”€โ”€โ†“โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚              โ”‚  Artifact  โ”‚ โ”€โ”€ associated 0..n โ”€โ”€โ†’ Problem Report (label: "About")
   โ”Œโ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚ +name      โ”‚
   โ”‚ CodeBugRep. โ”‚   โ”‚ +status    โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

   Problem Report โ—‡โ”€โ”€โ”€โ”€ 0..n โ”€โ”€โ”€โ†’ History Entry (when: Date, whatDone: String)
                  "History Log"
      
๐Ÿ’ก ุงู„ู€ relations ุงู„ู€ 5 ุงู„ู…ูˆุฌูˆุฏุฉ ููŠ ุงู„ู…ุซุงู„
1. Generalization โ€” ุงู„ู€ Manager ูˆุงู„ู€ Developer ุจูŠุฑุซูˆุง ู…ู† EmployeeุŒ ูˆุงู„ู€ CodeBugReport ุจูŠุฑุซ ู…ู† Problem Report.
2. Associations โ€” ู…ุซู„ Developer managedBy ManagerุŒ ูˆ Employee assignedTo ProjectุŒ ูˆ Artifact About Problem Report.
3. Aggregation โ€” Project โ—‡ ArtifactุŒ ูˆ Problem Report โ—‡ History Entry.
4. Multiplicities โ€” ุฒูŠ: 1ุŒ *ุŒ 0..*ุŒ 1..*ุŒ 0..n.
5. Labels โ€” Managed ByุŒ Assigned ToุŒ Responsible ForุŒ AboutุŒ History Log.
๐Ÿ“

13 Slide Exercises โ€” Practice

ุฏูŠ ุงู„ุชู…ุงุฑูŠู† ุงู„ู„ูŠ ุฏ. ุงู„ุฑู…ู„ูŠ ุญุงุทุทู‡ุง ููŠ ุงู„ุณู„ุงูŠุฏุฒ. ู„ุงุฒู… ุชุญู„ู‡ู… ูƒู„ู‡ู…ุŒ ู„ุฃู† ูƒู„ ุชู…ุฑูŠู† ุจูŠุฎุชุจุฑ concept ู…ุฎุชู„ู.

Ex 1 Shape Class & Square โ€” What relation?

Shape โ† Triangle, Square (open-triangle generalization arrow).

ุงู„ุฅุฌุงุจุฉ: Generalization (Inheritance) โ€” ุงู„ู€ Square ูˆุงู„ู€ Triangle ุจูŠุฑุซูˆุง ู…ู† Shape.
Ex 2-4 Account โ†” Client (Bank example)

Class diagram: Account [1] โ”โ” [0..n] Client +owner

  • Ex 2: What relation? โ†’ Simple Association with multiplicity.
  • Ex 3: What's true?
    • A client can own multiple accounts? True (0..n on client side from account's perspective is the inverse โ€” actually account is 1 to many clients here meaning 1 account has 0..n owners; the question is open to interpretation, but conventional answer follows the diagram).
    • An account can be used to access its client? True (bidirectional by default).
    • A client can be used to access its account? True.
  • Ex 4: What's true?
    • An account can have multiple owners? True (0..n on client side).
    • No account can exist without a client? Depends โ€” if 0..n includes 0, account can exist without clients.
    • A client must have an account? True if multiplicity is 1 on account side.
Ex 5 Building โ—†โ” Room (Composition)
  • It shows a composition relation? โœ“ True (filled diamond).
  • A building may have no rooms? โœ— False if multiplicity is 1..* (depends on diagram).
  • A room may exist without a building? โœ— False โ€” composition means parts die with whole.
Ex 6 Employee โ” Address (Plain Association, 1:1)
  • It shows a composition relation? โœ— False (no diamond shown).
  • An employee may have no address? โœ— False if multiplicity is 1 (mandatory).
  • An address may exist without an employee? โœ“ True if it's a plain association.
Ex 7 Employee โ†’ Manager/Developer, Project assignment

Part 1: Manager [1] managedBy [0..*] Developer (subclasses of Employee).

  • Employee can be Manager and Developer at the same time? โœ— False (in single inheritance, subclasses are disjoint).
  • Manager may have no developers assigned? โœ“ True (0..* includes 0).
  • Developer has one and only one manager? โœ“ True (multiplicity 1).

Part 2: Employee [1..*] assignedTo [0..*] Project.

  • Project has at least one employee? โœ“ True (1..*).
  • Every developer must be assigned to a project? โœ— False (0..* on project side means dev can have zero projects).
  • Manager may have no projects? โœ“ True.
Ex 8 Course (Reflexive: successor, prerequisite, mutuallyExclusive)
  • A course may have any number of prerequisites or none? โœ“ True (multiplicity *).
  • A course may not be taken with any number of courses? โœ— False (it CAN be).
  • A course may have an association relation with another course? โœ“ True โ€” reflexive association.
Ex 9 Book / Volume / Chapter (Aggregation vs Composition)

ุงู„ู€ scenario: ุงู„ู€ Book ููŠู‡ volumesุŒ ูˆุงู„ู€ volumes ููŠู‡ุง chapters. ูˆุงู„ู€ Chapters can be sold alone ุญุชู‰ ู„ูˆ ุงู„ู€ book ู…ุด ู…ุชุงุญ.

  • Fig 1 (Composition 1:* / 1:*) โ€” โŒ ุบู„ุท ู„ุฃู† ุงู„ู€ composition ู…ุนู†ุงู‡ุง ุฅู† ุงู„ู€ parts ู…ุง ุชู‚ุฏุฑุด ุชูˆุฌุฏ ู„ูˆุญุฏู‡ุง.
  • Fig 2 (Composition 1..* / 1..*) โ€” โŒ ุบู„ุท ู„ู†ูุณ ุงู„ุณุจุจ.
  • Fig 3 (Aggregation 1..* / 1..*) โ€” โœ… ุตุญ ู„ุฃู† ุงู„ู€ parts ู…ู…ูƒู† ุชูˆุฌุฏ ู…ุณุชู‚ู„ุฉ.
ุงู„ุฅุฌุงุจุฉ: Figure 3 โ€” ุงู„ู€ Aggregation ุตุญ ู„ุฃู† ุงู„ู€ chapters ูˆุงู„ู€ volumes ู…ู…ูƒู† ูŠุชุจุงุนูˆุง ุฃูˆ ูŠูˆุฌุฏูˆุง ุจุดูƒู„ ู…ุณุชู‚ู„ ุนู† ุงู„ู€ book.
Ex 10 Event/Schedule/Display system โ€” multiple relations

ุงู‚ุฑุฃ ุงู„ู€ diagram ุจุฏู‚ุฉ:

  • CurrentEventDisplay can display both SpecialEvents and RegularEvents? โœ“ True โ€” both inherit from Event, and Event is displayed.
  • CurrentEventDisplay can display multiple events? โœ“ True if multiplicity supports it.
  • Schedule can contain an Announcement? โœ— False โ€” Schedule aggregates Event, not Announcement.
  • UserInterface can display either events or announcements but not both? โœ— False โ€” different subclasses of UI handle different types.
  • SpecialEvent should have at least one Date? Depends on multiplicity on Date association.
  • An Announcement can have a Date? โœ— False โ€” Announcement doesn't connect to Date in the diagram.
Ex 11 Writer โ†” Document (Object diagram match)

Class diagram: Writer [*] โ” [1] Document

ุงู„ู€ multiplicity ุนู„ู‰ ู†ุงุญูŠุฉ ุงู„ู€ Writer ู‡ูŠ *ุŒ ูŠุนู†ูŠ document ูˆุงุญุฏ ู…ู…ูƒู† ูŠูƒูˆู† ู„ู‡ writers ูƒุชูŠุฑ ุฃูˆ ูˆู„ุง ูˆุงุญุฏ.

  • Figure I: 1 Writer โ†” 1 Document โœ“ valid
  • Figure II: 2 Writers โ†” 1 Document โœ“ valid (matches * to 1)
  • Figure III: 1 Writer โ†” 1 Document โœ“ valid
ุงู„ุฅุฌุงุจุฉ: Figure II ู‡ูŠ ุฃูุถู„ ูˆุงุญุฏุฉ ู„ุฃู†ู‡ุง ุจุชูˆุถุญ * multiplicity. ูƒู„ ุงู„ุฑุณูˆู…ุงุช validุŒ ู„ูƒู† II ุจุชุนุฑุถ ุงู„ู€ multiplicity ุจุดูƒู„ ุฃูˆุถุญ.
Ex 12 Person and Body parts (Head, Hand, Leg)
  • Fig 1 (Aggregation โ—‡) โ€” โŒ wrong, body parts can't survive without person.
  • Fig 2 (Composition โ—†) โ€” โœ… correct โ€” body parts die with person.
  • Fig 3 (Generalization) โ€” โŒ very wrong, Head IS-NOT-A Person.
Ex 13 Car & Engine, Wheel (parts can be sold separately)

"Knowing that parts can be sold separately" โ†’ Aggregation.

  • Fig 1 (Aggregation โ—‡) โ€” โœ… correct.
  • Fig 2 (Composition โ—†) โ€” โŒ wrong, parts die with whole.
  • Fig 3 (Generalization) โ€” โŒ very wrong, Engine IS-NOT-A Car.
๐ŸŽฏ

Exam Question Bank โ€” Lecture 5

4 ุฃุณุฆู„ุฉ ุญู‚ูŠู‚ูŠุฉ ู…ู† ุงู…ุชุญุงู†ุงุช ุงู„ู€ MCQ ุนู„ู‰ Class Diagrams. ุงู„ู€ Question 2 ููŠ ูƒู„ ุงู…ุชุญุงู† (UML Modeling, 20 marks) ุจูŠุนุชู…ุฏ ุนู„ู‰ ุงู„ู€ concepts ุฏูŠ.

Question 1 Fall 2021โ€“22 ยท Q1.07 (also Winter 2021)
Given the class diagram: Advertiser [1] โ”โ” [*] Account. Which code properly implements it?

Code (a): Advertiser has Set accounts, addAccount() adds & sets owner with null check. Account has setOwner() that calls addAccount if newOwner != null.
Code (b): Advertiser has single Account account field. (Wrong โ€” should be Set.)
Code (c): Advertiser has Set; Account has Set owners (multiple owners!). (Wrong โ€” should be single owner.)
A Code (a)
B Code (b)
C Code (c)
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: A โ€” Code (a) ุงู„ู€ multiplicity 1 to * ู…ุนู†ุงู‡ุง: Advertiser ูˆุงุญุฏ โ†” Accounts ูƒุชูŠุฑ. ุงู„ูƒูˆุฏ ุงู„ุตุญ:
  • ุงู„ู€ Advertiser ููŠู‡ Set<Account> accounts (collection).
  • ุงู„ู€ Account ููŠู‡ Advertiser owner (single referenceุŒ ู…ุด Set).
  • addAccount ูˆ setOwner ุจูŠุดุชุบู„ูˆุง ู…ุนุงู‹ ู„ู„ุญูุงุธ ุนู„ู‰ ุงู„ู€ bidirectional integrity.
B โ€” ุงุณุชุฎุฏู… Account account (single) ููŠ Advertiser ุจุฏู„ Set. ุบู„ุท ู„ุฃู† ูƒู„ Advertiser ู„ุงุฒู… ูŠู‚ุฏุฑ ูŠู…ุชู„ูƒ multiple accounts. ยท C โ€” ุฎู„ู‰ ุงู„ู€ Account ููŠู‡ Set ู…ู† Advertisers (multiple owners). ุฏู‡ many-to-manyุŒ ู…ุด 1-to-*.
Question 2 Fall 2021โ€“22 ยท Q1.08 (also Winter 2021)
Which class diagram represents this code?

class Order { OrderStatus status; ArrayList<OrderDetail> details; ... }
class OrderDetail { Product product; }
class Product { String code, name; float unitPrice; }
enum OrderStatus { OPEN, CLOSED; }
A (a) Order โ—†[1:*] OrderDetail โ†’ Product; Order โ†’ OrderStatus
B (b) Order [1:1] OrderDetail
C (c) Order โ†’ OrderDetail โ†’ Product (without composition)
D (d) Order [*:*] OrderDetail (many-to-many)
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: A โ€” Composition with 1:*
  • ArrayList<OrderDetail> = Order has many OrderDetails (multiplicity *).
  • Order owns OrderDetails completely (composition โ—†).
  • OrderDetail has single Product reference (1:1 association).
  • Order has OrderStatus enum.
B โ€” 1:1 ุบู„ุทุŒ ู„ุฃู† ุงู„ู€ ArrayList ู…ุนู†ุงู‡ุง ุนู†ุงุตุฑ ู…ุชุนุฏุฏุฉ. ยท C โ€” ุงู„ู€ Order ูŠู…ู„ูƒ OrderDetails ูƒุฌุฒุก ู…ู†ู‡ (composition)ุŒ ู…ุด ู…ุฌุฑุฏ association. ยท D โ€” Many-to-many ุบู„ุทุŒ ู„ุฃู† ุงู„ู€ OrderDetail ุจูŠุฎุต Order ูˆุงุญุฏ ูู‚ุท.
Question 3 Winter 2023 ยท Q1.04
Customers of a car sales shop can buy cars. Customers with a bad credit history should pay an extra down payment. Which diagram represents this?
A <<include>> from Bad Credit Customer's "Buy a car" to "Pay extra down payment"
B <<include>> on Customer side
C <<extend>> on Customer side
D <<extend>> from Bad Credit Customer's "Buy a car" to "Pay extra down payment"
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: D extend = ุญุงู„ุฉ ู…ุดุฑูˆุทุฉ (should pay if bad credit). ุงู„ู€ Bad Credit Customer ู‡ูˆ ุงู„ู€ actor ุงู„ู…ุฎุชุต ุจุงู„ู€ extra paymentุŒ ูุงู„ู€ extend ุจูŠูƒูˆู† ุนู„ู‰ ุงู„ู€ side ุจุชุงุนู‡.
A, B โ€” ุงู„ู€ include ู…ุนู†ุงู‡ุง always. ู‡ู†ุง ุงู„ู€ extra payment ู…ุด ุฏุงูŠู…ุงู‹ุŒ ู‡ูŠ ุจุณ ู„ู€ bad credit. ยท C โ€” ุงู„ู€ extend ุตุญุŒ ู„ูƒู† ุนู„ู‰ ู†ุงุญูŠุฉ ุงู„ู€ Customer ุงู„ุนุงุฏูŠ ุบู„ุท.
Question 4 Winter 2023 ยท Q1.05
Which class diagram is wrong?

(a) Class A with self-association (regular line with arrowhead).
(b) Class A with self-loop (curve from one end to another, plain).
(c) Class A with self-loop showing generalization arrow (open triangle).
(d) Class A with self-loop showing aggregation diamond.
A (a)
B (b)
C (c)
D (d)
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: C โ€” Self-generalization is invalid ุงู„ู€ Class ู…ุง ุชู‚ุฏุฑุด ุชุฑุซ ู…ู† ู†ูุณู‡ุง โ€” ูŠุนู†ูŠ A extends A = infinite loop ู…ู† ู†ุงุญูŠุฉ ุงู„ู…ุนู†ู‰. ุฏู‡ ู…ุณุชุญูŠู„ ููŠ OO design.
A, B โ€” ุงู„ู€ Reflexive associations ุตุญูŠุญุฉุŒ ู…ุซู„ Course prerequisite Course. ยท D โ€” ุงู„ู€ Reflexive aggregation ุตุญูŠุญุฉุŒ ู…ุซู„ Folder contains subfolders.
Question 5 Slide Ex 9 (practice)
A book has volumes, volumes have chapters. Chapters can be sold alone even without the book. Which is the correct relation?
A Composition (filled diamond)
B Aggregation (open diamond)
C Generalization (open triangle)
D Plain association
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: B โ€” Aggregation Parts can be sold separately โ†’ ุงู„ู€ parts ู…ู…ูƒู† ุชูˆุฌุฏ ู…ุณุชู‚ู„ุฉ โ†’ Aggregation (open diamond โ—‡). ู„ูˆ CompositionุŒ ูุงู„ู€ chapter ู…ุง ุชู‚ุฏุฑุด ุชูˆุฌุฏ ุจุฏูˆู† ุงู„ู€ book.
A โ€” ุงู„ู€ Composition ู…ุนู†ุงู‡ุง ุฅู† ุงู„ู€ parts ู…ุง ุชู‚ุฏุฑุด ุชูˆุฌุฏ ู„ูˆุญุฏู‡ุงุŒ ูˆุฏู‡ ุนูƒุณ ุงู„ู…ุทู„ูˆุจ. ยท C โ€” ุงู„ู€ Chapter ู…ุด ู†ูˆุน ู…ู† Book. ยท D โ€” ุงู„ู€ Plain association ู…ุง ุจูŠุนุจุฑุด ุนู† ุนู„ุงู‚ุฉ part-whole.
Question 6 Slide Ex 12 (practice)
What's the right relation between Person and body parts (Head, Hand, Leg)?
A Composition (filled diamond)
B Aggregation (open diamond)
C Generalization
D Plain association
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: A โ€” Composition ุงู„ู€ body parts ู…ุง ุชู‚ุฏุฑุด ุชุนูŠุด ุจุฏูˆู† ุงู„ู€ Person. ู„ู…ุง ุงู„ู€ Person ูŠู…ูˆุชุŒ ุงู„ู€ body parts ู…ุง ุจุชูƒูˆู†ุด entities ู…ุณุชู‚ู„ุฉ.
B โ€” ุงู„ู€ Aggregation ู…ุนู†ุงู‡ุง ุฅู† ุงู„ู€ parts ู…ุณุชู‚ู„ุฉุŒ ูˆุฏู‡ ู…ุด ุงู„ุญุงู„ุฉ ู‡ู†ุง. ยท C โ€” ุงู„ู€ Hand ู…ุด ู†ูˆุน ู…ู† Person. ยท D โ€” ู…ุง ุจูŠุนูƒุณุด ู…ุนู†ู‰ is part of.
Question 7 Practice on multiplicity
Class diagram: Project [0..*] โ”โ” [1..*] Employee. Which is true?
A Every project must have at least one employee, every employee must be on at least one project
B Every employee must be on at least one project; a project can exist with zero employees
C Every project must have at least one employee; an employee can exist with zero projects
D Both can exist independently
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: C ุงู„ู€ multiplicity ุจุชุชู‚ุฑุฃ ุนู„ู‰ ุงู„ู€ opposite end:
  • 1..* ุนู„ู‰ ู†ุงุญูŠุฉ ุงู„ู€ Employee โ†’ ูƒู„ project ู„ุงุฒู… ูŠูƒูˆู† ุนู†ุฏู‡ employee ูˆุงุญุฏ ุฃูˆ ุฃูƒุชุฑ.
  • 0..* ุนู„ู‰ ู†ุงุญูŠุฉ ุงู„ู€ Project โ†’ ูƒู„ employee ู…ู…ูƒู† ูŠูƒูˆู† ู…ุฑุชุจุท ุจู€ 0 ุฃูˆ ุฃูƒุชุฑ ู…ู† ุงู„ู…ุดุงุฑูŠุน.
Question 8 Practice (Winter 2023 style)
Which of the following is NOT a valid UML relationship for a class to have with itself?
A Self-association (e.g. Person knows Person)
B Self-generalization (A extends A)
C Self-aggregation (Folder contains Folders)
D Self-composition (Tree contains Tree)
โœ… ุงู„ุฅุฌุงุจุฉ ุงู„ุตุญ: B โ€” Self-generalization ุงู„ู€ Class ู…ุง ุชู‚ุฏุฑุด ุชุฑุซ ู…ู† ู†ูุณู‡ุงุŒ ู„ุฃู† ู…ุนู†ุงู‡ุง infinite loop ููŠ ุงู„ู€ inheritance. ุงู„ุจุงู‚ูŠ valid:
  • Self-association: Course โ†’ Course (prerequisite).
  • Self-aggregation: Folder โ†’ SubFolder.
  • Self-composition: Tree โ†’ SubTree.
๐Ÿ“‹

Cheat Sheet

ูƒู„ ุงู„ุฑู…ูˆุฒ ูˆุงู„ู…ุตุทู„ุญุงุช ูˆุงู„ู‚ูˆุงุนุฏ ููŠ ุตูุญุฉ ูˆุงุญุฏุฉ.

๐ŸŽจ UML Symbols

Class
Rectangle ุจู€ 3 ุฃู‚ุณุงู… (name / attrs / ops)
+
public
โˆ’
private
#
protected
โ”โ”โ”
Association (plain line)
โ”โ”โ†’
Directed association
โ”โ”โ–ณ
Generalization (open triangle)
โ—‡โ”โ”
Aggregation (open diamond)
โ—†โ”โ”
Composition (filled diamond)

๐Ÿ”ข Multiplicity

1
exactly one
0..1
optional (zero or one)
*
zero or many
1..*
at least one
N..M
between N and M inclusive
0, 3..8
zero OR (between 3 and 8)

๐Ÿ”— Relations Decision Tree

"IS-A"
โ†’ Generalization
"HAS-A" + part dies
โ†’ Composition
"HAS-A" + part survives
โ†’ Aggregation
"USES" / "KNOWS"
โ†’ Plain Association
Attribute of relationship
โ†’ Association Class

๐Ÿ“ UML 3 Amigos

Rumbaugh
OMT
Booch
Booch method
Jacobson
OOSE / Use cases
Founded
Rational Software โ†’ IBM
Standardized
OMG, 1997

โš ๏ธ Common Pitfalls

Avoid
Unnecessary 1:1 โ†’ use attribute
Avoid
Self-generalization (A extends A)
Avoid
Inheritance when instance can change class โ†’ use attribute
Composition only if
Part can't exist without whole

๐ŸŽฏ Reading Multiplicity

Where to read?
On the OPPOSITE end
A [1] โ” [*] B
A has many B's, B has 1 A
A [1..*] โ” [0..*] B
A has 0 or many B's, B has at least 1 A
โšก

Rapid Revision

Flashcards ยท Common Mistakes ยท What the doctor loves to ask.

5 UML Class Diagram symbolsุŸ
tap to flip
Classes ยท Attributes ยท Operations ยท Associations ยท Generalizations
Aggregation vs CompositionุŸ
tap to flip
Open โ—‡ vs Filled โ—†. Composition = part dies with whole.
UML founded byุŸ
tap to flip
Rumbaugh ยท Booch ยท Jacobson (OMG 1997)
Multiplicity 0..1 ู…ุนู†ุงู‡ุงุŸ
tap to flip
Optional โ€” zero or one
Multiplicity 1..* ู…ุนู†ุงู‡ุงุŸ
tap to flip
At least one (mandatory minimum)
ู…ุซุงู„ ุนู„ู‰ Reflexive associationุŸ
tap to flip
Course โ†’ Course (prerequisite/successor)
ู…ุซุงู„ ุนู„ู‰ Association ClassุŸ
tap to flip
Student โ†” CourseSection with Registration (grade)
Building ูˆ Room: ุงูŠู‡ ุงู„ู€ relationุŸ
tap to flip
Composition โ—† (room can't exist without building)
Car ูˆ Engine (engine sold separately): ุงูŠู‡ ุงู„ู€ relationุŸ
tap to flip
Aggregation โ—‡ (parts can exist alone)

๐Ÿšจ Common Mistakes

1. ุฎู„ุท Open ูˆ Filled diamondOpen โ—‡ = Aggregation (independent parts). Filled โ—† = Composition (ุงู„ู€ part ุจุชู†ุชู‡ูŠ ู…ุน ุงู„ู€ whole). ุงูุชูƒุฑู‡ุง: Filled = Final.
2. ู‚ุฑุงุกุฉ Multiplicity ู…ู† ุงู„ุฌุงู†ุจ ุงู„ุบู„ุทุงู„ู€ multiplicity ุนู„ู‰ side A ุจุชู‚ูˆู„ ูƒุงู… instance ู…ู† A ู„ูƒู„ instance ู…ู† B (ุงู„ู€ opposite). ู„ูˆ A [1] โ” [*] B: ู„ูƒู„ B ูˆุงุญุฏุŒ ููŠู‡ A ูˆุงุญุฏ ุจุณ.
3. ุงุณุชุฎุฏุงู… Generalization ู„ู…ุง Composition ู‡ูŠ ุงู„ุตุญุงู„ู€ Hand ู…ุด ู†ูˆุน ู…ู† PersonุŒ ู„ูƒู†ู‡ุง ุฌุฒุก ู…ู† Person. ู„ู…ุง ุชุดูˆู part of โ†’ ููƒุฑ ููŠ Aggregation/CompositionุŒ ู…ุด Generalization.
4. Include vs Extend ููŠ Use Cases(ุฌุง ููŠ Lec 3 ุจุณ ุจูŠุชุณุฃู„ ูƒุชูŠุฑ): Include = ุงุณุชุฎุฏุงู… ุฏุงุฆู…. Extend = ุฃุญูŠุงู†ุงู‹ุŒ ุจุดุฑุท ู…ุนูŠู† (conditional).
5. ู†ุณูŠุงู† ุงู„ู€ Multiplicity ุนู„ู‰ ุงู„ู€ endsุฃูŠ association ู„ุงุฒู… ู„ู‡ุง multiplicity ุนู„ู‰ ุงู„ู€ end (1, *, 0..1, ุฅู„ุฎ). ู„ูˆ ู†ุณูŠุชู‡ุงุŒ ู‡ุชูู‚ุฏ marks ููŠ Question 2.

โญ What Dr. El-Ramly Loves to Ask

๐Ÿ”ฅ ุงู„ุฃุณุฆู„ุฉ ุงู„ู…ุชูƒุฑุฑุฉ ุนู„ู‰ Lec 5
  1. Code โ†” Class Diagram โ€” ุจูŠุฏูŠ Java/C++ code ูˆุนู„ูŠูƒ ุชุฎุชุงุฑ ุงู„ู€ diagram ุงู„ุตุญุŒ ุฃูˆ ุงู„ุนูƒุณ.
  2. Aggregation vs Composition โ€” ุจูŠุฏูŠ scenario ู…ุซู„ book ุฃูˆ car ุฃูˆ buildingุŒ ูˆุนู„ูŠูƒ ุชุญุฏุฏ ุงู„ู€ relation ุงู„ุตุญ.
  3. Which class diagram is wrong? โ€” ุงู„ู€ self-generalization ุฏุงูŠู…ุงู‹ ุบู„ุท.
  4. Multiplicity questions โ€” ุชูุณูŠุฑ diagram ู…ุนุทู‰ ู…ุซู„ manager-developer ุฃูˆ project-employee.
  5. Question 2 (UML Modeling) โ€” 20+ ู…ุงุฑูƒ ุนู„ู‰ ุจู†ุงุก use case diagram + class diagram + sequence diagram + state diagram ู„ู†ุธุงู… ู…ุนูŠู†. ุฑูƒุฒ ุนู„ูŠู‡ ู„ุฃู†ู‡ ุจูŠุชูƒุฑุฑ ููŠ ูƒู„ ุงู…ุชุญุงู† final.
๐ŸŽฏ ุขุฎุฑ ู†ุตูŠุญุฉ ู„ู„ู€ Question 2: ุงุจุฏุฃ ุจู€ actorsุŒ ุจุนุฏูŠู† use casesุŒ ุจุนุฏูŠู† class diagram ููŠู‡ุง ูƒู„ ุงู„ู€ classes ุงู„ู„ูŠ ุงุชุฐูƒุฑุช ููŠ ุงู„ู€ requirementsุŒ ู…ุน multiplicities ูˆุฃุณู…ุงุก relations ูˆุงุถุญุฉ.