Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 A Second Look at Classes and Objects - 3 Aggregation.

Similar presentations


Presentation on theme: "Chapter 9 A Second Look at Classes and Objects - 3 Aggregation."— Presentation transcript:

1 Chapter 9 A Second Look at Classes and Objects - 3 Aggregation

2 2 Contents I. Aggregation II. Security Issues with Aggregate Classes III. Avoid Using null References

3 3 I. Aggregation Aggregation occurs when an instance of a class is a field in another class. In real life, objects are frequently made of other objects. A house is made of door objects window Objects wall objects … It is the combination of all these objects that makes a house object.

4 4 The Course Class An example: We need an object to represent a course that you are taking in college. A Course class hold the following information: The course name The instructor's last name, first name, and office number The textbook's title, author, and publisher We can put fields for each of these items in the Course class. However, a good design principle is to separate related items into their own classes.

5 5 The Course Class An Instructor class holds the instructor-related data: The instructor's last name, first name, and office number A TextBook class holds the textbook-related data: The textbook's title, author, and publisher A Course class holds: The course name An Instructor object A TextBook object

6 6 The Course Class UML class diagram for the Instructor class and TextBook class:

7 7 The Course Class UML diagram for the Course class. Notice that the Course class has an Instructor object and a TextBook object.

8 8 The Course Class Making an instance of one class a field in another class is called object aggregation. The word aggregation means “a whole which is made of constituent parts”. The Course class is an aggregate class because it is made of constituent objects. “Has a” relationship When an instance of one class is a member of another class, it is said that there is a “has a” relationship between class.

9 9 The Course Class The “has a” relationship is sometimes called a whole-part relationship because one object is part of a greater whole. The relationships that exist among the Course, Instructor, and TextBook classes can be described as follows: The course has an instructor. The course has a textbook.

10 10 The Course Class Aggregation in UML diagram: Aggregation is shown in UML class diagram by connecting two classes with a line that has an open diamond at one end. The diamond is closet to the class that is the aggregate.

11 11 The Course Class

12 12

13 13

14 14

15 15 The Course Class

16 16 The Course Class

17 17 II. Security Issues with Aggregate Classes When writing an aggregate class, we should be careful not to unintentionally create “security holes” that can allow code outside to modify private data inside the class. Perform Deep Copies When Creating Field Objects: Return Copies of Field Object, Not the Originals

18 18 Perform Deep Copies When Creating Field Objects An aggregate object contains references to other objects. When we make a copy of the aggregate object, it is important that you also make copies of the objects it references. This is known as a deep copy. If we make a copy of an aggregate object, but only make a reference copy of the objects it references, then we have performed a shallow copy.

19 19 Perform Deep Copies When Creating Field Objects Deep Copies public Course(String name, Instructor instr, TextBook text) { //Assign the courseName courseName = name; //Create a new Instructor object, passing //instr as an argument to the copy instructor. instructor = new Instructor(instr); //Create a new TextBook object, passing //text as an argument to the copy instructor. textBook = new TextBook(text); }

20 20 Perform Deep Copies When Creating Field Objects Bad constructor in the aggregation context public Course(String name, Instructor instr, TextBook text) { //Assign the courseName courseName = name; instructor = instr; textBook = text; } In this example, the instructor and textBook fields are merely assigned the address of the objects passed into the constructor.

21 21 Perform Deep Copies When Creating Field Objects This can cause problems because there may be variables outside the Course object that also contain references to these Instructor and TextBook objects. These outside variables would provide direct access to the Course object's private data. Why was a deep copy not also done for the courseName field? This is permissible because String objects are immutable. An immutable object does not provide a way to change its contents.

22 22 Return Copies of Field Object, Not the Originals When a method in the aggregate class returns a reference to a field object, return a reference to a copy of the field object. public Instructor getInstructor() { return new Instructor(instructor); } Bad method in the aggregation context public Instructor getInstructor() { return instructor; //WRONG! Causes a // security hole } Any variable that receives the address can then access the Instructor object. This means the code outside the Course object can change the values held by the Instructor object.

23 23 Return Copies of Field Object, Not the Originals Note: It is permissible to return a reference to a String object, even if the String object is a private field. This is because String objects are immutable.

24 24 III. Avoid Using null References By default, a reference variable that is an instance field is initialized to the value null. This value indicates that the variable does not reference an object. We cannot use it to perform an operation that would require the existence of an object. For example, a null reference variable cannot be used to call a method.

25 25

26 26 III. Avoid Using null References FullName name = new FullName(); //Get the length of the full name len = name.getLength(); This code will crash because the getLength method is called before the name object's fields are made to reference String objects.

27 27 III. Avoid Using null References One way to prevent the program from crashing: public int getLength() { int len = 0; if(lastName != null) len += lastName.length(); if(firstName != null) len += firstName.length(); if(middleName != null) len += middleName.length(); return len; }

28 28 III. Avoid Using null References Another way to handle this problem is to write a no-arg constructor that assigns values to the reference fields. public FullName() { lastName = “”; firstName = “”; middleName = “”; }


Download ppt "Chapter 9 A Second Look at Classes and Objects - 3 Aggregation."

Similar presentations


Ads by Google