Notes on ‘Refactoring ‘ — 3.3 Encapsulate Collection

Before Re-factoring

class Person{
  Set courses; 
  Set getCourses(){
     return courses; 
  }
  void setCourses(){...}
}


After Re-factoring

class Person{
  Set courses; 
  Set getCourses(){
    return Collections.unmodifiableSet(courses);
  }
  void addCourse(){...}
  void removeCourse(){...}
}

Benefits:

1.
Person will know it when
courses changes.

2. Coupling between clients which call
Person and
courses will be reduced.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.