USE Interface CallBack 通过接口实现回调

    刚刚在书上发现有一个接口回调的事,但是自己也没用过这玩意,所以在园子里soso了一把,好像也没找到interace CallBack,所以在这里给大家分享一下。如果有不正确的地方,大家一起讨论,讨人,希望不要误人子弟了。呵呵.

1、少不了的Interface来实现回调的

2、当然先有一个对像CallBack,它来继承Interface,做要被回调的方法事情,被回调来通过某些场景,或条件一但成立情况下调用的!

3、控制Controller,在某个特定条件判断成立情况下开始调用,回调!

类图如下:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CA_OOO_Demo001
 7 {
 8     /// <summary>
 9     /// 定义一个回调接口
10     /// </summary>
11     interface IBack 
12     {
13         void CellBackMethod();
14     }
15 
16     class CellBack : IBack
17     {
18 
19         public void CellBackMethod()
20         {
21             Console.WriteLine("Thi is CellBack Method!");
22         }
23     }
24 
25     class Controller
26     {
27         public IBack CellbackObj = null;
28 
29         public Controller(IBack obj)
30         {
31             this.CellbackObj = obj;
32         }
33 
34         public void Start()
35         {
36             Console.WriteLine("键盘按下回车键,条件成立下执行回调方法!");
37             if (ConsoleKey.Enter == Console.ReadKey(true).Key)
38             {
39                 CellbackObj.CellBackMethod();
40             }
41         }
42 
43     }
44 
45 
46     class InterfaceCellBack
47     {
48         static void Main(string[] args)
49         {
50             Controller cOjb = new Controller(new CellBack());
51             cOjb.Start();
52         }
53     }
54 }

上面的例子感觉成了,接口策略模式了,然后再加一个实现接口类,就OK了,代码如下:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace CA_OOO_Demo001
 7 {
 8     /// <summary>
 9     /// 定义一个回调接口
10     /// </summary>
11     interface IBack 
12     {
13         void CellBackMethod();
14     }
15 
16     class CellBack : IBack
17     {
18 
19         public void CellBackMethod()
20         {
21             Console.WriteLine("Thi is CellBack Method!A");
22         }
23     }
24 
25     class CellBackB : IBack
26     {
27 
28         public void CellBackMethod()
29         {
30             Console.WriteLine("Thi is CellBack Method!B");
31         }
32     }
33 
34     class Controller
35     {
36         public IBack CellbackObj = null;
37 
38         public Controller(IBack obj)
39         {
40             this.CellbackObj = obj;
41         }
42 
43         public void Start()
44         {
45             Console.WriteLine("键盘按下回车键,条件成立下执行回调方法!");
46             if (ConsoleKey.Enter == Console.ReadKey(true).Key)
47             {
48                 CellbackObj.CellBackMethod();
49             }
50         }
51 
52     }
53 
54 
55     class InterfaceCellBack
56     {
57         static void Main(string[] args)
58         {
59             Controller cOjb = new Controller(new CellBack());
60             Controller cOjb2 = new Controller(new CellBackB());
61             cOjb.Start();
62             cOjb2.Start();
63         }
64     }
65 }

欢迎童鞋们讨论...

原文地址:https://www.cnblogs.com/p_db/p/2581265.html