Bifalgorithm post · Computer science one · Composition

How should a class manage owned collections and avoid shallow copies?

How should a class manage owned collections and avoid shallow copies? (Computer science one · Composition) — "We can take this concept further and design our c…

Ideas in this flowchart

  • Designing classes that own collections of other classes
  • A class can own a collection of instances of another class.
  • A Student can own a Set<Course> representing the student's class schedule; this is aggregation rather than strict composition.
  • The design must determine who instantiates the dateOfBirth value.
  • The outside user can construct a LocalDate and pass it to the Student constructor.
  • The outside code can provide the date of birth as a string.
  • Both dateOfBirth designs have advantages and disadvantages that require consideration.
  • A Student constructor may receive a pre-computed Set<Course> for the schedule.
  • Assigning this.schedule = schedule creates a shallow copy because both variables reference the same collection.
  • A course added through either reference changes the same underlying schedule.
  • The schedule should instead be copied into a separate collection.
  • The constructor can use new HashSet<Course>(schedule) to create the copied set.
  • This follows the reusable copy-constructor pattern found throughout the Java Collections library.
Feed/

How should a class manage owned collections and avoid shallow copies?

No cover

Channel

Computer science one

1 page

"We can take this concept further and design our classes to own collections of other classes. For example, we could define a Course class and then update our Student class to own a collection of Course objects representing a student’s class schedule (this type of collection ownership is sometimes referred to as aggregation rather than strict composition).\n\n1 private Set<Course> schedule;\n\nBoth of these updates beg the question: who is responsible for instantiating the instances of dateOfBirth and the schedule? Should we force the “outside” user of our Student class to build the LocalDate instance and pass it to a constructor? Should we allow the outside code to simply provide us a date of birth as a string? Both of these design choices have advantages and disadvantages that have to be considered.\n\nWhat about the course schedule? We could require that a user provide the constructor with a pre-computed Set of courses, but care must be taken. Consider the following (partial) example.\n\n1 public Student(..., Set<Course> schedule) {\n\n2 ...\n\n3 this.schedule = schedule;\n\n4 }\n\nThis is an example of a shallow copy: the instance’s schedule variable is referencing the same collection as the parameter variable. If a change is made, say a course is added, via one of these references, then the change is effectively made for both. If we are going to design it this way, it would be better to make a deep copy of the set of courses:\n\n1 public Student(..., Set<Course> schedule) {\n\n2 ...\n\n3 this.schedule = new HashSet<Course>(schedule);\n\n4 }\n\nThis is the same pattern we described above: almost every data structure in the Java Collections library has a (deep) copy constructor."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.