I've been thinking about this for a while during my past development.
I am kind of against doing such things, I've normally declared a separate method that explicitly handle the logic.
E.g.
If a Class has a method which return a list of objects, I think it is a bad idea to modify this collection directly via its getter method.
Public Class ReportGenerator{
private List<Configurations> configurations;
List<Configuration> getConfigurations (){
return this.configurations;
}
}
I think by doing the following is bad practice:
getConfigurations().remove(1); //remove a configuration via a getter
I would use the following appoach:
Public Class ReportGenerator{
private List<Configurations> configurations;
public List<Configuration> getConfigurations (){
return Collections.unmodifiableList(this.configurations);
}
public void removeConfiguration(int index) //logic explicitly handle the remove
{
this.configurations.remove(index);
}
}
However there is one more thing just comes to my mind is that what if Configuration object also has getter and setter, then you can't prevent others by doing
reportGenerator.getConfigurations().get(0).settTitle("test");
They can still change the state of Configuration object by using the getter method of ReportGenerator, which I think it shouldn't allow that. But if we want to prevent this we would have to declare more methods in the ReportGenerator class to forward the call to the Configuration object, and use defensive copy.
So my question is that if you have a object A which contains another object B using composition and they both have getter and setter, do you prefer to change object B's state via A's getter method? E.g.
A.getB().setTitle("");
or do you prefer to add method to A to change B's state (basically forwarding call to B)
E.g.
A.setBTitle("");
and internally in this method, A call B.setTitle("");
Sorry about the long question, I think I am not really sure what exactly I want to ask. Hope you can understand. :P