You are on page 1of 6

8/24/2011

Mục đích

Bộ môn Công nghệ Phần mềm  Mô tả khung nhìn tĩnh của hệ thống và cách
Viện CNTT & TT đưa nó vào trong một mô hình.
Trường Đại học Bách Khoa Hà Nội  Minh họa cách đọc và hiểu một biểu đồ lớp.
 Mô hình hóa mối liên kết (association) và kết
tập (aggregation) và chỉ ra cách mô hình
LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG chúng vào biểu đồ lớp.
Bài 12. Biểu đồ lớp  Mô hình tổng quát hóa (generalization) trên
một biểu đồ lớp.

Nội dung Nội dung

1. Biểu đồ lớp (Class diagram) 1. Biểu đồ lớp (Class diagram)


2. Liên kết (Association) 2. Liên kết (Association)
3. Kết tập (Aggregation) 3. Kết tập (Aggregation)
4. Tổng quát hóa (Generalization) 4. Tổng quát hóa (Generalization)

3 4

1.1. Lớp (Class) Biểu diễn thuộc tính


 attributeName : Type = Default

Class_Name
attribute1
attribute2
attribute3
method1()
method2()
method3()

5 6

1
8/24/2011

Mô tả phương thức Phạm vi truy cập (Visibility)


operationName([direction] parameter:class,...):returnType

private

public protected

7 8

Phạm vi truy cập được biểu diễn như thế


nào? Phạm vi (Scope)

Class1
- privateAttribute
+ publicAttribute
# protectedAttribute
- privateOperation ()
+ publicOPeration () Class1
# protecteOperation ()
- classifierScopeAttr
- instanceScopeAttr
+ classifierScopeOp ()
+ instanceScopeOp ()
9 10

Ví dụ: Scope 1.2. Biểu đồ lớp là gì?


 Biểu đồ lớp chỉ ra sự tồn tại của các lớp

Student

- name
- address
- studentID
- nextAvailID : int

+ addSchedule ([in] theSchedule : Schedule, [in] forSemester : Semester)


+ getSchedule ([in] forSemester : Semester) : Schedule
+ hasPrerequisites ([in] forCourseOffering : CourseOffering) : boolean
# passed ([in] theCourseOffering : CourseOffering) : boolean
+ getNextAvailID () : int

11 12

2
8/24/2011

Biểu đồ lớp (Class Diagram – CD) Ví dụ Biểu đồ lớp


 Khung nhìn tĩnh của hệ thống  Có cách nào tốt hơn để tổ chức biểu đồ lớp?
CloseRegistrationForm Schedule CloseRegistrationController
- semester
LoginForm RegistrationController
+ open() + is registration open?()
+ close registration() + commit() + close registration() RegisterForCoursesForm
+ select alternate()
+ remove offering()
+ level() Professor
+ cancel() - name
Schedule
Student + get cost() - employeeID : UniqueId CloseRegistrationForm CloseRegistrationController
+ delete() - hireDate
+ submit() - status
+ get tuition()
+ save() - discipline
+ add schedule() Professor
+ any conflicts?() - maxLoad
+ get schedule() Student
+ create with offerings()
+ delete schedule() Course CourseOffering
+ update with new selections() + submitFinalGrade()
+ has pre-requisites()
+ acceptCourseOffering()
+ setMaxLoad() CourseCatalogSystem
+ takeSabbatical()
+ teachClass() BillingSystem
13 14

Gói (package) Ví dụ: Registration Package

Registration

CloseRegistrationForm CloseRegistrationController

University
Artifacts
RegisterForCoursesForm RegistrationController

15 16

Nội dung Liên kết (association) là gì?

1. Biểu đồ lớp (Class diagram)  Mối liên hệ ngữ nghĩa giữa hai hay nhiều lớp

2. Liên kết (Association)


3. Kết tập (Aggregation)
4. Tổng quát hóa (Generalization)

Student Schedule Course

17 18

3
8/24/2011

Bội số quan hệ (Multiplicity) Biểu diễn bội số quan hệ


Unspecified
Exactly One 1

Zero or More 0..*

Zero or More *

One or More 1..*

Zero or One (optional value) 0..1

Specified Range 2..4


instructor
Professor CourseOffering
0..1 0..*
Multiple, Disjoint Ranges 2, 4..6
19 20

Ví dụ về bội số quan hệ Nội dung

1. Biểu đồ lớp (Class diagram)


RegisterForCoursesForm
1
RegistrationController 2. Liên kết (Association)
1

0..1
3. Kết tập (Aggregation)
4. Tổng quát hóa (Generalization)
0..1

1 0..*
Student Schedule CourseOffering
0..* 0..4

21 22

Kết tập (aggregation) là gì? Ví dụ về kết tập


 Là một dạng đặc biệt của liên kết mô hình
hóa mối quan hệ toàn thể-bộ phận (whole-
part) RegisterForCoursesForm
1
RegistrationController
1

0..1

0..1

1 0..*
Student Schedule CourseOffering
Whole 1 Part 0..* 0..4

0..1
23 24

4
8/24/2011

Cấu thành (Composition) là gì? Association, Aggregation and Composition

 Mối quan hệ giữa các lớp


(relationship)

Whole Part

Whole Part

Composition 25 26

Ví dụ – Association Ví dụ – Aggregration vs. Composition


public class StudentRegistrar {
public StudentRegistrar (){
(new RecordManager()).initialize();
}
}

27 28

Nội dung Tổng quát hóa (Generalization)

1. Biểu đồ lớp (Class diagram)  Là mối liên hệ “là một loại” (“is a kind of”)

2. Liên kết (Association)


3. Kết tập (Aggregation)
4. Tổng quát hóa
(Generalization)

29 30

5
8/24/2011

Lớp trừu tượng và lớp cụ thể


(Abstract and Concrete Class) Ví dụ về Đơn kế thừa

Ancestor
Account
- balance
Superclass - name
Discriminator Animal Abstract class - number
(parent)
+ communicate () Abstract operation + withdraw()
+ createStatement() Generalization
Communication There are no direct instances of Animal Relationship

Lion Tiger Subclasses


(children) Savings Checking
+ communicate () + communicate ()

All objects are either lions or tigers 31 32


Descendents

Ví dụ về Đa kế thừa Đa hình (Polymorphism) là gì?

FlyingThing Animal

Multiple Inheritance

Airplane Helicopter Bird Wolf Horse Manufacturer B


Manufacturer A Manufacturer C

OO Principle:
Encapsulation

Remote Control
33 34

Tổng quát hóa: Thực thi đa hình

Animal

+ communicate ()

Lion Tiger

+ communicate () + communicate ()

Without Polymorphism With Polymorphism


if animal = “Lion” then Animal communicate
Lion communicate
else if animal = “Tiger” then
Tiger communicate
end
35

You might also like