委托模式的应用

public class Manage 
{      
      //声明委托
      public delegate void EventHandler(string a);
      //声明事件
      public event EventHandler Change;
      public void RaiseEvent(string a)
      {
            Change(a);
      }
}

public class Action
{
      public static void doA()
      {
            Console.WriteLine("doA");
      }
      
      public static void doB()
      {
            Console.WriteLine("doB");
      }
      
      public static void doC()
      {
            Console.WriteLine("doC");
      }
}

static void Main(string[] args)
{
      Manage m = new Manage();
      //注册事件
      m.Change += new Manage.EventHandler(m_Change);
      Action.doA();
      //当调用doA()方法时,触发事件,通知doA()方法被调用
      m.RaiseEvent("doA");
      //触发事件时的具体操作
      static void m_Change(string a)
      {
            switch(a)
            {
                  case "doA":
                        break;
                  case "doB":
                        break;
                  case "doC":
                        break;
                  default:
                        break;
            }
      }
}

你可能感兴趣的:(模式)