exercise 1
play

Exercise 1 public class AlcoholRegulations { public class Student { - PowerPoint PPT Presentation

Exercise 1 public class AlcoholRegulations { public class Student { public boolean canLegallyDrinkAlcohol(Student st){ private String id; switch (st.getUniversity().getCountry()) { private University university; case "Afghanistan": private


  1. Exercise 1 public class AlcoholRegulations { public class Student { public boolean canLegallyDrinkAlcohol(Student st){ private String id; switch (st.getUniversity().getCountry()) { private University university; case "Afghanistan": private int age; return false ; private List<Course> registeredCourses; case "Armenia": ... return true ; } case "Canada.BC": return (st.getAge >= 19); case "Canada.AB": return (st.getAge >= 18); ... } } 1 }

  2. TODO Refactor these classes to reduce their coupling. 2

  3. Exercise 1 public class AlcoholRegulations { P2 public boolean canLegallyDrinkAlcohol(Student st){ switch (st.getUniversity().getCountry()) { P1 case "Afghanistan": return false ; case "Armenia": return true ; case "Canada.BC": 3 return (st.getAge >= 19); case "Canada.AB":

  4. public class Student { private String id; private University university; private int age; private List<Course> registeredcourses; private Country country; //added } Unnecessa sary duplication of da data Still tied to the Student class public class AlcoholRegulations { public boolean canLegallyDrinkAlochol(Student st) { //here we removed the call to getUniversity() switch (st.getCountry()) { case "Afghanistan": return false ; ALTERNATIVE 1 } } } 4

  5. No mor ore coupling right now (o (only one class) ) :) :) public class Student { Bad cohesion :( ( private int age; private String id; private University university; private List<Course> registeredCourses; Still tied to the Student class public boolean canLegallyDrinkAlcohol() { switch ( this .getCountry()) { case "Afghanistan": return false ; case "Armenia": return true ; case "Canada.BC": return ( this .getAge() >= 19); case "Canada.AB": return ( this .getAge() >= 18); } ALTERNATIVE 2 public String getCountry() { return university.getCountry(); } public int getAge() { return age; } 5 }

  6. public interface Person { Limited to peopl ple that have a a link public int getAge(); public University getUniversity(); with a universi sity } public class Student implements Person { private String id; Still so some coupling private University university; (AlcoholRegulation depends on Perso (A son) ) private int age; private List<Course> registeredCourses; More complex design :( ( public int getAge() {...} public University getUnivesity() {...} } public class AlcoholRegulation { public boolean canLegallyDrinkAlcohol (Person p) { switch (p.getUniversity().getCountry()) { case "Afghanistan": Demeter's principle still violated return false ; ALTERNATIVE 3 case "Armenia": return true ; case "Canada.BC": return (p.getAge()>=19); case "Canada.AB": return (p.getAge()>=18); ... } } 6 }

  7. public class Person{ private String id; private int age; private String country; } public class Student extends Person{ private String sid; private University university; Still some me coupling private List<Course> registeredCourse; (A (Alcoh oholRegulation depends on Perso son) ) ... } More complex design :( ( public class AlcoholRegulation { public boolean canLegallyDrinkAlcohol(Person p){ switch (p.getCountry()){ case ... case "Canada.BC": return (p.getAge >= 19); ALTERNATIVE 4 case "Canada.AB": return (p.getAge >= 18); ... } } } 7

  8. public class AlcoholRegulations { public boolean canLegallyDrink(String country, int age){ switch (country) { No more dependencies, , rely on the use Demeter's principle of basic data types. case "Afghanistan": respected return false ; case "Armenia": return true ; case "Canada.BC": return (age >= 19); POSSIBLE SOLUTION case "Canada.AB": return (age >= 18); } } } 8

  9. Exercise 2 – What's wrong here ? public class GameLauncher { protected RiskController controller; protected RiskGame game; private PrintWriter outChat = null ; private BufferedReader inChat = null ; private ChatArea chatter = null ; private Socket chatSocket = null ; private ChatDisplayThread myReader = null ; private int applicationPort; protected String myIPAddress; protected boolean unlimitedLocalMode; private boolean autoplaceallMode; private boolean battleMode; private boolean replayMode; ... 9 }

  10. TODO Design a UML class diagram of this system where you would aim for more cohesion 10

  11. Exercise 2 – What's wrong here ? public class GameLauncher { protected RiskController controller; protected RiskGame game; private PrintWriter outChat = null ; CHAT User INTEFACE private BufferedReader inChat = null ; CHAT User INTEFACE private ChatArea chatter = null ; CHAT User INTEFACE private Socket chatSocket = null ; CHAT NETWORK private ChatDisplayThread myReader = null ; CHAT User INTEFACE private int applicationPort; NETWORK NETWORK protected String myIPAddress; protected boolean unlimitedLocalMode; Game Prope perties Game Prope perties private boolean autoplaceallMode; private boolean battleMode; Game Properties private boolean replayMode; Game Properties ... 11 }

  12. Do not duplicate class sses, you can create se several associations s between classes if needed 12

  13. Use se inheritance when you want to extend a behavior or 13

  14. AGAIN here attributes and associations are expressing pretty much the same thing. T ake a look back at the exercises we did in class on association if you still don't understand why 14

  15. Design pretty okay, still some cohesion problems with chat/network aspects 15

  16. One of the best design gn from the class 16

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