You are on page 1of 41

Chương 4

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

ER DFD with Data Store Structure Chart

h
OO approach MyAccount::

Objects aNo= 101 withdraw(X)


( )
:MyAccount
balance=500
balance( )
withdraw( )
---

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

How does an object encapsulate?


What does it encapsulate?

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

BankAccount Security RealEstate

Savings Checking Stock Bond

Decreasingg Elements at the same level of the hierarchy


abstraction should be at the same level of 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

Class and Object Name

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

Class Name Professor


name
Attributes (ct) empID
Operations (hv) create( )
save( )
delete( )
change( )

UML/NN 18
Lớp các đối tượng

UML/NN 19
Class và Object

„ Class là một định nghĩa cho một đối tượng


„ Lớp xác định cấu trúc và hành vi của đối tượng thuộc lớp
„ Một mẫu (template)
„ Obj t đ
Objects đuợc
ợ nhóm
hó lại
l i thành
thà h những
hữ lớlớp

Objects
j Class

Professor

Professor Smith Professor Mellon

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

Attribute Attribute Value


:CourseOffering
number
b = 101
startTime = 900
CourseOffering endTime = 1100
number
startTime
endTime :CourseOffering
number = 104
startTime = 1300
endTime = 1500

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

Realization relationship (stay tuned for realization relationships)


Interface
Tube
Elided/Iconic
Representation
p
(“lollipop”)
Pyramid

Shape Cube

Canonical Tube
(Class/Stereotype) <<interface>>
Representation Shape
p
Pyramid
Draw
Move
Scale
Rotate Cube

UML/NN (stay tuned for realization relationships) 28


Các khái niệm cơ bản
„ Object
„ Cl
Class
„ Attribute
„ Operation
„ Interface (Polymorphism)
„ Component
„ Package
„ Subsystem
„ Relationships

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

public Account(int an, int b){ Savings


accNo = an;
balance = b; Cheque
} ODLi it
ODLimit
withdraw( )
public int balance(){
return balance; withdraw( )
}
public abstract void withdraw(int amount);
}

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

You might also like