一点背景:我正在创造一些游戏。在该游戏中,玩家可以拥有一所房子。房子可能包含家具物品,这些家具可能有自定义纹理。一件家具可能包含不同数量的纹理。一件家具有几个用于纹理的插槽(0 - x)。
上课:
public class House {
private List<Furniture> furniture;
public House() {
furniture = new ArrayList<Furniture>();
}
public List<Furniture> getFurniture() {
return furniture;
}
public void addFurniture(Furniture furniture) {
furniture.add(furniture);
}
}
家具类:
public class Furniture {
private Map<Integer, Texture> textures;
public Furniture() {
textures = new HashMap<Integer, Texture>();
}
public void setTexture(int index, Texture texture) {
textures.put(index, texture);
}
}
在玩家住宅家具管理GUI中,会出现以下情况:
List<Furniture> furniture = house.getFurniture();
// Let's assume that player selected one piece of furniture which he wants to edit
Furniture furniture = .... // This is the one he selected.
// Later on he chooeses a texture and I set it:
furniture.setTexture(some_index, new Texture(...));
问题
从setTexture方法中更新我的数据库是否有意义?从数据保持类中与数据存储区交互是一种好习惯吗?
答案 0 :(得分:1)
根据我的经验,直接在这些java bean中进行更新并不是一个好习惯。您应该使用DAO模式。 通过这种方式,您可以抽象模型上的所有CRUD操作。
有关详细信息,请访问DAO pattern