zoukankan      html  css  js  c++  java
  • strategytheory.cs

      using System;
     
      // Strategy Pattern    by  Judith Bishop         Oct 2007
      // Shows two strategies and a random switch between them

      // The Context
      class Context {
        // Context state
        public const int start = 5;
        public int Counter = 5;
        
        // Strategy aggregation
        IStrategy strategy = new Strategy1();
        
        // Algorithm invokes a strategy method
        public int Algorithm() {
          return strategy.Move(this);
        }
        
        // Changing strategies
        public void SwitchStrategy() {
          if (strategy is Strategy1)
            strategy = new Strategy2();
          else
            strategy = new Strategy1();
        }
      }
     
      // Strategy interface
      interface IStrategy  {
        int Move (Context c);
      }

      // Strategy 1
      class Strategy1 : IStrategy {
        public int Move (Context c) {
          return ++c.Counter;
        }
      }
     
      // Strategy 2
      class Strategy2 : IStrategy {
        public int Move (Context c) {
          return --c.Counter ;
        }
      }
     
      // Client
      static class Program {
        static void Main () {
          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("|| ");
              context.SwitchStrategy();
            }
            Console.Write(context.Algorithm() +"  ");
          }
          Console.WriteLine();
        }
      }
      /* Output
      4  || 5  6  7  || 6  || 7  8  9  10  || 9  8  7  6  || 7  || 6  5  
      */
  • 相关阅读:
    动态规划3--Help Jimmy
    动态规划2--最长公共子序列
    动态规划1--最长公共子序列
    递归5--汉诺塔问题的栈实现
    递归4--汉诺塔问题
    递归3--棋盘分割
    CSS实现和选择器
    Java基础十二--多态是成员的特点
    头文件algorithm中的常用函数
    我是怎样成长为系统架构师的
  • 原文地址:https://www.cnblogs.com/shihao/p/2506821.html
Copyright © 2011-2022 走看看