Class Relationships Class Relationships n In systems with

  • Slides: 15
Download presentation
Class Relationships

Class Relationships

Class Relationships n In systems with multiple classes, it can become difficult to keep

Class Relationships n In systems with multiple classes, it can become difficult to keep track of relationships q n e. g. the Student class requires the Course class to work There are many ways classes can interact q Details of the interactions are part of the design

Class Relationships n Three of the most common relationships: q Dependency: A uses B

Class Relationships n Three of the most common relationships: q Dependency: A uses B q Aggregation: A has-a B q Inheritance: A is-a B n Today: dependency and aggregation n Inheritance later

Dependency n It’s very common for one class to use another q q q

Dependency n It’s very common for one class to use another q q q n 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 n A dependency exists when one class relies on another in some way,

Dependency n A dependency exists when one class relies on another in some way, usually by invoking the methods of the other n We don't want numerous or complex dependencies among classes q in general – we want to minimize dependencies n Nor do we want complex classes that don't depend on others n A good design strikes the right balance

Dependency n Some dependencies occur between objects of the same class n A method

Dependency n Some dependencies occur between objects of the same class n A method of the class may accept an object of the same class as a parameter n For example, the concat method of the String class takes as a parameter another String object str 3 = str 1. concat(str 2); n 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

Dependency Example public class Coin { char face; \ ‘h’ for heads, ‘t’ for tails …… contstructors, methods, etc…… public void flip() { Random rg = new Random(); int rand = rg. next. Int(2); if(r==0) face = ‘h’; else face = ‘t’; } } ----- DEPENDENCY!

Aggregation n An aggregate is an object that is made up of other objects

Aggregation n An aggregate is an object that is made up of other objects n Therefore aggregation is a has-a relationship q A bike has a two wheels n In software, an aggregate object contains references to other objects as instance data n The aggregate object is defined in part by the objects that make it up n 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

Aggregation Example public class Wheel { private int diameter; Wheel(int diameter) { this. diameter = diameter; } public int get. Diameter() { return diameter; } public int get. Circumference() { return Math. pi*diameter; } }

Aggregation Example public class Bike { private Wheel frontwheel; private Wheel backwheel; …. other

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 n UML – Unified Modelling Language q q n general methods to

Graphical Representations n UML – Unified Modelling Language q q n 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 q … including “class diagrams”

Class Diagrams n n Diagrams that are used to describe classes and class relationships

Class Diagrams n n Diagrams that are used to describe classes and class relationships Represents: q q classes: instance variables, methods (and relevant types and arguments) class relationships: dependency and aggregation…

Drawing a Class n n Some details occasionally omitted (for clarity) Not Java syntax

Drawing a Class n n Some details occasionally omitted (for clarity) Not Java syntax – language independent

Class Relationships n n n Other arrows illustrate different relationships You should be able

Class Relationships n n n 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 n A full UML diagram gives a lot of information about

Design and UML n A full UML diagram gives a lot of information about the design q n Probably too detailed for initial planning q q n creating one requires very careful 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….