If I have a class which looks like the following:
class A {
private List<B> bs = new ArrayList<B>;
public List<B> getBs() {
return bs;
}
}
It holds a list of object of type B.
Another class makes use of this list and hands one Object of the list to yet another object of class C:
class SomeOtherClass {
private C c;
public void someMethod() {
//code...
ArrayList<B> bs = mA.getBs();
c.setB(bs.get(0));
}
}
Finally, some values of member fields of object b are changed.
class C {
private B b;
public void setB(B b) {
this.b = b;
this.b.ChangeValue();
}
}
This way of course the objects in the original list gets modified, which is what I want.
However, I find it extremely confusing that class C modifies the values of the object in list bs
of class A. For another developer it would be hard to see that this method change the values.
So I think what I do here is considered to be bad practice, isn't it?`
What's a better way of writing such kind of code?