public class MyClass {
private List<String> myList;
public List<String> getMyList() {
return myList;
}
// Other code omitted for clarity
}
You may think: “what’s wrong with that?”, the answer is that you are breaking your object’s encapsulation by publishing access to its internal state. Worse than that, any client code can now modify your object’s state without accessing your object's interface. That’s bad!
The solution is simple: use Collections.unmodifiableList(..):
public class MyClass {
private List<String> myList;
public List<String> getMyList() {
return Collections.unmodifiableList(myList);
}
// Other code omitted for clarity
}
Now, if any client calls your code, and wants to add a new element to the list:
List<String> myList = myClass.getMyList();
myList.add("Hello");
then it’ll cause a java.lang.UnsupportedOperationException and your client will know to leave your code alone. To change its copy of the list the client must do:// Create a new modifiable copy List… which means everyone’s happy: your object’s encapsulation is intact and the client’s got its own list to play with...copy = new LinkedList (myClass.getMyList()); copy.add("Hello");
No comments:
Post a comment