static void Main(string[] args) { // 获取享元工厂 SoldierFactory soldierFactory = SoldierFactory.GetInstance(); // 通过享元工厂获取 Soldier soldier1 = soldierFactory.GetSoldier(StandType.blue); Soldier soldier2 = soldierFactory.GetSoldier(StandType.blue); Soldier soldier3 = soldierFactory.GetSoldier(StandType.blue); Soldier soldier4 = soldierFactory.GetSoldier(StandType.blue); Soldier soldier5 = soldierFactory.GetSoldier(StandType.blue); Console.WriteLine("判断两个士兵是否相同:{0}", object.ReferenceEquals(soldier1, soldier2)); Soldier soldier6 = soldierFactory.GetSoldier(StandType.red); Soldier soldier7 = soldierFactory.GetSoldier(StandType.red); Soldier soldier8 = soldierFactory.GetSoldier(StandType.red); Console.WriteLine("判断两个士兵是否相同:{0}", object.ReferenceEquals(soldier6, soldier7)); soldier1.attack(new Target { TargetName = "A高地", x = 0, y = 1 }); soldier2.attack(new Target { TargetName = "A高地", x = 0, y = 1 }); soldier3.attack(new Target { TargetName = "A高地", x = 0, y = 1 }); soldier4.attack(new Target { TargetName = "A高地", x = 0, y = 1 }); Console.ReadKey(); }
static void Main(string[] args) { var army = new List <Tuple <ISoldier, SoldierState> >(); for (int counter = 0; counter < 1000; counter++) { army.Add(Tuple.Create(SoldierFactory.GetSoldier(typeof(Grunt)), new SoldierState { Health = 100, Weapon = Weapon.Pistol, Ammo = 50 })); } for (int counter = 0; counter < 10; counter++) { army.Add(Tuple.Create(SoldierFactory.GetSoldier(typeof(Commander)), new SoldierState { Health = 150, Weapon = Weapon.Rifle, Ammo = 100 })); } army.Add(Tuple.Create(SoldierFactory.GetSoldier(typeof(Boss)), new SoldierState { Health = 1000, Weapon = Weapon.Bfg9000, Ammo = int.MaxValue })); Debugger.Break(); // Examine army size vs. SoldierFactory size }