public static void Main() { List <Soldier> soldiers = new List <Soldier>(); string input = string.Empty; while ((input = Console.ReadLine()) != "End") { string[] tokens = input.Split(); string soldierType = tokens[0]; Soldier soldier; try { soldier = SoldierFactory.GetSoldier(soldiers, soldierType, tokens); } catch (ArgumentException ex) { continue; } soldiers.Add(soldier); } Console.WriteLine(string.Join($"{Environment.NewLine}", soldiers)); }
// Entry point into console application. static void Main() { SoldierFactory factory = new SoldierFactory(); soldier = factory.GetSoldier("0"); soldier.RenderSoldier("Bob", "Red"); soldier = factory.GetSoldier("0"); soldier.RenderSoldier("Bob", "Blue"); soldier = factory.GetSoldier("1"); soldier.RenderSoldier("Tom", "White"); soldier = factory.GetSoldier("1"); soldier.RenderSoldier("Jerry", "Black"); // Console.WriteLine(soldier); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { //比如,我们现在需要10000个一般士兵,只需这样 SoldierFactory factory = new SoldierFactory(); AK47 ak47 = new AK47(); for (int i = 0; i < 100; i++) { Soldier soldier = null; if (i <= 20) { soldier = factory.GetSoldier("士兵" + (i + 1), ak47, SoldierType.Normal); } else { soldier = factory.GetSoldier("士兵" + (i + 1), ak47, SoldierType.Water); } soldier.Fight(); } //我们有这么多的士兵,但是使用的内存不是很多,因为我们缓存了。 Console.Read(); }
//享元模式调用 public static void GetFlyWeight() { //比如,我们现在需要10000个一般士兵,只需这样 SoldierFactory facotry = new SoldierFactory(); AK47 ak47 = new AK47(); //我们又这么多的士兵,但是使用的内存不是很多,因为我们缓存了 for (int i = 0; i < 100; i++) { Soldier soldier = null; if (i <= 20) { soldier = facotry.GetSoldier("士兵" + (i + 1), ak47, SoldierType.Normal); } else { soldier = facotry.GetSoldier("士兵" + (i + 1), ak47, SoldierType.Water); } soldier.Fight(); } Console.Read(); }
public void Run() { string[] input; while ((input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries))[0] != "End") { Soldier current = factory.GetSoldier(input, soldiers); if (current == null) { continue; } soldiers.Add(current); } foreach (Soldier soldier in soldiers) { Console.Write(soldier); } }
public static void Main(string[] args) { ISoldier soldier; for (int i = 0; i < 1000; i++) { soldier = SoldierFactory.GetSoldier(SoldierType.SONNASI); } Console.WriteLine(Sonnasi.ObjectCounter); for (int i = 0; i < 1000; i++) { soldier = SoldierFactory.GetSoldier(SoldierType.ASUR); } Console.WriteLine(Asur.Objectcounter); Console.ReadLine(); }
static void Main() { List <ISoldier> soldiers = new List <ISoldier>(); string input; while ((input = Console.ReadLine()) != "End") { try { string[] args = input.Split(); string type = args[0]; var tempSoldier = SoldierFactory.GetSoldier(args, soldiers); soldiers.Add(tempSoldier); } catch (ArgumentException) { }; } foreach (var soldier in soldiers) { Console.WriteLine(soldier.ToString()); } }
public static void Main() { List <ISoldier> soldiers = new List <ISoldier>(); Dictionary <string, Private> privatesById = new Dictionary <string, Private>(); string input = null; while ((input = Console.ReadLine()) != "End") { string[] soldierParams = input.Split(); Enums.SoldierType soldierType = Enum.Parse <Enums.SoldierType>(soldierParams[0]); ISoldier soldier = SoldierFactory.GetSoldier(soldierType, soldierParams.Skip(1).ToArray(), privatesById); if (soldier != null) { soldiers.Add(soldier); } } soldiers.ForEach(Console.WriteLine); }