class A{
private List<T> list;
public int getListSize(){
return list.size();
}
}
class B{
private A objectA;
public int getSizeOfListInA{
return objectA.getListSize();
}
}
class C{
B objectB;
// here I need to know the size of the list
}
I'm oversimplifying here, but I was wondering if this is the right way to write proper code - suppose class C needs to know the size of the list. If there was another class D holding object of class C in it, and D wanted to know the size of the list from A, I'd need another getSizeOfTheList method in class C. So it's a lot of code duplication - three methods that generally do the same thing, simply because a class that is level up wants to know its internal stuff. Of course it would be best if C didn't have to know the list size, but sometimes it makes sense.
Edit:
If A was immutable class, then maybe it would better to have getObjectA instead in class B? Suppose I want to make available all objectA methods to the clients of class B - by including getObjectA method I don't need to rewrite each of them in B. The client would simply first call getObjectA and then call its method.
The question is obviously whether I really want clients to access all methods of A. And it would be harder for clients of B to use my API. So either me or my clients will have easier life.