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 }
TODO Refactor these classes to reduce their coupling. 2
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":
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
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 }
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 }
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
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
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 }
TODO Design a UML class diagram of this system where you would aim for more cohesion 10
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 }
Do not duplicate class sses, you can create se several associations s between classes if needed 12
Use se inheritance when you want to extend a behavior or 13
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
Design pretty okay, still some cohesion problems with chat/network aspects 15
One of the best design gn from the class 16
Recommend
More recommend