标签:
1.基本委托
class delete { delegate double MyDelegate(double message); public static double Ordinary(double price) { double price1 = 0.95 * price; Console.WriteLine("Ordinary Price : " + price1); return price1; } public static double Favourable(double price) { double price1 = 0.85 * price; Console.WriteLine("Favourable Price : " + price1); return price1; } static void Main(string[] args) { MyDelegate myDelegate = new MyDelegate(Ordinary); myDelegate += new MyDelegate(Favourable); Console.WriteLine("current price : " + myDelegate(100)); } }
2反省委托
//public class lang //{ // public string name { set; get; } //} //public class c : lang { // public string name { set; get; } //} //public delegate void Handler<T>(T obj); //public static void GetLang(lang l) { // Console.WriteLine("this is lang"+l.name); //} //public static void getC(c cc){ // Console.WriteLine("this is "+cc.name); //} //static void Main(string[] args) { // Handler<lang> h = new Handler<lang>(GetLang); // lang l = new lang(); // l.name = "test language"; // h(l); // Handler<c> hc = new Handler<c>(getC); // c cc = new c(); // cc.name = "c language"; // hc(cc); //}
3.观察者模式
//public delegate double Handler(double basicWages); //interface IWork { // double GetWages(double basicWages); //} //public class Manager:IWork //{ // public double GetWages(double basicWages) // { // double totalWages = 1.5 * basicWages; // Console.WriteLine("Manager‘s wages is : " + totalWages); // return totalWages; // } //} //public class Assistant:IWork //{ // public double GetWages(double basicWages) // { // double totalWages = 1.2 * basicWages; // Console.WriteLine("Assistant‘s wages is : " + totalWages); // return totalWages; // } //} //public class WageManager //{ // private Handler wageHandler; // //加入观察者 // public void Attach(Handler wageHandler1) // { // wageHandler += wageHandler1; // } // //删除观察者 // public void Detach(Handler wageHandler1) // { // wageHandler -= wageHandler1; // } // //通过GetInvodationList方法获取多路广播委托列表,如果观察者数量大于0即执行方法 // public void Execute(double basicWages) // { // if (wageHandler != null) // if (wageHandler.GetInvocationList().Count() != 0) // wageHandler(basicWages); // } // static void Main(string[] args) // { // WageManager wageManager = new WageManager(); // //加入Manager观察者 // Manager manager = new Manager(); // Handler managerHandler = new Handler(manager.GetWages); // wageManager.Attach(managerHandler); // //加入Assistant观察者 // Assistant assistant = new Assistant(); // Handler assistantHandler = new Handler(assistant.GetWages); // wageManager.Attach(assistantHandler); // //同时加入底薪3000元,分别进行计算 // wageManager.Execute(3000); // Console.ReadKey(); // } //}
4.没弄懂
//public class WageManager //{ // IList<Worker> workerList = new List<Worker>(); // public void RegisterWorker(Worker worker) // { // workerList.Add(worker); // } // public void RemoveWorker(Worker worker) // { // workerList.Remove(worker); // } // public void Excute(double basicWages) // { // if (workerList.Count != 0) // foreach (var worker in workerList) // worker.GetWages(basicWages); // } // public delegate double Handel(double d); // public void addHandel(Handel h) { // h += h; // } // static void Main(string[] args) // { // WageManager wageManager = new WageManager(); // //注册观察者 // wageManager.RegisterWorker(new Manager()); // wageManager.RegisterWorker(new Assistant()); // //同时输入底薪3000元,分别进行计算 // wageManager.Excute(5000); // Console.ReadKey(); // } //} //public abstract class Worker //{ // public abstract double GetWages(double basicWages); // public abstract double CheckWages(double basicWages); //} //public class Manager : Worker //{ // public override double CheckWages(double basicWages) // { // double totalWages = 1.5 * basicWages; // Console.WriteLine("Manager‘s wages is " + totalWages); // return totalWages; // } // //Manager实际工资为底薪1.5倍 // public override double GetWages(double basicWages) // { // double totalWages = 1.5 * basicWages; // Console.WriteLine("Manager‘s wages is " + totalWages); // return totalWages; // } //} //public class Assistant : Worker //{ // public override double CheckWages(double basicWages) // { // double totalWages = 1.2 * basicWages; // Console.WriteLine("Assistant‘s wages is " + totalWages); // return totalWages; // } // //Assistant实际工资为底薪的1.2倍 // public override double GetWages(double basicWages) // { // double totalWages = 1.2 * basicWages; // Console.WriteLine("Assistant‘s wages is " + totalWages); // return totalWages; // } //}
标签:
原文地址:http://www.cnblogs.com/0to9/p/5162703.html