Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g.

Similar presentations


Presentation on theme: "Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g."— Presentation transcript:

1 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g. (from chapter 4) a restaurant has-a kitchen. What about its chef? Did she get created when they created the restaurant? What if she was a horrible cook? And we wanted to replace her? Or: a dog and a collar.

2 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I2 An oldie but a goodie One collar for its life… A dog has-a tail; the tail is a part of the dog. We need something different to model a relationship like dog and collar.

3 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I3 Association Also called “knows a”. A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail) No necessary lifetime link We’ll look at two different implementations of “knows a”: –The first we will see today, and is very similar to our implementation of “has a”. –The second, which we will see next time, is a bit more complex but is also more flexible.

4 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I4 First implementation In Java code, the first involves 3 changes to the “knowing” class: –Declaration of instance variable of the “known” class/type (because the “knowing” object will want to communicate with the “known” object). –Assignment of existing “known” instance to the instance variable (because the instance variable must refer to an object). –Parameter of “known” class in “knowing” class constructor (because the creator of an instance of the “knowing” class needs to supply an instance of the “known” class).

5 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I5 Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; }

6 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I6 UML See Eclipse example

7 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I7 Essence of association We use an association relationship when we want to model that one object can communicate with another object (as in the composition relationship) but –there isn’t any lifetime link between the two objects, or –the objects in the relationship can change over time (think of Clifford’s collar).

8 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I8 Lifetime Notice in this first implementation that although the “knowing” class (the Dog) does not create an instance of the “known” class (the Collar), there is a lifetime dependency: the Collar object must exist before the Dog object can be created (since a Collar object must be used in the constructor of the Dog object).

9 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I9 Lifetime (continued) This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship.

10 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I10 Association relationship We’ve seen one implementation of “knows a”: public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } Now we will see a more flexible implementation.

11 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I11 Essence of association We use an association relationship when we want to model that one object can communicate with another object (as in the composition relationship) but –there isn’t any lifetime link between the two objects, or –the objects in the relationship can change over time (think of Clifford’s collar).

12 Fall 2007CSE 115/503 Introduction to Computer Science for Majors I12 Changing the known property We want to be able to set a new value for the property (e.g. give Clifford a new collar). How can we do that? Using a “mutator” or “setter” method.


Download ppt "Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Now What? What if we want to talk to something that was created somewhere else? E.g."

Similar presentations


Ads by Google