Tải bản đầy đủ (.pdf) (6 trang)

LẬP TRÌNH HƯỚNG đối TƯỢNG bài 12 BIỂU đồ lớp

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (464.87 KB, 6 trang )

8/24/2011

Mục đích
Mô tảkhung nhìn tĩnh của hệthống và cách
đưa nó vào trong một mô hình.
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
chúng vào biểu đồlớp.
Mô hình tổng quát hóa (generalization) trên
một biểu đồlớp.

Bộ môn Công nghệ Phần mềm
Viện CNTT & TT
Trường Đại học Bách Khoa Hà Nội

k o?sqìmg?g ︰mf?I。h?s ︸mf
Bài 12. Biểu đồlớp

2

Nội dung
1.
2.
3.
4.

Nội dung

Biểu đồlớp (Class diagram)
Liên kết (Association)


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

1.
2.
3.
4.

Biể u đồ lớ p ( Class diagram)
Liên kết (Association)
Kết tập (Aggregation)
Tổng quát hóa (Generalization)

3

1.1. Lớp (Class)

4

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

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

8

Phạm vi (Scope)


Class1
- privateAttribute
+ publicAttribute
# protectedAttribute
- privateOperation ()
+ publicOPeration ()
# protecteOperation ()

Class1
- classifierScopeAttr
- instanceScopeAttr
+ classifierScopeOp ()
+ instanceScopeOp ()
10

9

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)
Khung nhìn tĩnh của hệthống
CloseRegistrationForm

Schedule

Ví dụBiểu đồlớp
Có cách nào tốt hơn đểtổchức biểu đồlớp?

CloseRegistrationController

- semester
+ open()
+ close registration()

Student
+ get tuition()
+ add schedule()

+ get schedule()
+ delete schedule()
+ has pre-requisites()

+ commit()
+ select alternate()
+ remove offering()
+ level()
+ cancel()
+ get cost()
+ delete()
+ submit()
+ save()
+ any conflicts?()
+ create with offerings()
+ update with new selections()

LoginForm

+ is registration open?()
+ close registration()

RegistrationController

RegisterForCoursesForm

Professor

Schedule


- name
- employeeID : UniqueId
- hireDate
- status
- discipline
- maxLoad

CloseRegistrationForm

CloseRegistrationController

Professor
Student

+ submitFinalGrade()
+ acceptCourseOffering()
+ setMaxLoad()
+ takeSabbatical()
+ teachClass()

Course

CourseOffering

CourseCatalogSystem
BillingSystem
13

Gói (package)


14

Ví dụ: Registration Package

Registration
CloseRegistrationForm

CloseRegistrationController

RegisterForCoursesForm

RegistrationController

University
Artifacts

15

Nội dung
1.
2.
3.
4.

16

Liên kết (association) là gì?
Mối liên hệngữnghĩa giữa hai hay nhiều lớp

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

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

Student

17

Schedule

Course

18

3


8/24/2011

Biểu diễn bội sốquan hệ

Bội sốquan hệ(Multiplicity)

Unspecified
Exactly One

Professor

instructor


CourseOffering

0..1

0..*

0..*

Zero or More

*

One or More

1..*

Zero or One (optional value)

0..1

Specified Range

2..4

Multiple, Disjoint Ranges

2, 4..6
20

Nội dung

1.

1

Zero or More

19

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

RegisterForCoursesForm

1

2.

RegistrationController

1

3.

0..1

4.

Biểu đồlớp (Class diagram)
Liên kết (Association)
Kế t tậ p ( Aggregation)
Tổng quát hóa (Generalization)


0..1
1
Student

Schedule

0..*

0..*

CourseOffering

0..4

22

21

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 (wholepart)

RegisterForCoursesForm

1


RegistrationController

1
0..1

0..1
1

Whole

Student

1
0..1

Schedule

0..*

Part
23

0..*

CourseOffering

0..4

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)

Part

Whole

Whole

Part

Composition

26

25

Ví dụ– Association

Ví dụ– Aggregration vs. Composition

public class StudentRegistrar {

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

27

Nội dung
1.
2.
3.
4.

28

Tổng quát hóa (Generalization)
Là mối liên hệ“là một loại” (“is a kind of”)

Biểu đồlớp (Class diagram)
Liên kết (Association)
Kết tập (Aggregation)
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

Discriminator

Abstract operation

+ communicate ()

Communication

+ withdraw()
+ createStatement()

Generalization
Relationship

There are no direct instances of Animal

Lion

Tiger


Subclasses
(children)

+ communicate ()

+ communicate ()

- balance
- name
- number

Superclass
(parent)

Abstract class

Animal

All objects are either lions or tigers

Savings

31

Ví dụvềĐa kếthừa

FlyingThing

Checking
32


Descendents

Đa hình (Polymorphism) là gì?

Animal
Multiple Inheritance

Airplane

Helicopter

Bird

Wolf

Horse

Manufacturer A

Manufacturer B

Manufacturer C

OO Principle:
Encapsulation
33

Remote Control


34

Tổng quát hóa: Thực thi đa hình
Animal
+ communicate ()

Lion
+ communicate ()

Without Polymorphism
if animal = “Lion” then
Lion communicate
else if animal = “Tiger” then
Tiger communicate
end

Tiger
+ communicate ()

With Polymorphism
Animal communicate

35

6



×