Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Context context = new Context();

            context.Strategy = new StrategyA();
            context.Algorithm();

            context.Strategy = new StrategyB();
            context.Algorithm();
        }
Exemplo n.º 2
0
        public void Test()
        {
            Context context = new Context();

            context.SetStrategy(new ConcreteStrategy()); //可以在运行时指定类型,通过配置文件和反射机制实现
            context.Algorithm();                         //使用算法
        }
Exemplo n.º 3
0
        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();
        }
Exemplo n.º 4
0
        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();
        }