software engineering i 02161
play

Software Engineering I (02161) Week 6 Assoc. Prof. Hubert - PowerPoint PPT Presentation

Software Engineering I (02161) Week 6 Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2013 Contents Class Diagrams Implementing Associations Bi-directional associations Generalisation Notes and Comments


  1. Software Engineering I (02161) Week 6 Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2013

  2. Contents Class Diagrams Implementing Associations Bi-directional associations Generalisation Notes and Comments Layered Architecture State machines Library Application and GUI

  3. 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; } }

  4. Implementing Associations: Cardinality 1 A B 1 ◮ Field may not be null public class A { private B b = new B(); public A(B b) { this.b = b;} public B getB() { if (b == null) {b = computeB();} return b; } public void setB(B b) { if (b != null) {this.b = b;} } }

  5. 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) returns true if e is in the collection boolean contains(E e) allows to iterate over the collection Iterator < E > iterator() number of elements int size()

  6. 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>(); ... }

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

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

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

  10. 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); ... Solution: setStudents copies the set public void setStudents(Set<Student> stds) { students = new HashSet<Student>(stds); }

  11. 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

  12. 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

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

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

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

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

  17. Generalisation Example {abstract} fine and maxBorrowInDays Medium are abstract in Medium and defined differently in Book and Cd. String signature For Book we have 20 DKK and 28 days, String title while for CD we have 40 DKK fine String author and max days for borrowing is 7. Calendar borrowDate Medium(String,String,String) int fine int maxBorrowInDays boolean isOverdue boolean isBorrowed Book Cd Book(String,String,String) Cd(String,String,String) int fine int fine int maxBorrowInDays int maxBorrowInDays Liskov-Wing Substitution Principle ” If S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness). ”

  18. Notes and Comments

  19. Contents Class Diagrams Layered Architecture State machines Library Application and GUI

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

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

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

  23. 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

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

  25. 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

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

  27. 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()

  28. Presentation Layer: Swing GUI public class VendingMachineUI extends javax.swing.JFrame implements java.util.Observer { private VendingMachine vendingMachine = new VendingMachine(10, 10); ... private javax.swing.JButton fiveCrowns = new javax.swing.JButton(); ... private void initComponents() { fiveCrowns.setText("DKK 5"); fiveCrowns.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) vendingMachine.input(5);; } } ) ... }; ... public void update(Observable o, Object arg) { displayState(); } }

  29. Presentation Layer: TextUI public class VendingMachineTextUI implements Observer { VendingMachine vendingMachine; public static void main(String[] args) throws Exception { new VendingMachineTextUI(5,5).mainLoop(System.in, System.out); } public void mainLoop(InputStream in, PrintStream out) throws IOException BufferedReader rs = new BufferedReader(new InputStreamReader(in)); do { showMenu(out); int number = Integer.valueOf(rs.readLine()); processChoice(number, out); } while (number != 0); } private void processChoice(int number, PrintStream out) { switch (number) { case 3: vendingMachine.input(5); break; ... } } public void update(Observable o, Object arg) { NotificationType type = (NotificationType) arg; if (type == NotificationType.CURRENT_MONEY) { System.out.println("Current Money: DKK " + vendingMachine.getCurrentMoney()); return; } } }

  30. Advantages of the separation 1 Presentation layer easily changed 2 Additional presentation layers 3 Automatic tests public void testInputDataSetA() { VendingMachine m = new VendingMachine(10, 10); m.input(1); m.input(2); assertEquals(3, m.getCurrentMoney()); m.selectFruit(Fruit.APPLE); assertEquals(Fruit.APPLE, m.getDispensedItem()); }

  31. Contents Class Diagrams Layered Architecture State machines Library Application and GUI

  32. Example Vending Machine ◮ Actions ◮ Input coins ◮ Press button for bananas or apples ◮ Press cancel ◮ Displays ◮ current amount of money input ◮ Effects ◮ Return money ◮ Dispense banana or apple

  33. Vending Machine: state machine

  34. State transition table event guard state state state Banana selected and Apple selected and Idle (I) not enough money not enough money (B) (A) enough money for dispense banana and dispense banana and dispense banana and banana banana rest money rest money-> I rest money-> I not enough banana -> B -> B money for banana no bananas banana -> I -> I available enough money for dispense apple and dispense apple and dispense apple and apple apple rest money -> I rest money -> I rest money -> I not enough apple -> A -> A money for apple no apples apple -> I -> I available enough money for add money to current dispense banana and add money to current money banana money rest money-> I money enough money for add money to current add money to current dispense apple and money apple money money rest money -> I not enough add money to current add money to current add money to current money money for neither money money money banana nor apple return current money return current money cancel return current money -> I -> I

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