static void Main(string[] args) { Context context = new Context(); context.Strategy = new StrategyA(); context.Algorithm(); context.Strategy = new StrategyB(); context.Algorithm(); }
public void Test() { Context context = new Context(); context.SetStrategy(new ConcreteStrategy()); //可以在运行时指定类型,通过配置文件和反射机制实现 context.Algorithm(); //使用算法 }
static void Main(string[] args) { Context context = new Context(); context.SwitchStrategy(); Random r = new Random(37); for (int i = Context.start; i <= Context.start + 15; i++) { if (r.Next(3) == 2) // Random.Next(int maxValue) { Console.Write("|| "); context.SwitchStrategy(); } Console.Write(context.Algorithm() + " "); } Console.WriteLine(); }
static void Main(string[] args) { Context context = new Context(); context.SwitchStrategy(); Random r = new Random(37); for (int i = Context.start; i <= Context.start + 15; i++) { if (r.Next(3) == 2) { Console.Write("||"); Thread.Sleep(250); context.SwitchStrategy(); } Console.Write(context.Algorithm() + " "); } Console.WriteLine(); Console.ReadLine(); }