我想在VB.NET或C#或其他一些.NET语言中实现Observer模式.我听说代理可以用于此,但无法弄清楚为什么它们比观察者实现的普通旧接口更受欢迎.所以,
为什么我应该使用委托而不是定义我自己的接口并传递对实现它们的对象的引用?
为什么我要避免使用委托,并使用良好的ol-fashioned接口?
Ben M.. 27
当你可以直接调用一个方法,你并不需要一个代表.
A delegate is useful when the code calling the method doesn't know/care what the method it's calling is -- for example, you might invoke a long-running task and pass it a delegate to a callback method that the task can use to send notifications about its status.
Here is a (very silly) code sample:
enum TaskStatus { Started, StillProcessing, Finished } delegate void CallbackDelegate(Task t, TaskStatus status); class Task { public void Start(CallbackDelegate callback) { callback(this, TaskStatus.Started); // calculate PI to 1 billion digits for (...) { callback(this, TaskStatus.StillProcessing); } callback(this, TaskStatus.Finished); } } class Program { static void Main(string[] args) { Task t = new Task(); t.Start(new CallbackDelegate(MyCallbackMethod)); } static void MyCallbackMethod(Task t, TaskStatus status) { Console.WriteLine("The task status is {0}", status); } }
As you can see, the Task
class doesn't know or care that -- in this case -- the delegate is to a method that prints the status of the task to the console. The method could equally well send the status over a network connection to another computer. Etc.
当你可以直接调用一个方法,你并不需要一个代表.
A delegate is useful when the code calling the method doesn't know/care what the method it's calling is -- for example, you might invoke a long-running task and pass it a delegate to a callback method that the task can use to send notifications about its status.
Here is a (very silly) code sample:
enum TaskStatus { Started, StillProcessing, Finished } delegate void CallbackDelegate(Task t, TaskStatus status); class Task { public void Start(CallbackDelegate callback) { callback(this, TaskStatus.Started); // calculate PI to 1 billion digits for (...) { callback(this, TaskStatus.StillProcessing); } callback(this, TaskStatus.Finished); } } class Program { static void Main(string[] args) { Task t = new Task(); t.Start(new CallbackDelegate(MyCallbackMethod)); } static void MyCallbackMethod(Task t, TaskStatus status) { Console.WriteLine("The task status is {0}", status); } }
As you can see, the Task
class doesn't know or care that -- in this case -- the delegate is to a method that prints the status of the task to the console. The method could equally well send the status over a network connection to another computer. Etc.
你是O/S,我是一个应用程序.当你发现发生的事情时,我想告诉你调用我的一种方法.为此,我向你传递一个代表我想要你打电话给我的方法.我自己也不称之为我的那种方法,因为我希望你在发现某事时给它打电话.你不直接调用我的方法,因为你不知道(在编译时)该方法存在(我甚至没有在你建立时编写); 相反,您可以调用在运行时收到的委托指定的任何方法.
从技术上讲,您不必使用委托(除非使用事件处理程序,否则它是必需的).你可以没有他们.实际上,它们只是工具箱中的另一个工具.
关于使用它们的第一件事就是Inversion Of Control.只要你想控制函数在其外部的行为方式,最简单的方法就是将一个委托作为参数放置,让它执行委托.
你不是在想程序员.
问题是,为什么你可以在调用委托时直接调用函数?
大卫·惠勒(David Wheeler)的一句名言是:计算机科学中的所有问题都可以通过另一层次的间接来解决.
我有点舌头.显然,您将在大多数时间直接调用函数,尤其是在模块中.但是,当需要在包含对象不可用(或相关)的上下文(例如事件回调)中调用函数时,委托很有用.