在`收藏<;MyObject>;`“MyObject”类的内部与外部[Java]

在`收藏<;MyObject>;`“MyObject”类的内部与外部[Java],java,collections,Java,Collections,存储集合的最佳方式是什么?对于当前用户,此集合实际上是静态的。每个用户只能看到他们的集合我的项目项目实现IItem: public interface IItem { public Integer getItemID(); public void setItemID(Integer id); public String getTitle(); public void setTitle(String title); /*more getters an

存储
集合的最佳方式是什么?对于当前用户,此集合实际上是静态的。每个用户只能看到他们的集合<代码>我的项目
项目实现
IItem

public interface IItem {    
    public Integer getItemID();
    public void setItemID(Integer id);
    public String getTitle();
    public void setTitle(String title);
    /*more getters and setters*/
    public IItem parseServerResponse(String response);
    public int postItem(); //posts this IItem to server, return ok ->200, unauth->401, etc
    public IItem findItem(String[] filters);
    /*more advanced methods*/
}
我可以将
Collection
存储在其他地方,但是我不能从
CurrentMyItems
访问私有
MyItem
方法:

public class CurrentMyItems{        
    private final List<IItem> allItemsList;
    public CurrentMyItems(String allItemsServerResponseString){
        JSONArray rawItems = parseResponse(allItemsServerResponseString);
        int arrSize = rawItems.length()+estimateQuantityOfNewItems();
        List<IItem> allItemsList =  new ArrayList<>(arrSize);           
        for (int i = 0; i < Items.length(); i++) {
            allItems.add(i, parseItem(Items.get(i)));
            }               
    }
    /*methods*/
}
allItemsStaticList
-存储所有项目的静态列表。看起来内存效率很高,但如果将来我需要单独存储MyItems集合,该怎么办?这是极不可能的,但仍然

allItemsList
-同一个类有两个不同的函数。要么是

存储单个项目,在这种情况下
allItemsList/Map=null

allItemsList=newarraylist(),而其他字段为空。
这似乎还可以,但它打破了最不令人惊讶的原则

存储
MyItemCollection
的哪种方法更自然

另外,如果
MyItem-MyItem=getMyItemByID(int-id),我应该将
Item
s存储为
Map
还是
List
是访问
MyItem
的主要方式吗


更新

我们可以实现一个
Item
类,这样一个实例既可以保存
Item
实例的集合,也可以保存建模的数据,但不能同时保存两者

public class Item {
    private final Map<Integer, Item> itemsMap;
    private final IntegerProperty itemID;  // private final String[] names;

    public Item(){
        itemsMap = new HashMap<>();
        itemID = null; //names = null;
    }

    private Item(Integer id) {           
            itemsMap= null;
            itemID = new SimpleIntegerProperty(id); //names = new String[1];
        }  

    public Item makeGenericItem(){
        return itemsMap == null ? null : new Item(itemsMap.size());            
    }
  // other methods, including getters and setters
} 
公共类项目{
私人最终地图项目Map;
private final IntegerProperty itemID;//private final字符串[]名称;
公共项目(){
itemsMap=newhashmap();
itemID=null;//name=null;
}
私有项(整数id){
itemsMap=null;
itemID=新的SimpleIntegerProperty(id);//名称=新字符串[1];
}  
公共项makeGenericItem(){
return itemsMap==null?null:新项(itemsMap.size());
}
//其他方法,包括getter和setter
} 
但代价是什么?。。该类违反了单一责任原则


结论-在大多数情况下,项实例的集合应存储在项类之外。

在OOP中,对象的数据元素也称为对象的属性。因此,您应该问问自己,项目集合是否是项目的属性

例如,假设您的项目是学生。你认为学生名单是学生的一种属性吗可能不是,因为学生名单不是学生的一部分。相反,学生是学生名单的一部分

由于学生列表在现实生活中不是学生的属性,所以我不会在代码中对其进行不同的建模,只是为了让它在技术上更优雅


类的设计应该由您所在领域的结构驱动。当您需要决定将属性放在何处时,不要问“由于我的编程语言提供的功能,将其放在此处是否有意义?”而是问“此属性在我的域中属于何处?”。

我可能在此处遗漏了一些内容,但是
Collection
字段的位置如何影响
MyItem
中的私有方法从
CurrentMyItems
的可访问性呢?@ChristophBöhme我想降低私有方法的可见性,如果它只被
MyItem
Collection
使用。我可以通过将
Collection
作为
MyItem
的一个字段来实现这一点。如果我在
MyItem
之外存储
Collection
,我必须将范围至少设置为package private。所以问题是,我是否应该将集合放在项中以降低某些方法的可见性?不要问自己应该将列表放在哪里才能访问该方法,而是根据实例与其他类的关系,实例属于哪里。如果您直接将其放入
MyItem
中,是否有可能
IItem
IItem
,因为
MyItem
将有一个
MyItem
列表,其中有一个
MyItem
列表,该列表具有…@AxelH我同意。这是可以解决的(添加到我的帖子下面的“更新”),但它使代码更难阅读/理解。成本很高,而收益(将方法可见性从包私有更改为私有)很小。
public class Item {
    private final Map<Integer, Item> itemsMap;
    private final IntegerProperty itemID;  // private final String[] names;

    public Item(){
        itemsMap = new HashMap<>();
        itemID = null; //names = null;
    }

    private Item(Integer id) {           
            itemsMap= null;
            itemID = new SimpleIntegerProperty(id); //names = new String[1];
        }  

    public Item makeGenericItem(){
        return itemsMap == null ? null : new Item(itemsMap.size());            
    }
  // other methods, including getters and setters
}