Class Relationships
In systems with multiple classes, it can become difficult to keep track of relationships e.g. the Student class requires the Course class to work There are many ways classes can interact Details of the interactions are part of the design
Class Relationships Three of the most common relationships: Dependency: A uses B Aggregation: A has-a B Inheritance: A is-a B Today: dependency and aggregation Inheritance later
Dependency It’s very common for one class to use another methods use a class to temporarily store/manipulate information an instance variable gets a new value by using another class arguments are passed of another class type … any case where a class needs another class to compile or run
Dependency A dependency exists when one class relies on another in some way, usually by invoking the methods of the other We don't want numerous or complex dependencies among classes in general – we want to minimize dependencies Nor do we want complex classes that don't depend on others A good design strikes the right balance
Dependency Some dependencies occur between objects of the same class A method of the class may accept an object of the same class as a parameter For example, the concat method of the String class takes as a parameter another String object str3 = str1.concat(str2); This drives home the idea that the service is being requested from a particular object
Dependency Example public class Coin { char face; \\ ‘h’ for heads, ‘t’ for tails …… contstructors, methods, etc…… public void flip() { Random rg = new Random(); DEPENDENCY! int rand = rg.nextInt(2); if(r==0) face = ‘h’; else face = ‘t’; }
Aggregation An aggregate is an object that is made up of other objects Therefore aggregation is a has-a relationship A bike has a two wheels In software, an aggregate object contains references to other objects as instance data The aggregate object is defined in part by the objects that make it up This is a special kind of dependency – the aggregate usually relies on the objects that compose it
Aggregation Example public class Wheel { private int diameter; Wheel(int diameter) { this.diameter = diameter; } public int getDiameter() { return diameter; } public int getCircumference() { return Math.pi*diameter; }
Aggregation Example public class Bike { private Wheel frontwheel; private Wheel backwheel; ….other instance variables…. Bike(int frontsize, int ibacksize) { this.frontwheel = new Wheel(frontsize); this.backwheel = new Wheel(backsize); } …..other methods } -other methods may use wheel size… -for example to compute distance traveled -Note: A Bike cannot be created without Wheel objects
Graphical Representations UML – Unified Modelling Language general methods to model/design/document software and other structures commonly used to design object oriented systems The are several different types of UML diagrams … including “class diagrams”
Class Diagrams Diagrams that are used to describe classes and class relationships Represents: classes: instance variables, methods (and relevant types and arguments) class relationships: dependency and aggregation…
Drawing a Class Some details occasionally omitted (for clarity) Not Java syntax – language independent
Class Relationships Other arrows illustrate different relationships You should be able to read these It is up to you if you find them useful in your own designs
Design and UML A full UML diagram gives a lot of information about the design creating one requires very careful planning Probably too detailed for initial planning could be done for low-level design or documentation Often, details will change during implementation – but don’t plan it that way Our blackjack design isn’t finished yet….