我有這樣的代碼:分手長切換
public void addSkillXp(SkillType attribute, int value) {
final SkillDatabaseEntity skillDatabaseEntity = skillValueCache.getEntity();
switch (attribute) {
case TWO_HANDED_CRUSHING_WEAPONS:
skillDatabaseEntity.setTwoHandedCrushingWeaponsXp(value);
skillMapper.addTwoHandedCrushingWeaponsXp(userEntity.getId(), value);
break;
case ONE_HANDED_CRUSHING_WEAPONS:
skillDatabaseEntity.setOneHandedCrushingWeaponsXp(value);
skillMapper.addOneHandedCrushingWeaponsXp(userEntity.getId(), value);
break;
case TWO_HANDED_AXES:
skillDatabaseEntity.setTwoHandedAxesXp(value);
skillMapper.addTwoHandedAxesXp(userEntity.getId(), value);
break;
case ONE_HANDED_AXES:
skillDatabaseEntity.setOneHandedAxesXp(value);
skillMapper.addOneHandedAxesXp(userEntity.getId(), value);
break;
case THROWING_WEAPONS:
skillDatabaseEntity.setThrowingWeaponsXp(value);
skillMapper.addThrowingWeaponsXp(userEntity.getId(), value);
break;
case FISTFIGHT:
skillDatabaseEntity.setFistfightXp(value);
skillMapper.addFistfightXp(userEntity.getId(), value);
break;
...
}
的開關推移更多的20例該SkillDatabaseEntity是一個簡單的DAO類:
public class SkillDatabaseEntity {
private int twoHandedCrushingWeaponsXp;
private int oneHandedCrushingWeaponsXp;
private int twoHandedAxesXp;
private int oneHandedAxesXp;
private int throwingWeaponsXp;
private int fistfightXp;
private int longswordsXp;
private int shortswordsXp;
private int polearmsXp;
private int daggersXp;
private int longbowsXp;
private int showrtbowsXp;
private int crossbowsXp;
private int lightArmorXp;
private int heavyArmorXp;
private int robeArmorXp;
private int armorlessDefenseXp;
private int shieldDefenseXp;
private int staffsXp;
private int wandsXp;
private int spectresXp;
private int scavengingXp;
private int cookingXp;
public int getTwoHandedCrushingWeaponsXp() {
return twoHandedCrushingWeaponsXp;
}
public void setTwoHandedCrushingWeaponsXp(int twoHandedCrushingWeaponsXp) {
this.twoHandedCrushingWeaponsXp = twoHandedCrushingWeaponsXp;
}
public int getOneHandedCrushingWeaponsXp() {
return oneHandedCrushingWeaponsXp;
}
public void setOneHandedCrushingWeaponsXp(int oneHandedCrushingWeaponsXp) {
this.oneHandedCrushingWeaponsXp = oneHandedCrushingWeaponsXp;
}
public int getTwoHandedAxesXp() {
return twoHandedAxesXp;
}
public void setTwoHandedAxesXp(int twoHandedAxesXp) {
this.twoHandedAxesXp = twoHandedAxesXp;
}
public int getOneHandedAxesXp() {
return oneHandedAxesXp;
}
public void setOneHandedAxesXp(int oneHandedAxesXp) {
this.oneHandedAxesXp = oneHandedAxesXp;
}
public int getThrowingWeaponsXp() {
return throwingWeaponsXp;
}
public void setThrowingWeaponsXp(int throwingWeaponsXp) {
this.throwingWeaponsXp = throwingWeaponsXp;
}
...
}
我需要添加removeSkillXp但真正要避免這種巨大的交換機並且如果我在這裏我想提高老scwitch了。我怎樣才能做到這一點?
我有什麼作爲計劃是創建這樣一個新的類:
SkillModifier:
- increaseExperience(UserEntity,值)
- decreaseExperience(UserEntity,值)
- getExperience (UserEntity,value)
- getSupportedSkillType()
而創建這個類在開關任何情況下的一個實例,添加實例到地圖(可以使用Spring DI輕鬆完成),並使用這樣的:
public void addSkillXp(SkillType attribute, int value) {
skillMap.get(attribute).increaseExperience(userEntity, value);
}
能否這項工作?還是有更好的模式?
不幸的是,數據庫類是必須使用數據庫框架MyBatis。 :S如果沒有數據庫課程,整個事情將成爲「走在公園裏」。 –