【发布时间】:2016-09-24 04:48:34
【问题描述】:
我没有获取克隆对象并修改属性,而是首先修改了对象的属性,然后返回了它的克隆。在规则方面和性能方面有什么区别吗?此外,有关设计的任何其他建议都会很棒。谢谢。
public class Category implements Cloneable {
private int id;
private String title;
private int totalGames;
// getters and setters
public Category clone() {
try {
return (Category)super.clone();
} catch(CloneNotSupportedException ex) {
return null;
}
}
}
public class CategoryCache {
private static Category category = new Category(0, null, 0);
private CategoryCache() {
}
public static Category getCategory(int id, String title, int totalGames) {
category.setId(id);
category.setTitle(title);
category.setTotalGames(totalGames);
return category;
}
}
while (<100 times>) {
Category category = CategoryCache.getCategory(<var1>, <var2>, <var3>).clone();
arlCategory.add(category); // add to arraylist
}
【问题讨论】:
标签: java design-patterns prototype-pattern