intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: Nguyen Kien | Ngày: | Loại File: PDF | Số trang:14

244
lượt xem
128
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'phân tích thiết kế hướng đối tượng (phần 10)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Phân tích thiết kế hướng đối tượng (phần 10)

  1. Created by Trang H ng Sơn ---------------------------------- 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. :A myB : B doOne doTwo 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: : Register : Sale makePayment(cashTendered) makePayment(cashTendered) messages in interaction diagrams indicate operations in the class diagrams classes identified in the interaction diagrams are Register Sale declared in the 1 class diagrams ... ... currentSale makePayment(…) makePayment(…) ... ... public class Register { private Sale sale = new Sale(); public void makePayment(Money cashTendered) { sale.makePayment(cashTendered); } } -1-
  2. Created by Trang H ng Sơn ---------------------------------- - Các ký hi u chung: lifeline box representing the class lifeline box representing a Font, or more precisely, that Font is lifeline box representing an named instance an instance of class Class – an unnamed instance of class Sale instance of a metaclass «metaclass» :Sale s1 : Sale Font List is an interface lifeline box representing an lifeline box representing instance of an ArrayList class, one instance of class Sale, in UML 1.x we could not use an parameterized (templatized) to selected from the sales interface here, but in UML 2, this (or hold Sale objects ArrayList collection an abstract class) is legal sales: x : List sales[ i ] : Sale ArrayList related example + Singleton Objects: public class Register { private Store store = Store.getInstance(); public void doX() { store.doA(); } } + Messages: : Register : Sale doX doA doB a found message whose sender will not be specified doC doD execution specification bar indicates focus of control typical sychronous message shown with a filled-arrow line -2-
  3. Created by Trang H ng Sơn ---------------------------------- + Reply or Returns: 2 cách th hi n : Register : Sale doX d1 = getDate getDate 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: note that newly created : Register : Sale objects are placed at their creation "height" makePayment(cashTendered) create(cashTendered) : Payment authorize -3-
  4. Created by Trang H ng Sơn ---------------------------------- 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 create(cashTendered) : Payment the «destroy» stereotyped ... message, with the large X and short lifeline «destroy» indicates explicit object X destruction + Looping construct: :A :B makeNewSale a UML loop loop [ more items ] enterItem(itemID, quantity) frame, with a boolean guard expression description, total endSale + Conditional Messages: 2 cách th hi n : Foo : Bar : Foo : Bar xx xx opt [ color = red ] calculate [ color = red ] calculate yy yy public class Foo { private Bar bar = new Bar(); public void doX() { bar.xx(); if (color.equals(“red”)) { bar.calculate(); } bar.yy(); -4-
  5. Created by Trang H ng Sơn ---------------------------------- } } + Mutually Exclusive Conditional Messages: :A :B :C doX alt [ x < 10 ] calculate [ else ] calculate 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 lineItems[i] : : Sale This lifeline box represents one SalesLineItem instance from a collection of many t = getTotal SalesLineItem objects. lineItems[i] is the expression to loop select one element from the [ i < lineItems.size ] st = getSubtotal collection of many SalesLineItems; the ‘i” value refers to the same “i” in the guard i++ 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 lineItems[i] : : Sale SalesLineItem t = getTotal loop st = getSubtotal -5-
  6. Created by Trang H ng Sơn ---------------------------------- public class Sale { private List lineItems = new ArrayList(); 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: : Foo : Bar xx opt [ color = red ] loop(n) calculate + Reference Diagrams: sd AuthenticateUser :B :C :A :B :C authenticate(id) doX doA doM1 doB doM2 authenticate(id) ref AuthenticateUser ref sd DoFoo DoFoo :B :C interaction occurrence doX note it covers a set of lifelines doY note that the sd frame it relates to doZ has the same lifelines: B and C -6-
  7. Created by Trang H ng Sơn ---------------------------------- + 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: Payment {abstract} Payment is an abstract authorize() {abstract} superclass, with concrete ... subclasses that implement the polymorphic authorize operation CreditPayment DebitPayment authorize() authorize() ... ... object in role of abstract polymorphic message superclass :Register :Payment {abstract} doX authorize stop at this point – don’t show any further details for this message :DebitPayment :Foo :CreditPayment :Bar authorize authorize doA doX doB separate diagrams for each polymorphic concrete case -7-
  8. Created by Trang H ng Sơn ---------------------------------- + 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 ... } } } -8-
  9. Created by Trang H ng Sơn ---------------------------------- 2. GRASP principles (General Responsibility Assignment Software Patterns): thi t k 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. -9-
  10. Created by Trang H ng Sơn ---------------------------------- - 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. - 10 -
  11. Created by Trang H ng Sơn ---------------------------------- + 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 squares = new Board().getAllSquares(); - 11 -
  12. Created by Trang H ng Sơn ---------------------------------- 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 ñó). - 12 -
  13. Created by Trang H ng Sơn ---------------------------------- 3. Case study “H th ng thư vi n ñi n t ”: - Use Case “ðăng nh p”: : Thu Thu : FormChinh : FormDangNhap : CtrlDangNhap : Thuthu 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 - 13 -
  14. Created by Trang H ng Sơn ---------------------------------- - Use Case “Thêm sách”: : Thu Thu : FormChinh : FormThemSach : CtrlThemSach : Sach 1: Yeu cau them sach 2: Load form ThemSach 3: Nhap thong tin sach moi 4: Boolean := insertBook(thongTinSach) 5: Boolean := validate(thongTinSach) 6: [ validate = True ] Boolean := insert(thongTinSach) 7: True 8: True 9: Thong bao them sach thanh cong - 14 -
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2