装饰模式,EnumMap Populating不起作用。为什么?

时间:2017-04-09 10:06:46

标签: java design-patterns decorator enum-map

我想让我的角色扮演一个角色(像司机,枪手等......)与装饰设计模式。不是每个角色都需要所有技能,只有其中一些(例如,合伙人在犯罪中只需要初学者) 。  但是当我想创建不同的角色时,程序会崩溃。(我随后根据用户的决定将我的角色提供给精确的装饰者。他选择角色的成员)作为一个例子,这里我只需要战略技能来自会员类(因为犯罪伙伴只使用那个)。但是当我创建EnumMap时,它无法正常工作。如何解决这个问题?

///会员类

import java.util.*;

public class Member implements Partner{
    String Name;

    int Share;
    //Creating the enum for the skills
    public static enum Skill {
        DRIVE, VEHICLE, ACCURACY, WEAPONS, REFLEX, STRATEGY, CHARISMA, HACKING, SPEED, STEALTH;

        public static Integer valueOf(Skill skill) {
            // TODO Auto-generated method stub
            return Member.skills.get(skill);
        }
    }
    //Creating an EnumMap to store the values and the names of the skills
    static Map<Skill, Integer> skills = new EnumMap<Skill, Integer>(Skill.class);
    Random r = new Random();
    //Constructor
    public Member(String Name) {
        this.Name = Name;

        skills.put(Skill.DRIVE, r.nextInt(100));
        skills.put(Skill.VEHICLE, r.nextInt(100));
        skills.put(Skill.ACCURACY, r.nextInt(100));
        skills.put(Skill.WEAPONS, r.nextInt(100));
        skills.put(Skill.REFLEX, r.nextInt(100));
        skills.put(Skill.STRATEGY, r.nextInt(100));
        skills.put(Skill.CHARISMA, r.nextInt(100));
        skills.put(Skill.HACKING, r.nextInt(100));
        skills.put(Skill.SPEED, r.nextInt(100));
        skills.put(Skill.STEALTH, r.nextInt(100));
    }

    public Map<Skill, Integer> getSkills() {
        return skills;
    }
    public Integer getSkill(Member.Skill skill) {
        return (Integer) skills.get(Skill.valueOf(skill));
    }

    public void setSkills(String skillname, int number) {
        skills.put(Skill.valueOf(skillname), number);
    }
}

///合作伙伴界面

public interface Partner {
    //Creating the enum for the skills
    public static enum Skill {
        DRIVE, VEHICLE, ACCURACY, 
WEAPONS, REFLEX, STRATEGY, CHARISMA, HACKING, SPEED, STEALTH;
    }


}

/// RoleDecorator类

public class RoleDecorator implements Partner {
    protected Partner DecoratedPartner;
    public RoleDecorator(Partner DecoratedPartner){
        this.DecoratedPartner=DecoratedPartner;
    }   

}

//// PartnerInCrime Class这里有些不好......

import java.util.*;



public class PartnerInCrime extends RoleDecorator {
    Member.Skill STRATEGY;
    Random r = new Random();

     Map<Member.Skill, Integer> skill=new EnumMap<Member.Skill, Integer>(Member.Skill.class);
    public PartnerInCrime(Partner DecoratedPartner) {
        super(DecoratedPartner);
        skill.put(STRATEGY, Member.Skill.valueOf(STRATEGY));
    }



    public Map<Member.Skill, Integer> getskill() {
        return skill;
    }
}

0 个答案:

没有答案