- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
存储 Collection<MyItem>
的最佳方式是什么? ?该集合对于当前用户来说实际上是静态的。每个用户只能看到他们的 Collection 。 MyItem
项目工具 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<MyItem>
其他地方,但后来我无法访问私有(private) MyItem
来自 CurrentMyItems
的方法:
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*/
}
或者在MyItem
里面类(见注释掉的选项):
public class MyItem implements IItem {
/*
private final static List<IItem> allItemsStaticList = new ArrayList<>();
private final static Map<Integer, IItem> allItemsStaticMap = new HashMap<>();
private final List<IItem> allItemsList; //
private final static Map<Integer, IItem> allItemsMap;
*/
/*implemented methods*/
}
allItemsStaticList
- 存储所有项目的静态列表。似乎内存效率高,但如果我将来需要存储单独的 MyItems 集合怎么办?这不太可能,但仍然......
allItemsList
- 同一类有两个不同的功能。要么是
存储单个项目,在这种情况下 allItemsList/Map = null;
或者
allItemsList = new ArrayList<>();
,而其他字段为空。 这看起来没问题,但它违反了最小惊喜原则。
存储 MyItemCollection
的方法更自然?
此外,我应该存储 Item
吗?作为 Map
或 List
鉴于MyItem myItem = getMyItemByID(int id);
是访问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
}
但是代价是什么?.. 这个类违反了单一责任原则。
结论 - 在大多数情况下,Item 实例的 Collection 应该存储在 Item 类之外。
最佳答案
在 OOP 中,对象的数据元素也称为对象的属性。因此,您应该问问自己,项目集合是否是项目的属性。
例如,当您假设您的项目是学生时。您会说学生列表是学生的属性吗? -- 可能不是,因为学生列表不是学生的一部分。相反,学生是学生列表的一部分。
由于学生列表不是现实生活中学生的属性,我不会在代码中对其进行不同的建模,只是为了使其在技术上更优雅。
你的类的设计应该由你正在工作的域的结构驱动。当你需要决定把属性放在哪里时,不要问“把它放在这里是否有意义,因为我的特性编程语言报价?”但要问“这个属性在我的域中属于哪里?”。
关于java - 在 `Collection <MyObject>` 内部和外部 `MyObject` 类之间进行选择 [Java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940642/
我对 PHP 中的对象克隆有疑问。我知道克隆创建了一个“深拷贝”,因为创建了一个新对象,其变量初始化为克隆它的对象中相应变量的值。然而,as discussed here ,这意味着任何引用变量都将引
我有一个有点难以解释的合法 JS 问题。请原谅我莫名其妙的说话方式,努力把握大意。 :P 在 JS 中对象可以有属性。您可以将功能分配给属性。您可以重新定义对象的属性而不改变其其他属性。 var my
我有这种情况,我在 C# 中使用的 web 服务方法返回一个业务对象,当使用以下代码调用 web 服务方法时,我在引用中得到异常“无法将类型为 ContactInfo 的对象转换为类型 Contact
我试图进入在 ScalaDays 2013 上展示的新 Scala Pickling 库:Scala Pickling 我真正缺少的是一些如何使用库的简单示例。 我明白我可以像这样腌制一些物体并再次解
这个问题已经有答案了: Which is more effective: if (null == variable) or if (variable == null)? [duplicate] (9
这行代码做了什么? MyObject *objectOne = (MyObject *)recognizer.view; 我对 (MyObject *) 感到困惑。 它是否有效地将 recognize
我遇到了一个具有不可变属性的类: MyObject[] allObjs 属性初始化如下: List objs = createAllMyObjects(); allObjs = objs.toArra
这是我的代码: Set> mySet = getSetOf(MyObject.class); public Set getSetOf(Class setClass) { Set set =
我在面试时被问到这个问题。不能使用 Google Guava 或 MultiMap。我有课 public class Alpha { String company; int loc
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Sorting a collection of objects Sorting an ArrayList of Co
我想根据对象属性对 ArrayList 中的一些对象进行排序。该对象具有: public class Minterm { String minTerm; char flagTick;
这个比那个好吗? 这甚至是一个有效的问题吗? 我最近被告知 MyObject.DoSomething()已经过时了,服务方式是首选。那正确吗? 例子: public class Policy : IC
这个问题在这里已经有了答案: Why can't assign I to ? (2 个答案) What are the risks of explicitly casting from a lis
MyObject obj = new MyObject(); 我知道“new”关键字将调用构造函数并初始化托管堆中的对象。 我的问题是 CLR 如何执行以下操作。 CLR 如何执行上述行? CLR 如
如何转换 Flux直接到Mono> ? 我正在寻找相当于 Single> single = observable.toList()来自 RxJava。 使用阻塞运算符,我可以这样做: val just
在我的 Scalding Flow 中,我有一个 TypedType[Set[MyObject]]。如何将其展平为 TypedType[MyObject]? 最佳答案 pipe.flatMap(ide
我有很多 resharper 警告将我的参数从常规数组更改为相同类型的 IEnumerable,所以它会改变这个: public Interface IGenerator { voi
存储 Collection 的最佳方式是什么? ?该集合对于当前用户来说实际上是静态的。每个用户只能看到他们的 Collection 。 MyItem项目工具 IItem : public inter
我目前正在练习/学习 Ruby 中的单例方法和单例类,我遇到了一个疑问。鉴于: myobject = Object.new 这样做有什么区别吗: def myobject.mymethod end 然
类似于this question但使用对象而不是指针。 如果我有如下代码 Foo f; vector vect; vect.push_back(f); vect.erase(vect.begin())
我是一名优秀的程序员,十分优秀!