Download presentation
Presentation is loading. Please wait.
Published byAubrie Hutchinson Modified over 9 years ago
1
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1
2
Agenda Announcements – Cell phones / laptops off & away / Name signs out Last time –Calling methods local variables instance variables –‘this’ connection between stack and heap Today –Our second relationship: Association –Methods with parameters
3
Method call/invocation When method is called, body of method is carried out. Local variables of method are allocated space in invocation record / stack frame. Invocation record is on runtime stack. Invocation record is the entire context for the execution of the method
4
Revisiting Clifford –dog-tail is one relationship –dog-collar is another
5
Another relationship One collar for its life…? A dog has-a tail; the tail is a part of the dog. We need something different to model the relationship between a dog and a collar.
6
Association No necessary lifetime link Two implementations: –The first is very similar to composition, but differs in one crucial respect: where the target class is instantiated. –The second, which decouples lifetimes completely, is a bit more complex but also more flexible.
7
Revisiting Clifford Dog-Tail relationship is COMPOSITION –Dog takes responsibility for creating a Tail Dog-Collar relationship is ASSOCIATION –Dog takes NO responsibility for creating Collar
8
First implementation 3 changes to source class: 1.Declaration of instance variable 2.Assignment of existing instance to the instance variable 3.Parameter of constructor is of same type as instance variable 1 2 3
9
Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } 1 2 3
10
UML See Eclipse example
11
Essence of association one object can communicate with another object there is no necessary lifetime link between the two objects the objects in the relationship can change over time
12
Lifetime issue in constructor implementation the source class (Dog) does not create an instance of the target (Collar) class there is a lifetime dependency: the target (Collar) object must exist before the source (Dog) object can be created: – a Collar object must be provided in the constructor of the Dog object.
13
This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship. Lifetime issue (continued)
14
Changing a property value 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” method.
15
Association via constructor and mutator method public class Dog { private Collar _collar; public Dog(Collar c) { _collar = c; } public void setCollar(Collar c) { _collar = c; }
16
Constructor/mutator similarity Similarities: both set the value of an instance variable Differences: constructor sets value of an instance variable when the class is instantiated mutator sets the value of an instance variable after the object already exists constructor initializes ALL instance variables mutator sets the value of just one instance variable public class Dog { private Collar _collar; private Sweater _sweater; private Tail _tail; public Dog(Collar c, Sweater s){ _collar = c; _sweater = s; _tail = new Tail(); } public void setCollar(Collar c){ _collar = c; } public void setSweater(Sweater s){ _sweater = s; }
17
Retrieving a property value Suppose an object wants to let other objects know the value of one of its properties? How can we do that? –We can define an “accessor” method.
18
accessor method public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } public void setCollar(Collar collar) { _collar = collar; } public Collar getCollar() { return _collar; }
19
accessor/mutator differences in function public void setCollar(Collar collar){ _collar = collar; } public Collar getCollar() { return _collar; } Information flowing in to method Information flowing out from method
20
public void setCollar (Collar collar) a.c.m. r.t.s. name parameter list public Collar getCollar () a.c.m. r.t.s. name parameter list a.c.m. = access control modifier r.t.s. = return type specification void = no value is returned by method (note difference with constructors: no r.t.s.) accessor/mutator differences in form
21
Shape s1 = new Shape(java.awt.Color.BLUE); Shape s2 = new Shape(java.awt.Color.RED); public class Shape { private java.awt.Color _color; public Shape(java.awt.Color c) { _color = c; }... } Example 1
22
Shape s1 = new Shape(java.awt.Color.BLUE); Shape s2 = new Shape(java.awt.Color.RED); Example 1 s1 Shape _color s2 BLUE Shape _color RED
23
public class Shape { private java.awt.Color _color; public Shape(java.awt.Color c) { _color = c; } public java.awt.Color getColor() { return _color; } public void setColor(java.awt.Color c) { _color = c; }
24
Shape s1 = new Shape(java.awt.Color.BLUE); Shape s2 = new Shape(java.awt.Color.RED); s2.setColor(s1.getColor()); Example 1 s1 Shape _color s2 BLUE Shape _color RED
25
Result? Both shapes have the same color (java.awt.Color.BLUE). This is OK.
26
Dog fido = new Dog(new Collar()); Dog dino = new Dog(new Collar()); Example 2 fido Dog _collar dino Dog _collar
27
Dog fido = new Dog(new Collar()); Dog dino = new Dog(new Collar()); dino.setCollar(fido.getCollar()); Example 2 fido Dog _collar dino Dog _collar ???
28
Result? Both dogs have the same collar. ?!? Second collar is “lost”. :-(
29
What could we do instead? Try to express what the basic problem is, and what we could do instead, in plain English.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.