Topic: Return mutable collection or...? |
Print this page |
1.Return mutable collection or...? | Copy to clipboard |
Posted by: bujinwang Posted on: 2003-09-25 08:57 Dear developers, Suppose I have got a class A contains a list of B(components). Here is what I have implemented. code: -------------------------------------------------------------------------------- public class A{ private ArrayList components = new ArrayList(); public void add(B component){ this.components.add(component); } public void remove(B component){ this.components.remove(component); } public int getComponentsCount(){ return components.size(); }} -------------------------------------------------------------------------------- Ok, one of my friend comes in and has another idea of how to implement this: code: -------------------------------------------------------------------------------- public class A{ private ArrayList components = new ArrayList(); public BCollection getComponents(){ return new BCollection(components); }} -------------------------------------------------------------------------------- and Here is his BCollection class: code: -------------------------------------------------------------------------------- public class BCollection{ private ArrayList components = null; public BCollection(ArrayList components){ this.components = components; } public void add(B component){ this.components.add(component); } public void remove(B component){ this.components.remove(component); } public int getComponentsCount(){ return this.components.size(); }} -------------------------------------------------------------------------------- According to him, that way we don't to have "A" around to operate on components. But to me, it's kind of breaking the encapsulation, which shoudl be avoided. What do you guys think? Any strong reason why/why not doing this? Thanks, Bujin Wang |
2.Re:Return mutable collection or...? [Re: bujinwang] | Copy to clipboard |
Posted by: bujinwang Posted on: 2003-09-25 09:03 Dear developers, Suppose I have got a class A contains a list of B(components). Here is what I have implemented.
|
3.Re:Return mutable collection or...? [Re: bujinwang] | Copy to clipboard |
Posted by: rostone Posted on: 2003-09-25 11:39 I think it will be better if he moves arraylist from A to B. As a user, class A doesn't need to care the arraylist. |
4.Re:Return mutable collection or...? [Re: bujinwang] | Copy to clipboard |
Posted by: archonLing Posted on: 2003-09-25 11:47 First rule in programming, KISS --> Keep It Simple & Stupid To me, 1st implemenation gets the job done, and it is clear & easy to understand. 2nd implemenation has a problem, class A has an accessor method, getXXX() without constructor or setXXX(). How does it suppose to work? Let us assume it works, it would return a BCollection object, not a list of Objects. Why making things more complicated than it should be? |
5.Re:Return mutable collection or...? [Re: bujinwang] | Copy to clipboard |
Posted by: bujinwang Posted on: 2003-09-25 22:53 Well, don't assume the A and B and BCollection will compile or work. I use the code to describe me the issue. Please note that A has a lot other operations, not just managing Bs. In that case, is it reasonable to move B managing code to BCollection? Thanks, Bujin wang |
6.Re:Return mutable collection or...? [Re: bujinwang] | Copy to clipboard |
Posted by: floater Posted on: 2003-09-25 23:23 You didn't get the dependency description in your orginal post, and in fact, that's where you and your fellow have confusion. From what you posted, archonLing gave the reason. But I sense there are a lot more behind the scene. You have to straight out the object dependencies first before you put into code. Encapsulation is based on isolation. I recently post a JDBC framework in the technical article area, it has a similar case to this. In that context, I have a class to hold sql parameters, which does only the conversion from primitive types to objects(similar to your fellow's A class), and another class to do the parameter setting in PreparedStatement(simliar to the B class). The reason I use two seperate classes is because sql parameters class is a generic one, doesn't care PreparedStatement portion of the second class. and PreparedStatement doesn't care sql parameters either, it takes just a List. Another approach is refactoring, you first put everything into one big class and then refactor it. Then you can see the dependencies clearly. |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |