32
Fall 2007 CSE 115/503 Introduction to Computer Scien ce for Majors I 1 End of Week Five! • Questions? • First part of essay assignment (choosing a topic) is posted on the website. Check it out! • Mentors You have until next Friday. • Lab 4

Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 End of Week Five! Questions? First part of essay assignment (choosing a topic) is posted

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 1

End of Week Five!

• Questions?

• First part of essay assignment (choosing a topic) is posted on the website. Check it out!

• Mentors You have until next Friday.

• Lab 4

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 2

Overview of today’s topics

• Review of composition relationship.• Details of code elements of the composition

relationship in code.• Introduction to the association relationship.• If time permits:

– mutator methods

– accessor methods

– an alternate way to code the association relationship

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 3

Composition

• A whole-part relationship (e.g. Dog-Tail)

• Whole and part objects have same lifetime– Whole creates instance of part in its constructor

• In Java code, involves 3 changes to whole class:– Declaration of instance variable of part class/type– Instantiation of part class in whole class constructor– Assignment of new part instance to instance variable

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 4

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 5

Important points about composition

• Whole has responsibility for creating its parts (which is why instantiation of parts happens in constructor of whole).

• Whole can communicate with parts. This is why an instance variable is declared: to establish a name for the newly created object.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 6

And now

the gory details

and

vocabulary review

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 7

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Class definition is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 8

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Instance variable name is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 9

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Instance variable declaration is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 10

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Access control modifiers are shown in green:

Note that access control modifier of _tail is private, not public.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 11

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Constructor definition is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 12

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Header of constructor definition is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 13

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Access control modifier in header of constructor definition is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 14

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Name of constructor in header of constructor definition is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 15

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Parameter list in header of constructor definition is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 16

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Instantiation of class Tail is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 17

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

‘new’ operator in instantiation of class Tail is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 18

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Use of constructor in instantiation of Tail class is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 19

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Argument list in instantiation of class Tail is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 20

Dog – Tail example in Java

public class Dog {

private Tail _tail;

public Dog() {

_tail = new Tail();

}

}

Assignment of new Tail instance to instance variable is shown in green:

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 21

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.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 22

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.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 23

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.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 24

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).

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 25

Dog – Collar example in Java

public class Dog {

private Collar _collar;

public Dog(Collar collar) {

_collar = collar;

}

}

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 26

UML

• See Eclipse example

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 27

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).

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 28

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).

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 29

Lifetime (continued)

This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 30

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.

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 31

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).

Fall 2007 CSE 115/503 Introduction to Computer Science for Majors I 32

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.