Académique Documents
Professionnel Documents
Culture Documents
Phương pháp
Ph há hướng
h ớ đối
tượng trong công nghệ
phần mềm
UML/NN 1
Nội dung
Phương pháp hướng đối tượng?
Các khái niệm cơ bản
UML/NN 2
Tổng quát về hướng đối tượng
Phương pháp lập trình hướng đối tượng giới thiệu một
quan điểm lập trình khác hẳn so với trường phái cổ
điển (có cấu trúc)
Bắt đầu hình thành vào những năm cuối của thập niên
60 đế
60, đến 1990 bắt đầđầu phổ
hổ biế
biến ttrong công
ô nghiệp
hiệ phần
hầ
mềm
Ngôn ngữ hướng đối tượng
Smalltalk, Eiffel
Object Pascal, C+, Java, C#,…
Hình thành phương pháp phân tích/thiết kế hướng đối
tượng
Hiện
ệ nayy có một
ộ qui
q trình phát
p triển phần
p mềm hợp
ợp
nhất UP dựa trên UML
UML/NN 3
Sự khác biệt
Structured
amount
Account amount message
message withdraw
ithd
aNo balance withdrawal
Account
h
OO approach MyAccount::
UML/NN 4
Đặc trưng cơ bản
Tính đóng gói (encapsulation): che dấu mọi chi
tiết hiện thực bên trong đối tượng.
tượng
Æ Tính độc lập cao giữa các đối tượng (tính kết dính thấp)
Tính pphân loại
ạ ((classification):
) ggom nhóm các
đối tượng có cùng cấu trúc và hành vi vào cùng
một lớp
Tí h gộp
Tính ộ ((aggregation):
ti ) 1 đối tượng
tượ có ó thể
chứa nhiều đối tượng khác tạo nên mối quan hệ
bao gộp một cách đệ qui giữa các đối tượng
Tính thừa kế (inheritance): phân biệt tổng quát
hóa và chuyên biệt hóa các đối tượng
UML/NN 5
Nguyên lý cơ bản
Obj t O
Object Orientation
i t ti
Encapsulattion
on
odularitty
Hierarchyy
bstractio
Mo
Ab
UML/NN 6
Abstraction?
Salesperson
Not saying
Which
salesperson
p
– just a
salesperson
in general!!!
Product
Customer
M
Manages C
Complexity
l i
UML/NN 7
Encapsulation?
Che dấu việc thực thi
Clients depend on interface
I
Improves R
Resiliency
ili
UML/NN 8
Modularity?
Order
Entry
Order Processing
System Order
Fulfillment
Billing
Manages Complexity
UML/NN 9
Hierarchy?
Asset
Increasing
abstraction
UML/NN 10
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 11
Object?
Thực thể vật lý
Thực thể khái niệm
Thực
ự thể phần
p mềm…
Chemical Process
Truck
Linked List
UML/NN 12
Object
An object is a concept, abstraction, or thing
with
ith sharp
h boundaries
b d i and d meaning
i for
f an
application
An object is something that has:
State
Behavior
Identityy
UML/NN 13
Biểu diễn đối tượng
: Professor
a + b = 10
ProfessorClark
Class Name Only
Professor Clark
ProfessorClark :
Professor Object Name Only
UML/NN 14
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships (trình bày ở chương sau)
UML/NN 15
Class?
A class is a description of a group of objects
with
ith common propertiesti (attributes),
( tt ib t )
behavior (operations), relationships, and
semantics
ti
An object is an instance (thể hiện) of a class
A class is an abstraction in that it:
Nhấn mạnh đến những g đặc tính chung
g
Bỏ qua những đặc tính riêng biệt
OO Principle: Abstraction
UML/NN 16
Lớp khóa học
Class
Course
Properties Behavior
Name Add a student
Location Delete a student
a + b = 10
Days offered Get course roster
Credit hours Determine if it is full
Start time
End time
UML/NN 17
Biểu diễn lớp
a + b = 10
Professor
Professor Clark
UML/NN 18
Lớp các đối tượng
UML/NN 19
Class và Object
Objects
j Class
Professor
Professor Jones
UML/NN 20
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 21
Attribute?
Object
Class
UML/NN 22
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 23
Operation?
CourseOffering
Class
addStudent
deleteStudent
getStartTime
Operation getEndTime
UML/NN 24
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 25
Polymorphism (đa hình)?
The ability to hide many different implementations behind a
single interface
Manufacturer B
Manufacturer A Manufacturer C
OO Principle:
Encapsulation
UML/NN 26
Interface?
Interfaces formalize polymorphism
Interfaces support “plug
plug-and-play
and play” architectures
Tube
<<interface>>
Shape
Pyramid
Draw
D
Move
Scale
Rotate Cube
Shape Cube
Canonical Tube
(Class/Stereotype) <<interface>>
Representation Shape
p
Pyramid
Draw
Move
Scale
Rotate Cube
UML/NN 29
Component?
A non-trivial, nearly independent, and replaceable
part of a system that fulfills a clear function in the
context of a well-defined architecture
Một thành phần có thể là
source code OO Principle:
Thành phần DLL (run time) Encapsulation
Thành phần thực thi
<<EXE>> <<DLL>>
Source File
Executable Component
Name
Name Component Name
Interface
UML/NN 30
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 31
Package?
A package is a general purpose mechanism for
organizing elements into groups
Một thành phần mô hình có thể chứa những thành
phần mô hình khác
Dùng
Tổ chức mô hình cho việc phát triển
Một đơn vị quản lý cấu hình (configuration management)
OO Principle:
Modularity
UML/NN 32
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 33
Subsystem (hệ thống con)?
A combination of a package (can contain other
model elements) and a class (has behavior)
Realizes one or more interfaces which define its
behavior
Realization
Subsystem
<<subsystem>>
Interface Subsystem Name
Interface
OO Principles:
p Encapsulation
p and Modularityy
(stay tuned for realization relationship)
UML/NN 34
Subsystem và Component
Components are the physical realization of an abstraction in
the design
g
Subsystems can be used to represent the component in the
design
Design Model Implementation Model
<<subsystem>> Component
Name
Component Name
Component Co po e t
Component
Interface Interface
OO P
Principles:
i i l Encapsulation
E l ti andd Modularity
M d l it
UML/NN 35
Các khái niệm cơ bản
Object
Cl
Class
Attribute
Operation
Interface (Polymorphism)
Component
Package
Subsystem
Relationships
UML/NN 36
Thông điệp (message)
Thông điệp là một phép gọi tác vụ của một
đối tượng
tượ cụ thể.
thể
Thông điệp bao gồm 3 phần
Đối tượng đích
Dấu hiệu
ệ nhận
ậ dạng
ạ g của tác vụ
ụ muốn gọi
gọ
Danh sách thông số gọi
Ví dụ: aCircle.SetRadius(3);
aCircle.Draw(pWnd);
UML/NN 37
Abstract Class và Abstract Method
Account
public abstract class Account accNo
{ balance
protected int accNo; balance( )
protected int balance; withdraw( )
UML/NN 38
Overriding, Inheritance, Reusing
Account
accNo
public class Savings extends Account balance
{ balance( )
public Savings(int an, int b) withdraw( )
{
super (an, b); Savings
}
Cheque
public void withdraw(int amount) ODLi it
ODLimit
withdraw( )
{
if (balance > amount) withdraw( )
b l
balance -= amount;
t
}
}
UML/NN 39
Overriding, Class Variable
Account
accNo
public class Current extends Account balance
{ balance( )
private static int ODlimit=100; withdraw( )
public Current(int
p ( an,, int b))
{ Savings
super (an, b);
} Cheque
public void withdraw(int amount) ODLi it
ODLimit
withdraw( )
{
if (balance+ODlimit > amount) withdraw( )
balance -=
= amount;
}
}
UML/NN 40
Run-time và inherited polymorphism
import java.io.*; Account
public class withdrawCtrl accNo
{ balance
public Account getAccount() throws IOException { balance( )
int d =2; withdraw( )
Account A;
if (d
(d==1)
1)
A= new Savings(100, 1000); Savings
else
Cheque
A= new Current(120,2000);
( , );
return A;
ODLi it
ODLimit
withdraw( )
}
withdraw( )
public void withdrawCtr() throws IOException {
Account A;
A=getAccount();
int amount =100;
A.withdraw(amount); System.out.println (A.balance());
}
}
UML/NN 41