使用代表的好处?

7 .net delegates design-patterns

我想在VB.NET或C#或其他一些.NET语言中实现Observer模式.我听说代理可以用于此,但无法弄清楚为什么它们比观察者实现的普通旧接口更受欢迎.所以,

  • 为什么我应该使用委托而不是定义我自己的接口并传递对实现它们的对象的引用?
  • 为什么我要避免使用委托,并使用良好的ol-fashioned接口?

Ben*_*n 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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.


Chr*_*isW 22

你是O/S,我是一个应用程序.当你发现发生的事情时,我想告诉你调用我的一种方法.为此,我向你传递一个代表我想要你打电话给我的方法.我自己也不称之为我的那种方法,因为我希望你在发现某事时给它打电话.你不直接调用我的方法,因为你不知道(在编译时)该方法存在(我甚至没有在你建立时编写); 相反,您可以调用在运行时收到的委托指定的任何方法.


kem*_*002 9

从技术上讲,您不必使用委托(除非使用事件处理程序,否则它是必需的).你可以没有他们.实际上,它们只是工具箱中的另一个工具.

关于使用它们的第一件事就是Inversion Of Control.只要你想控制函数在其外部的行为方式,最简单的方法就是将一个委托作为参数放置,让它执行委托.


har*_*rpo 6

你不是在想程序员.

问题是,为什么你可以在调用委托时直接调用函数?

大卫·惠勒(David Wheeler)的一句名言是:计算机科学中的所有问题都可以通过另一层次的间接来解决.

我有点舌头.显然,您将在大多数时间直接调用函数,尤其是在模块中.但是,当需要在包含对象不可用(或相关)的上下文(例如事件回调)中调用函数时,委托很有用.

  • http://en.wikipedia.org/wiki/Indirection说,这通常是故意错误引用"抽象层"取代"间接层".Kevlin Henney的必然结果是,"......除了太多的间接层问题之外." (2认同)

Cha*_*ana 2

实际上,委托传递的是对方法的引用,而不是对象...接口是对对象实现的方法子集的引用...

如果在应用程序的某个组件中,您需要访问对象的多个方法,则定义一个表示该对象方法子集的接口,并在您可能需要传递给该对象的所有类上分配和实现该接口组件...然后通过该接口而不是通过它们的具体类传递这些类的实例。

如果,在某些方法或组件中,您所需要的只是几个方法之一,这些方法可以位于任意数量的不同类中,但都具有相同的签名,那么您需要使用委托。