LECTURE 7: Object Oriented Design

Ivan Marsic Rutgers University

Topics

• Gán trách nhiệm cho các đối tượng • Design Principles • Expert Doer • High Cohesion • Low Coupling

• Business Policies • Class Diagram

2

System Sequence Diagrams

Biểu đồ tuần tự hệ thống cho usecase mở khóa

: System

User «initiating actor»

Timer «offstage actor»

select function(“unlock")

prompt for the key

enter key

verify key

signal: valid key, lock open

open the lock, turn on the light

start ("duration“)

System Sequence Diagrams considered interactions between the actors

3

Design: Object Interactions

Design Sequence Diagram

System Sequence Diagram

Controller Controller : Checker : Checker : KeyStorage : KeyStorage : LockCtrl : LockCtrl

: System

ystemystem

User «initiating actor»

Timer «offstage actor»

select function(“unlock")

prompt for the key

checkKey() checkKey() sk := getNext() sk := getNext()

enter key

verify key

signal: valid key, lock open

alt alt val != null val != null setOpen(true) setOpen(true)

open the lock, turn on the light

start ("duration“)

• System Sequence Diagram mô tả tương tác giữa các actor

4

• Object Sequence Diagram mô tả tương tác giữa các đối tượng

val == null : setLit(true) val == null : setLit(true) [else] [else]

Metaphor for Software Design: “Connecting the Dots”

:InterfacePage

:SearchRequest

:Controller

:PageMaker

:DatabaseConn

:Archiver

:Notifier

:InvestigRequest

Resident Resident

Database Database

Landlord Landlord

Ta bắt đầu từ các đối tượng/khái niệm từ pha phân tích, và sửa hoặc thêm các đối tượng mới, nhằm làm cho hệ thống hoạt động được.

5

Types of Object Responsibilities

• Knowing responsibility: ghi nhớ dữ liệu hoặc

tham chiếu, chẳng hạn data values, data collections, tham chiếu tới các đối tượng khác. Biểu diễn ở dạng thuộc tính

• Doing responsibility: thực hiện tính toán, chẳng hạn xử lý dữ liệu, điều khiển các thiết bị phần cứng,… biểu diễn dưới dạng phương thức

• Communicating responsibility: Liên lạc với các đối tượng khác, thể hiện ở việc gọi phương thức của đối tượng khác

6

Design: Assigning Responsibilities

? ?

? ?

: DatabaseConn : DatabaseConn : PageMaker : PageMaker : Controller : Controller : Checker : Checker : DeviceCtrl : DeviceCtrl

R1. R1.

checkKey() checkKey() accessList := retrieve(params : string) accessList := retrieve(params : string)

activate( "lock" ) activate( "lock" ) interfacePage := interfacePage := render(accessList : string) render(accessList : string)

? ?

R2. R2.

(a)

(b)

7

Đặc điểm của thiết kế tốt

• Chuỗi liên lạc ngắn

• Phân công công việc cân bằng

method_1()

method_1() method_2() … method_N()

• Ít quan hệ giữa các lớp

8

Design Principles

• Expert Doer Principle: ai biết thông tin thì làm

• High Cohesion Principle: không giữ nhiều trách

nhiệm

• Low Coupling Principle: không giữ quá nhiều

communication responsibility

There are many more …

9

Cohesion

Low cohesion

High cohesion

10

Class Diagram

Base Class Base Class

Container Container

PhotoSObsrv Key KeyStorage 1 1..*

+ isDaylight() : boolean + getNext() : Key

– code_ : string – timestamp_ : long – doorLocation_ : string 1 validKeys 1 sensor

Controller

# numOfAttemps_ : long # maxNumOfAttempts_ : long

+ enterKey(k : Key) – denyMoreAttempts() KeyChecker 1

checker + checkKey(k : Key) : boolean – compare(k : Key, sk : Key) : boolean 1 devCtrl

DeviceCtrl 1 logger # devStatuses_ : Vector

Logger

11

+ logTransaction(k : Key) + activate(dev : string) : boolean + deactivate(dev :string) : boolean + getStatus(dev : string) : Object