/// <summary> /// 定义一个静态的公开的GetInstance方法供外部得到DH类唯一实例是调用 /// </summary> /// <returns></returns> public static DH GetInstance() { //先判断dh是否已经被实例化,若未被实例化,先加锁保证线程安全 if (dh == null) { lock (lockObject) { //先判断dh是否已经被实例化,若未被实例化,先实例化得到DH类的实例 //保证DH类只被实例化一次 if (dh == null) { dh = new DH(); } } } return(dh); }
/// <summary> /// 创建英雄的静态方法 /// </summary> /// <param name="heroName">英雄名称</param> /// <returns></returns> public static IHero CreateHero(string heroName) { switch (heroName) { case "DH": return(DH.GetInstance()); //通过DH类公开的静态GetInstance方法得到DH类的实例 case "WD": return(new WD()); case "KOG": return(new KOG()); case "POM": return(new POM()); default: return(null); } }