callback(2)--使用代理

来源:互联网 发布:js控制更多 编辑:程序博客网 时间:2024/04/30 08:49

针对callback(1)的需求,可以使用代理来解决。

/相对于用接口Callback的方式,      //代理可以和接口一样指定方法的定义      //代理可以自动维护一个ArrayList,通过简单的 += 和 -= 就可以实现.      //如果Car类中,将该代理类型的成员设为public ,甚至可以不用再写维护的方法

CAR

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestCS{    class Car    {        #region Properties and variables        private int maxSpeed = 500;        private string petName;        private int currentSpeed;        public string PetName        {            get { return petName; }            set { petName = value; }        }        public int CurrentSpeed        {            get { return currentSpeed; }            set { currentSpeed = value; }        }        #endregion        //声明一个代理,此代理为一个inner class的代理。全名为Car.Exploed        public delegate void Exploed(string msg);        //声明一个该代理类型的成员。        private Exploed delegateExplodedList;        //维护该成员        //是否可以改为属性?        public void SetDelegateExplodedList(Exploed clientMethod)        {            this.delegateExplodedList += clientMethod;        }        public void SetDelegateExplodedListRemove(Exploed clientMethod)        {            this.delegateExplodedList -= clientMethod;        }        public Exploed DelegateExplodedList        {            set { this.delegateExplodedList += value; }        }        bool isExploed=false;        public void Accelerate(int delta)        {            if (isExploed)            {                if (delegateExplodedList != null)                {                    //触发此代理                    //帮代理赋参数值                    delegateExplodedList("(Car is doing---->Car is Dead!)");                }            }            else            {                currentSpeed += delta;                Console.WriteLine("Current Speed is {0}", currentSpeed.ToString());                if (maxSpeed< currentSpeed)                    isExploed = true;            }                    }    }}
 
 
客户端
using System;using System.Collections;using System.Linq;using System.Text;using System.Collections.Generic;using System.Runtime.Serialization;namespace TestCS{         class Program {     //相对于用接口Callback的方式,     //代理可以和接口一样指定方法的定义     //代理可以自动维护一个ArrayList,通过简单的 += 和 -= 就可以实现.     //如果Car类中,将该代理类型的成员设为public ,甚至可以不用再写维护的方法            public static void Main(string[] args)      {          Car c = new Car();          Car.Exploed clientDelegate = new Car.Exploed(Program.OnDeadMethod);          Car.Exploed clientDelegate2 = new Car.Exploed(Program.OnDeadMethod2);          c.SetDelegateExplodedList(clientDelegate);          c.SetDelegateExplodedListRemove(clientDelegate);          c.SetDelegateExplodedList(clientDelegate2);          //c.DelegateExplodedList = clientDelegate;          //c.DelegateExplodedList = clientDelegate2;          for (int i = 0; i < 10; i++)          {              c.Accelerate(100);          }      }      //客户端在这里做自定义的处理      public static void OnDeadMethod(string msg)      {          Console.WriteLine("Client doing------>"+msg);      }      public static void OnDeadMethod2(string msg)      {          Console.WriteLine("Client Doing Type 2--------->" + msg);      }  }}
原创粉丝点击