8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
1/41
ng p p ngtng trong cng ngh
phn mm
UML/NN 1
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
2/41
Ni dung Phng php hng i tng?
Cc khi nim c bn
UML/NN 2
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
3/41
Tng qut v hng i tng Phng php lp trnh hng i tng gii thiu mt
in (c cu trc) Bt u hnh thnh vo nhng nm cui ca thp nin
, n u p n rong c ng ng p p nmm
N n n hn i t n
Smalltalk, Eiffel
Object Pascal, C+, Java, C#,
tng Hi n na c m t ui trnh ht trin hn mm h
UML/NN 3
nht UP da trn UML
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
4/41
Skhc bitAccount
amountamount messa e
StructuredStructured
aNo balance
Account
w rawmessage
withdrawal
ER DFD with Data Store Structure Chart
withdraw(X)
MyAccount::
aNo= 101
OO approachOO approach
balance=500
balance( )
UML/NN 4
- - -
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
5/41
c trng c bn Tnh ng gi (encapsulation): che du mi chi
.
Tnh c lp cao gia cc i tng (tnh kt dnh thp) Tnh hn lo i classification : om nhm cci tng c cng cu trc v hnh vi vo cng
mt lp n g p aggrega on : ng c
cha nhiu i tng khc to nn mi quan h
Tnh tha k(inheritance): phn bit tng qutha v chuyn bit ha cc i tng
UML/NN 5
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
6/41
Nguyn l c bn
ec r en a on
ion
n y
apsula
stracti
ierarch
dulari
EncA HM
UML/NN 6
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
7/41
Abstraction?
Salesperson
Not sayingWhichsales erson just asalespersonin general!!!
Customer ro uc
UML/NN 7
anages omp ex ty
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
8/41
Encapsulation? Che du vic thc thi
Clients depend on interface
How does an object encapsulate?
What does it encapsulate?
UML/NN 8
mproves es ency
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
9/41
Modularity?
OrderEntry
Order ProcessingOrder
Fulfillment
Billing
UML/NN 9
Manages Complexity
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
10/41
Hierarchy?
Increasing
Asset
RealEstateBankAccount Security
Decreasin
UML/NN 10
abstraction
should be at the same level of abstraction
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
11/41
Cc khi nim c bn Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem
Relationships
UML/NN 11
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
12/41
Object? Thc thvt l
hc thkhi nim h c th hn mm
Chemical Process
Truck
Linked List
UML/NN 12
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
13/41
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
14/41
Biu din i tng
: Professora + b = 10
ProfessorClark
Class Name OnlyProfessor Clark
ProfessorClark :Professor Object Name Only
UML/NN 14
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
15/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships (trnh by chng sau)
UML/NN 15
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
16/41
Class?
A class is a description of a group of objectsw common proper es a r u es ,behavior (operations), relationships, andseman cs
An object is an instance (thhin) of a classA class is an abstraction in that it:
Nhn mnh n nhng c tnh chung B qua nhng c tnh ring bit
UML/NN 16
OO Principle: Abstraction
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
17/41
Lp kha hc
ClassCourse
Properties
Name
BehaviorAdd a student
a + b = 10Location
Days offered
Credit hours
Delete a studentGet course roster
Determine if it is full
End time
UML/NN 17
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
18/41
Biu din lp
Professora + b = 10
Professor Clark
Professorname
Class Name
Attributes (ct)
create( )save( )
Operations (hv)
UML/NN 18
change( )
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
19/41
Lp cc i tng
UML/NN 19
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
20/41
Class v Object
Class l mt nh ngha cho mt i tng Lp xc nh cu trc v hnh vi ca i tng thuc lp Mt mu (template)
Ob ects Class
ec s u c n m n n ng p
Professor
Professor Smith Professor Mellon
UML/NN 20
Professor Jones
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
21/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 21
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
22/41
Attribute?
ClassObject
:CourseOfferingAttribute Attribute Value
num er = 101startTime = 900endTime = 1100CourseOffering
:CourseOffering
=
startTimeendTime
startTime = 1300endTime = 1500
UML/NN 22
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
23/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 23
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
24/41
Operation?
CourseOfferingClass
deleteStudentgetStartTime
getEndTimeOperation
UML/NN 24
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
25/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 25
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
26/41
Polymorphism (a hnh)?
The ability to hide many different implementations behind a
Manufacturer AManufacturer B
Manufacturer C
OO Principle:Encapsulation
UML/NN 26
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
27/41
Interface?
Interfaces formalize polymorphism
Tube
- -
Pyramid
Shape
Cube
DrawMoveScaleRotate
Realization relationship (stay tuned for realization relationships)
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
28/41
Interface
TubeElided/Iconic
Representation
Pyramid
(lollipop)
CubeShape
Tube
Sha e
Canonical
(Class/Stereotype)
PyramidDrawMove
UML/NN 28
CubeRotate
(stay tuned for realization relationships)
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
29/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 29
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
30/41
Component?
A non-trivial, nearly independent, and replaceable
context of a well-defined architecture
OO Principle:
source code
Thnh phn thc thi
Source File
Name
ExecutableName
ComponentNameComponent
UML/NN 30
Interface
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
31/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 31
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
32/41
Package?
A package is a general purpose mechanism for
Mt thnh phn m hnh c thcha nhng thnh
Dng
Mt n v qun l cu hnh (configuration management)
OO Principle:
UML/NN 32
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
33/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 33
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
34/41
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
35/41
Subsystem v Component
Components are the physical realization of an abstraction inthe desi n
Subsystems can be used to represent the component in thedesign
Design Model Implementation Model
ComponentName
Component NameCom onent
Interface Interface
UML/NN 35
r nc p es: ncapsu a on an o u ar y
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
36/41
Cc khi nim c bn
Object
ass
Attribute
Operation
Interface (Polymorphism) Component
Package
Subsystem Relationships
UML/NN 36
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
37/41
Thng ip (message)
Thng ip l mt php gi tc v ca mtng c .
Thng ip bao gm 3 phn i tng ch
Du hiu nhn dng ca tc v mun gi Danh sch thng sgi
aCircle.Draw(pWnd);
UML/NN 37
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
38/41
Abstract Class v Abstract Method
public abstract class Account
Account
accNo
balance
protected int accNo;protected int balance;
balance( )
withdraw( )
public Account(int an, int b){accNo = an;
balance = b;
Savings
Cheque}public int balance(){
return balance;
withdraw( )m t
withdraw( )
public abstract void withdraw(int amount);}
UML/NN 38
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
39/41
Overriding, Inheritance, ReusingAccount
accNo
balance{
public Savings(int an, int b)
balance( )
withdraw( )
super (an, b);}
Savings
Chequepublic void withdraw(int amount){
if (balance > amount)
withdraw( )m t
withdraw( )
a ance -= amoun ;}
}
UML/NN 39
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
40/41
Overriding, Class VariableAccount
accNo
balance{
private static int ODlimit=100;ublic Current int an int b
balance( )
withdraw( )
{super (an, b);
}
Savings
Chequepublic void withdraw(int amount){
if (balance+ODlimit > amount)
withdraw( )m t
withdraw( )
-
}}
UML/NN 40
8/9/2019 UML Chuong 4 Cacvandecobanvehuongdoituong n
41/41
Run-time v inherited polymorphism
import java.io.*;public class withdrawCtrl
Account
accNo
balance
public Account getAccount() throws IOException {
int d =2;Account A;
balance( )
withdraw( )
==A= new Savings(100, 1000);
else
A= new Current 120 2000
Savings
Chequereturn A;
}withdraw( )
m t
withdraw( )
Account A;A=getAccount();int amount =100;
UML/NN 41
A.withdraw(amount);}
}
System.out.println (A.balance());