1 / 13

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. 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.

roza
Download Presentation

CSE115: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu

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

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

  4. First implementation 3 changes to source class: • Declaration of instance variable • Assignment of existinginstance to the instance variable • Parameter of constructor is of same type as instance variable 1 2 3

  5. Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } } 1 3 2

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

  7. Lifetime issue inconstructor 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.

  8. Lifetime issue (continued) This occurs in this particular implementation of the relationship, but is not an essential characteristic of the relationship.

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

  10. Association relationship(take 2) • We’ve seen one implementation of “knows a” – using a parameter in constructor: public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } } • Now we will see a more flexible implementation.

  11. Association viaconstructor and mutatormethod public class Dog { private Collar _collar; public Dog(Collar c) { _collar = c; } public void setCollar(Collar c) { _collar = c; } }

  12. Constructor/mutator similarity Similarity: both set the value of the instance variable. Difference: constructor sets value when Dog object is created mutatorchanges the value at some later point in time public class Dog { private Collar _collar; public Dog(Collarc) { _collar = c; } public void setCollar(Collarx) { _collar = x; } }

  13. On blackboard object diagram & memory diagram for: clifford.CollarsmallCollar; smallCollar = new clifford.Collar(); clifford.Dogfido; fido = new clifford.Dog(smallCollar); clifford.CollarmediumCollar; mediumCollar = new clifford.Collar(); fido.setCollar(mediumCollar);

More Related