1. Trang chủ
  2. » Công Nghệ Thông Tin

Phân tích thiết kế hướng đối tượng (phần 10) pptx

14 417 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 14
Dung lượng 1,09 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Tổng quan: - Sequence Diagram: là sơ ñồ mô tả sự tương tác giữa các ñối tượng theo hướng thời gian, nhấn mạnh thứ tự thực hiện các tương tác.. 1 currentSale messages in interaction diag

Trang 1

- 1 -

SEQUENCE DIAGRAM

1 Tổng quan:

- Sequence Diagram: là sơ ñồ mô tả sự tương tác giữa các ñối tượng theo hướng thời gian, nhấn mạnh thứ tự thực hiện các tương tác

doTwo doOne

doThree

public class A {

private B myB = new B();

public void doOne() {

myB.doTwo();

myB.doThree();

}

}

- Mối quan hệ giữa sơ ñồ tương tác và sơ ñồ lớp:

makePayment(cashTendered) makePayment(cashTendered)

Register

makePayment(…)

Sale

makePayment(…)

1 currentSale

messages in interaction diagrams indicate operations

identified in the interaction diagrams are declared in the class diagrams

public class Register {

private Sale sale = new Sale();

public void makePayment(Money cashTendered) {

sale.makePayment(cashTendered);

}

}

Trang 2

- Các ký hiệu chung:

sales:

ArrayList<Sale>

lifeline box representing an

instance of an ArrayList class,

parameterized (templatized) to

hold Sale objects

lifeline box representing an

unnamed instance of class Sale

lifeline box representing a named instance

sales[ i ] : Sale

lifeline box representing

one instance of class Sale, selected from the sales

ArrayList <Sale> collection

x : List

«metaclass»

Font

lifeline box representing the class

Font, or more precisely, that Font is

an instance of class Class – an

instance of a metaclass

related example

List is an interface

in UML 1.x we could not use an interface here, but in UML 2, this (or

an abstract class) is legal

+ Singleton Objects:

public class Register {

private Store store = Store.getInstance();

public void doX() {

store.doA();

}

}

+ Messages:

doA

doB doX

doC doD

typical sychronous message

shown with a filled-arrow line

a found message

whose sender will not

be specified

execution specification

bar indicates focus of control

Trang 3

- 3 -

+ Reply or Returns: 2 cách thể hiện

d1 = getDate

getDate doX

aDate

public class Register {

private Sale sale = new Sale();

public void doX() {

Date d1 = sale.getDate();

}

}

+ Messages to "self" or "this":

: Register doX

clear

public class Register {

public void doX(){

this.clear();

}

private void clear(){

}

}

+ Creation of Instances:

: Register : Sale

makePayment(cashTendered)

: Payment create(cashTendered)

authorize

note that newly created objects are placed at their creation "height"

Trang 4

-public class Sale {

private Payment payment;

public void makePayment(Money cashTendered) {

payment = new Payment(cashTendered);

payment.authorize();

}

}

+ Object Destruction: các ngôn ngữ lập trình không hổ trợ “garbage collection”

: Sale

: Payment create(cashTendered)

the «destroy» stereotyped message, with the large

X and short lifeline indicates explicit object destruction

«destroy»

X

+ Looping construct:

enterItem(itemID, quantity)

: B

endSale

a UML loop

frame, with a boolean guard

makeNewSale [ more items ]

loop

: A

+ Conditional Messages: 2 cách thể hiện

calculate

: Bar

yy

xx [ color = red ]

opt

: Foo

[ color = red ] calculate

: Bar

yy

xx : Foo

public class Foo {

private Bar bar = new Bar();

public void doX() {

bar.xx();

if (color.equals(“red”)) {

bar.calculate();

}

bar.yy();

Trang 5

- 5 -

}

}

+ Mutually Exclusive Conditional Messages:

: B : A

calculate doX

: C

calculate

[ x < 10 ] alt

[ else ]

public class A {

private B b = new B();

private C c = new C();

public void doX() {

if (x < 10) {

b.calculate();

} else {

c.calculate();

}

}

}

+ Iteration Over a Collection: 2 cách thể hiện

st = getSubtotal

lineItems[i] :

SalesLineItem

t = getTotal

[ i < lineItems.size ] loop

instance from a collection of many

SalesLineItem objects

lineItems[i] is the expression to

select one element from the collection of many SalesLineItems; the ‘i” value refers to the same “i” in the guard

in the LOOP frame

an action box may contain arbitrary language

statements (in this case, incrementing ‘i’)

it is placed over the lifeline to which it applies

i++

st = getSubtotal

lineItems[i] :

SalesLineItem

t = getTotal

loop

: Sale

Trang 6

-public class Sale {

private List<SalesLineItem> lineItems = new ArrayList<SalesLineItem>();

public Money getTotal() {

Money total = new Money();

Money subtotal = null;

for (SalesLineItem lineItem : lineItems) {

subtotal = lineItem.getSubtotal();

total.add( subtotal );

}

return total;

}

}

+ Nesting of Frames:

calculate

: Bar xx

[ color = red ]

opt

: Foo

loop(n)

+ Reference Diagrams:

interaction occurrence note it covers a set of lifelines note that the sd frame it relates to has the same lifelines: B and C

doA

doB

sd AuthenticateUser

ref AuthenticateUser

authenticate(id)

doX

doM1

authenticate(id)

doM2

ref

doX

doY doZ

Trang 7

- 7 -

+ Messages to Classes or Invoke Static Methods:

public class Foo {

public void doX() {

// static method call on class Calendar

Locale[] locales = Calendar.getAvailableLocales();

}

}

+ Polymorphic Messages and Cases:

:Register

authorize doX

:Payment {abstract}

superclass

:DebitPayment

doA authorize

:Foo

stop at this point – don’t show any further details for this message

doB

:CreditPayment

doX authorize

:Bar

Payment {abstract}

authorize() {abstract}

CreditPayment authorize()

DebitPayment authorize()

Payment is an abstract

superclass, with concrete subclasses that implement the polymorphic authorize operation

separate diagrams for each polymorphic concrete case

Trang 8

-+ Asynchronous and Synchronous Calls:

public class ClockStarter {

public void startClock() {

Thread t = new Thread( new Clock() );

t.start(); // asynchronous call to the 'run' method on the Clock

System.runFinalization(); // example follow-on message

}

}

// objects should implement the Runnable interface, in Java to be used on new threads

public class Clock implements Runnable {

public void run() {

while (true) { // loop forever on own thread

}

}

}

Trang 9

- 9 -

hướng trách nhiệm (Responsibility-Driven Design)

- “knowing” responsibility:

+ private encapsulated data

+ related objects

+ things it can derive or calculate

- “doing” responsibility:

+ take action (create an object, do a calculation)

+ initiate action in other objects

+ control / coordinate actions in other objects

- 5 nguyên lý cơ bản:

+ Controller :

Vấn ñề: ñối tượng nào ñứng sau UI layer ñể nhận sự tương tác của Actor và ñiều khiển các hoạt ñộng của hệ thống ?

Giải quyết:

- Facade controller: xử lý cho toàn bộ hệ thống, là ñối tượng root

- Session controller: xử lý cho một use case cụ thể nào ñó

Ví dụ:

- ðối tượng nào ñiều khiển hoạt ñộng của game Monopoly ?

- Facade controller: ñối tượng root ?  MonopolyGame

Trang 10

- Session controller: use case “PlayGame”  PlayMonopolyGameHandler hoặc PlayMonopolyGameSession

+ Creator :

Vấn ñề: ñối tượng nào tạo ra ñối tượng A ?

Giải quyết: ñối tượng B tạo ra ñối tượng A khi:

- B “contains” A (chứa)

- B records A (ghi nhận)

- B closely uses A (sử dụng)

- B has the initializing data for A (khởi tạo dữ liệu) Ví dụ:

- ðối tượng nào tạo ñối tượng Square ?  Board

Trang 11

- 11 -

+ Information Expert :

Vấn ñề: ñối tượng nào nắm giữ thông tin của ñối tượng A ?

Giải quyết: bổ sung thao tác lấy thông tin

Ví dụ:

- ðối tượng nào nắm giữ thông tin về ñối tượng Square ?  Board

+ Low Coupling :

Vấn ñề: làm sao ñể giảm sự ảnh hưởng khi có sự thay ñổi, nói cách khác là giảm sự móc nối, sự phụ thuộc lẫn nhau ?

Giải quyết: số lần gọi hàm ñến ñối tượng khác là ít nhất có thể

Ví dụ:

public class A {

public void doX() {

Square s = new Dog().getSquare("aaa");

}

}

public class Dog {

public void getSquare(String name) {

Map<Square> squares = new Board().getAllSquares();

Trang 12

return squares.get(name);

}

}

public class A {

public void doX() {

Square s = new Board().getSquare("aaa");

}

}

+ High Cohesion :

Vấn ñề: làm sao ñể ñưa các thao tác vào ñúng trách nhiệm của các ñối tượng ?

Giải quyết: phân rõ trách nhiệm của từng ñối tượng cụ thể

Ví dụ: trách nhiệm của MonopolyGame là Controller (ñiều khiển, ñiều hướng hoạt ñộng, chứ không phải giải quyết, thực thi các thao tác cụ thể nào ñó)

Trang 13

- 13 -

3 Case study “Hệ thống thư viện ñiện tử”:

- Use Case “ðăng nhập”:

1: Yeu cau dang nhap

2: Load form DangNhap

3: Nhap thong tin dang nhap (username, password)

4: Boolean := authorize(username, password)

5: Boolean := check(username, password)

6: True 7: True

8: Chuyen den trang quan ly

- Use Case “Tra cứu sách”:

: Ban Doc : FormChinh : FormTraCuuSach : CtrlTraCuuSach : Sach

1: Yeu cau tra cuu sach

2: Load form TraCuuSach

3: Nhap thong tin tra cuu (tuKhoa, tieuChi)

4: ArrayList := searchBook(tuKhoa, tieuChi)

5: ArrayList := search(tuKhoa,tieuChi)

6: Danh sach ket qua tim kiem 7: Danh sach ket qua tim kiem

8: Danh sach cac quyen sach tim duoc

9: Chon mot quyen sach

10: Sach := getBook(maSach)

11: Sach := get(maSach)

12: Sach 13: Sach

14: Thong tin chi tiet sach

Trang 14

- Use Case “Thêm sách”:

: Thu Thu : FormChinh : FormThemSach : CtrlThemSach : Sach

1: Yeu cau them sach

3: Nhap thong tin sach moi

9: Thong bao them sach thanh cong

4: Boolean := insertBook(thongTinSach)

8: True

6: [ validate = True ] Boolean := insert(thongTinSach)

7: True 5: Boolean := validate(thongTinSach) 2: Load form ThemSach

Ngày đăng: 08/07/2014, 08:20

TỪ KHÓA LIÊN QUAN