CLASS DIAGRAM
Adapted from Washington Univ’s course stuff
ì
Design Phase ì design: specifying the structure of how a
so;ware system will be wri>en and func?on, without actually wri?ng the complete implementa?on
ì a transi?on from "what" the system must do, to
"how" the system will do it
ì What classes will we need to implement a system
that meets our requirements? ì What fields and methods will each class have? ì How will the classes interact with each other?
OO Design ì Class Diagram ì Sequence Diagram
Class diagram ì Merupakan diagram sta?s: maksudnya hanya
menggambarkan struktur dari sistem, tanpa menggambarkan alur atau urutan proses
ì Elemen-‐elemen yang berhubungan: ì Class ì Atribut ì Method/fungsi ì Relasi class: ì Generalisasi ì Assosiasi ì Agregasi ì Composisi
Cara Menentukan Klas ì Textual Analysis (based on Dennis, 2002): ì Kata benda yang masih umum adalah Klas. Contoh: Komputer, Mahasiswa, Dosen dsb ì Kata benda yang sudah spesifik adalah Objek. Contoh: Dell, Sony Vaio, HP, Mahasiswa A, Pak Budi Harijanto dsb ì Kata benda yang merupakan proper? dari kata benda yang lain adalah Atribut. Contoh: Motherboard, Monitor, nim, nama, NIP dsb ì Behaviour atau fungsi atau proses tau kata kerja merupakan Mehod. Contoh: menmyimpan, menampilkan data, mengerjakan soal dsb ì Koleksi data menunjukkan ditunjukan dengan Array, List dsbs
Diagram Klas
Diagram Klas: Modifier akses ì + à public ì -‐ à private ì ~ à default ì # àprotected
Diagram Klas: Atribut ì Contoh
+ nim: String -‐ ipk: double = 0 (nilai 0 menunjukkan inisialisasi nilai atribut ipk) + spp: int = 1000 (garis bawah menunjukkan atribut klas, kalau di Java kata kunci sta'c)
Diagram Klas: Method ì Contoh
+ tampil () -‐ tambah(a:int, b:int): int (method tambah memiliki 2 argumen funsgi dan =pe fungsi int, atau akan me-‐ return nilai int) -‐ kurang(a:int, b:int) ( garis bawah menunjukkan method klas, kalau di Java ditandai kata kunci sta'c)
Contoh
Comments ì represented as a folded note, a>ached to the
appropriate class/method/etc by a dashed line
11
Relationships btwn. classes ì generaliza.on: an inheritance rela?onship ì inheritance between classes ì interface implementa?on
ì associa.on: a usage rela?onship ì dependency ì aggrega?on ì composi?on
12
Generalization relationships ì generaliza?on (inheritance) rela?onships ì
hierarchies drawn top-‐down with arrows poin?ng upward to parent
ì
line/arrow styles differ, based on whether parent is a(n): ì class:
solid line, black arrow ì abstract class: solid line, white arrow ì interface: dashed line, white arrow ì
we o;en don't draw trivial / obvious generaliza?on rela?onships, such as drawing the Object class as a parent
13
Associational relationships ì associa?onal (usage) rela?onships 1. mul?plicity (how many are used) ì *
⇒ 0, 1, or more
ì 1
⇒ 1 exactly
ì 2..4
⇒ between 2 and 4, inclusive
ì 3..*
⇒ 3 or more
2. name 3. navigability
(what rela?onship the objects have) (direc?on)
14
Multiplicity of associations n
one-to-one n
n
each student must carry exactly one ID card
one-to-many n
one rectangle list can contain many rectangles
15
Association types ì aggrega.on: "is part of" ì symbolized by a clear white diamond ì composi.on: "is en?rely made of" ì stronger version of aggrega?on ì the parts live and die with the whole ì symbolized by a black diamond
ì dependency: "uses temporarily" ì symbolized by do>ed line ì o;en is an implementa?on detail, not an intrinsic part of that object's state
Car
1 1
Book
aggregation
Engine
1 * composition
Page
dependency
Lottery Ticket
Random 16
Class diagram example 1
17
Class diagram example 2 Multiplicity Customer Class
Simple Aggregation
1
Rental Invoice
Abstract Class Rental Item
1..*
1
0..1
Composition
Simple Association
Generalization
DVD Movie
VHS Movie
Video Game
Checkout Screen
18
Class diagram example 3 StudentBody + main (args : String[])
Address - streetAddress : String - city : String - state : String - zipCode : long + toString() : String
1
100
Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address + toString() : String
Tools for creating UML diags. ì Violet (free) ì h>p://horstmann.com/violet/ ì Ra?onal Rose ì
h>p://www.ra?onal.com/
ì Visual Paradigm UML Suite (trial) ì h>p://www.visual-‐paradigm.com/ ì (nearly) direct download link:
h>p://www.visual-‐paradigm.com/vp/download.jsp?product=vpuml&edi?on=ce
ì StarUML (free) ì ArgoUML (free) (there are many others, but most are commercial)
20