2016-04-20 50 views
0

我有這樣的代碼:分手長切換

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); 
} 

能否這項工作?還是有更好的模式?

回答

1

整個開關似乎是多餘的,因爲每個案件都做同樣的事情。爲什麼不創建一個抽象類武器,它有一個方法增加XP並且由每個武器類擴展(2h等)。
通過這種方式,實現在抽象類中,其他子類只需調用.increaseXP。

順便說一句,數據庫類似乎是不必要的,只是爲每個玩家保留一個武器列表/數組。

一些更多的背景信息,請參閱:

https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism

+0

不幸的是,數據庫類是必須使用數據庫框架MyBatis。 :S如果沒有數據庫課程,整個事情將成爲「走在公園裏」。 –

1

如果從移動代碼的功能到對象,事情就變得容易多了。

例如:

enum SkillType { 
    TWO_HANDED_CRUSHING_WEAPONS { 
     @Override 
     void updateEntity(SkillDatabaseEntity entity, int value) { 
      entity.setTwoHandedCrushingWeaponsXp(value); 
     } 

     @Override 
     void mapSkill(SkillMapper mapper, int userId, int value) { 
      mapper.addTwoHandedCrushingWeaponsXp(userId, value); 

     } 
    }, 
    ONE_HANDED_CRUSHING_WEAPONS { 
     @Override 
     void updateEntity(SkillDatabaseEntity entity, int value) { 
      entity.addOneHandedCrushingWeaponsXp(value); 
     } 

     @Override 
     void mapSkill(SkillMapper mapper, int userId, int value) { 
      mapper.addOneHandedCrushingWeaponsXp(userId, value); 

     } 
    }, 
    TWO_HANDED_AXES { 
     @Override 
     void updateEntity(SkillDatabaseEntity entity, int value) { 
      entity.setTwoHandedAxesXp(value); 
     } 
     @Override 
     void mapSkill(SkillMapper mapper, int userId, int value) { 
      mapper.addTwoHandedAxesXp(userId, value); 

     } 
    }; 

    abstract void updateEntity(SkillDatabaseEntity entity, int value); 

    abstract void mapSkill(SkillMapper mapper, int userId, int value); 
} 

public void addSkillXp(SkillType skill, int value) { 
    final SkillDatabaseEntity skillDatabaseEntity = skillValueCache.getEntity(); 
    skill.updateEntity(skillDatabaseEntity, value); 
    skill.mapSkill(skillMapper, userEntity.getId(), value); 
} 

這裏addSkillXp已經變成了三行代碼。

另一個優雅的好處是,所有的技能代碼都在同一個enum

+0

那麼,我不想爲我的枚舉添加這樣的功能。我不想讓我的枚舉知道SkillMappers和DAO對象等。 –

+0

@LakatosGyula - 爲什麼? – OldCurmudgeon

+0

凝聚力。枚舉標識一項技能。我不希望它用這個技巧做所有不同的事情。當我開始添加更多功能時,它們可以非常快速地增長。另外我無法控制枚舉的創建。我不能在構造函數中注入/ autowire skillMapper,我只能將它作爲變量傳遞。當我需要另一個映射器/服務進行計算時會發生什麼?變量數會增長很快。3輸入變量有時可以接受,4對我來說是一個很大的問題。 –