public static BaseSword CreateSword(SwordEnum type) { if (Dic.ContainsKey(type)) { return(Dic[type]); } lock (Locker) { if (Dic.ContainsKey(type)) { return(Dic[type]); } BaseSword sword; switch (type) { case SwordEnum.木剑: sword = new WoodSword(); break; case SwordEnum.铁剑: sword = new IronSword(); break; case SwordEnum.魔剑: sword = new MagicSword(); break; default: throw new Exception("wrong sword"); } Dic.Add(type, sword); return(sword); } }
public static BaseSword GetSword(SwordEnum type) { Lazy <BaseSword> lazy = null; if (LazyDic.TryGetValue(type, out lazy)) { return(lazy.Value); } throw new Exception("没有该 type"); }