software engineering i 02161
play

Software Engineering I (02161) Week 7: Sequence Diagrams, Class - PowerPoint PPT Presentation

Software Engineering I (02161) Week 7: Sequence Diagrams, Class Diagrams II, Layered Architecture Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2017 Contents Recap Sequence Diagrams Object-orientation:


  1. Software Engineering I (02161) Week 7: Sequence Diagrams, Class Diagrams II, Layered Architecture Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2017

  2. Contents Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

  3. Recap ◮ How to get from requirements to the design? ◮ Execute the use case scenarios / user stories ◮ Different techniques: ”in your head”, Class Responsibility Collaboration (CRC) cards, Class diagrams + Sequence diagrams ◮ Communicating your design: ◮ Class diagrams

  4. Contents Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

  5. Sequence Diagram: Computing the price of an order ◮ Class diagram Order Customer name calculate price discount info 1 calculate base price calculate discounts * OrderLine Product quantity name 1 price ◮ Problem: ◮ What are the operations doing?

  6. Sequence diagram

  7. Synchronous– vs Asynchronous Calls: Synchronous b.m(4); c.n(...) // Starts after m has returned Asynchronous // (new Thread(){ public void run() {b.m(4);}}).start(); new Thread(() -> {b.m(4);}).start(); // Using Lambdas from Java 8 c.n(...) // Starts immediately after m has been called a:A b:B async call sync call return

  8. Interaction Frames Example Realising an algorithm using a sequence diagram public void dispatch() { for (LineItem lineItem : lineItems) { if (lineItem.getValue() > 10000) { careful.dispatch(); } else { regular.dispatch(); } } if (needsConfirmation()) { messenger.confirm(); } }

  9. Realisation with Interaction Frames

  10. Interaction Frame Operators I

  11. Usages of sequence diagrams ◮ Show the exchange of messages of a system ◮ i.e. show the execution of the system ◮ in general only, one scenario ◮ with the help of interaction frames also several scenarios ◮ For example use sequence diagrams for ◮ Designing (c.f. CRC cards) ◮ Visualizing program behaviour

  12. Contents Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

  13. Centralized control Order Customer name calculate price discount info 1 calculate base price calculate discounts * OrderLine Product quantity name price 1

  14. Centralised control

  15. Distributed control

  16. Distributed Control: Class diagram Order Customer name calculate price 1 discount info calculate base price calculate discounts calculate discount * OrderLine Product quantity name 1 price calculate price get price for quantity

  17. Centralized vs Distributed control ◮ Centralized control ◮ One method ◮ Data objects → procedural programming language ◮ Distributed control ◮ Objects collaborate ◮ Objects = data and behaviour → Object-orientation ◮ Advantage ◮ Easy to adapt → Design for change

  18. Design for Change How to add a new type of product, which is cheaper in large quantities?

  19. Design for Change How to add a new type of product, which is cheaper in large quantities? sd: Order calculateOrder Order OrderLine Product BulkProduct Customer User calculateOrder calculatePrice getPrice(n) calculatePrice getPrice(n) getDiscountValue(o) getBaseValue

  20. Contents Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Implementing Associations Bi-directional associations Associations Layered Architecture

  21. Implementing Associations: Cardinality 0..1 A B 0..1 Associations and attributes are treated the same A b: B ◮ Field can be null public class A { private B b; public B getB() { return b; } public void setB(B b) { this.b = b; } }

  22. Implementing Associations: Cardinality 1 A B 1 ◮ Field may not be null public class A { private B b = new B(); // 1st way of doing it public A(B b) { this.b = b;} // 2nd way public B getB() { // 3rd way if (b == null) {b = computeB();} return b; } public void setB(B b) { if (b != null) {this.b = b;} } }

  23. Interface Collection < E > Operation Description returns false if e is in the collection boolean add(E e) returns true if e is in the collection boolean remove(E e) boolean contains(E e) returns true if e is in the collection Iterator < E > iterator() allows to iterate over the collection number of elements int size()

  24. Implementing Associations: Cardinality * A B * Default: Unordered, no duplicates public class A { private Set<B> bs = new HashSet<B>(); ... } A {ordered} B * public class A { private List<B> bs = new ArrayList<B>(); ... }

  25. Encapsulation problem: getStudents University Student * University dtu = new University("DTU"); .. Set<Student> students = dtu.getStudents();

  26. Encapsulation problem: getStudents University Student * University dtu = new University("DTU"); .. Set<Student> students = dtu.getStudents(); Student hans = new Student("Hans"); students.add(hans); Student ole = dtu.findStudentNamed("Ole"); students.remove(ole); ... Solution: getStudents returns an unmodifiable set public void Set<Student> getStudents() { return Collections.unmodifiableSet(students); }

  27. Encapsulation problem: setStudents University Student * University dtu = new University("DTU"); .. Set<Student> students = new HashSet<Student>(); dtu.setStudents(students);

  28. Encapsulation problem: setStudents University Student * University dtu = new University("DTU"); .. Set<Student> students = new HashSet<Student>(); dtu.setStudents(students); Student hans = new Student("Hans"); students.add(hans); Student ole = dtu.findStudentNamed("Ole"); students.remove(ole); ... Solution: setStudents copies the set public void setStudents(Set<Student> stds) { students = new HashSet<Student>(stds); }

  29. Solution: How to change the association? University Student * public class University { private Set<Student> bs = new HashSet<Student>(); public void addStudent(Student s) {students.add(student);} public void containsStudent(Student s) {return students.contains(s);} public void removeStudent(Student s) {students.remove(s);} } Even better: domain specific methods like registerStudent

  30. Bi-directional associations Person Firma navn: String {read only} navn: String {read only} ansatte arbejdsgiver 0..1 * Implemented as two uni-directional associations Person Firma arbejdsgiver navn: String {read only} 0..1 navn: String {read only} ansatte * → Problem of referential integrity

  31. Referential Integrity p1:Person f1:Firma p2:Person f2:Firma

  32. Referential Integrity: setArbejdsgiver p1:Person f1:Firma p2:Person f2:Firma

  33. Referential Integrity: addAnsatte p1:Person f1:Firma p2:Person f2:Firma

  34. Library application LibraryApp Book 0..1 * ... 0..1 Calendar getDate() * borrowedBooks registerUser(..) addMedium(..) ... 1 0..1 * DateServer User Address 1 * Calendar getDate()

  35. Contents Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

  36. Low Coupling High coupling A B C D E F

  37. Low Coupling High coupling A B C D E F Low coupling A C B E D F

  38. High Cohesion Low Cohesion Person name cpr-number companyName work-address-street work-address-city home-address-street home-address-city

  39. High Cohesion Low Cohesion Person name cpr-number companyName work-address-street work-address-city home-address-street home-address-city High Cohesion Person Address home address name street cpr-number city address Company works at name

  40. Layered Architecture Eric Evans, Domain Driven Design, Addison-Wesley, 2004

  41. Example Vending Machine Two different presentation layers; same application layer ◮ Command line interface Current Money: DKK 5 ◮ Swing GUI 0) Exit 1) Input 1 DKK 2) Input 2 DKK 3) Input 5 DKK 4) Select banana 5) Select apple 6) Cancel Select a number (0-6): Rest: DKK 2 Current Money: DKK 0 Dispensing: Apple

  42. Application Layer VendingMachine «enumeration» Fruit dispensedItem: Fruit currentMoney: int APPLE totalMoney: int BANANA * restMoney: int input(money: int) select(f: fruit) cancel()

  43. Architecture Presentation Layer Presentation Layer VendingMachineUI VendingMachineTextUI Application Layer VendingMachine «enumeration» Fruit dispensedItem: Fruit currentMoney: int APPLE totalMoney: int BANANA restMoney: int * input(money: int) select(f: fruit) cancel()

  44. Presentation Layer: Swing GUI sd:buy apple

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend