简体   繁体   English

使用或不使用委托有什么区别

[英]What's the difference between using or not using delegate

I have some doubt of understanding the concept of using delegate, below is the delegate example that I have. 我对使用委托的概念有一些疑问,下面是我的委托示例。

This example is using a Photo filtering software as a instance, which will add filter to a photo and using delegate will increase the flexibility of adding new filter in the future. 此示例使用照片过滤软件作为实例,它将为照片添加过滤器,使用委托将增加将来添加新过滤器的灵活性。

Before using Delegate 在使用Delegate之前

Photo.cs: Photo.cs:

 class Photo
    {
        private string path;

        public Photo(string path)
        {
            this.path = path;
            Console.WriteLine("{0} imported", path);
        }

        public void Save()
        {
            Console.WriteLine("{0} photo saved", this.path);
        }
    }

PhotoFilter.cs: PhotoFilter.cs:

class PhotoFilter
{
    public void AddBrigtness(Photo photo)
    {
        Console.WriteLine("Added Brightness");
    }

    public void AddFilter(Photo photo)
    {
        Console.WriteLine("Added Filter");
    }

    public void AddShadow(Photo photo)
    {
        Console.WriteLine("Added Shadow");
    }
}

PhotoProcesser.cs: PhotoProcesser.cs:

class PhotoProcesser
{
   public void Process(string path)
    {
        var photo = new Photo(path);
        var filter = new PhotoFilter();

        filter.AddBrigtness(photo);
        filter.AddFilter(photo);
        filter.AddShadow(photo);

        photo.Save();
    }
}

Program.cs: Program.cs中:

class Program
{
    static void Main(string[] args)
    {
        var process = new PhotoProcesser();
        process.Process("123.jpg");
    }
}

After using Delegate 使用Delegate后

*** Photo.cs and PhotoFilter.cs are remain unchanged *** Photo.csPhotoFilter.cs保持不变

PhotoProcesser.cs: PhotoProcesser.cs:

 delegate void PhotoMethodHandler(Photo p); // Delegate

    class PhotoProcesser
    {
       public void Process(string path,PhotoMethodHandler methodHandler)
        {
            var photo = new Photo(path);

            methodHandler(photo);

            photo.Save();
        }
    }

Program.cs: Program.cs中:

  class Program
    {
        static void Main(string[] args)
        {
            var filter = new PhotoFilter();

            PhotoMethodHandler p = filter.AddBrigtness;
            p += filter.AddFilter;
            p += RemoveRedEyeFilter; // To simulate the flexibility of using Delegate

            var process = new PhotoProcesser();
            process.Process("123.jpg",p);
        }

        static void RemoveRedEyeFilter(Photo photo) // Newly added filter
        {
            Console.WriteLine("Added RemoveRedEye");
        }
    }

Output: 输出:

在此输入图像描述

At this point, I can understand the flexibility of using Delegate as a Function pointer, but if we are thinking in another way, we also can get the same result if we don't use the PhotoProcesser.cs and change the Program.cs as below: 在这一点上,我可以理解使用Delegate作为函数指针的灵活性,但如果我们以另一种方式思考,如果我们不使用PhotoProcesser.cs并将Program.cs更改为as,我们也可以获得相同的结果。下面:

Program.cs: Program.cs中:

  class Program
    {    
        static void Main(string[] args)
        {
            var photo = new Photo("p1.jpg");
            var filter = new PhotoFilter();

            filter.AddBrigtness(photo);
            filter.AddFilter(photo);
            RemoveRedEyeFilter(photo);

            photo.Save();

        }

        static void RemoveRedEyeFilter(Photo photo) // Newly added filter
        {
            Console.WriteLine("Added RemoveRedEye");
        }
    } 

Output: 输出:

在此输入图像描述

It will get the same outcome and the same flexibility (To add a new filter in this case) as using of delegate. 它将获得相同的结果和相同的灵活性(在这种情况下添加新的过滤器)作为委托的使用。

As per above example,could anyone can give me some direction to understand what is the benefit/different of using delegate? 按照上面的例子,任何人都可以给我一些方向来了解使用委托的好处/不同之处是什么? Thanks! 谢谢!

The idea behind delegates (and with lambda or any pointer to a function) is to be able to transfer a static behaviour (defined by code) to dynamic (defined by data). 委托(以及lambda或任何指向函数的指针)背后的想法是能够将静态行为(由代码定义)转换为动态(由数据定义)。

For instance I could do : 比如我可以这样做:

Photo p = new Photo();
p = ApplyX(p);
p = ApplyY(p);
...

But with delegate I can do 但是我可以做代表

List<Func<Photo,Photo>> filters = Whatever();
Photo p = new Photo();
foreach(var filter in filters)
{
   p = filter(p);
}

This would let you change the list of filters at runtime (by inserting and removing delegate in the filters list). 这将允许您在运行时更改过滤器列表(通过在过滤器列表中插入和删除委托)。

You can check this for further reading: Open Closed Principle 您可以查看此内容以进一步阅读: 打开封闭原则

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DynamicInvoke直接调用委托和使用DynamicInvokeImpl有什么区别? - What is the difference between calling a delegate directly, using DynamicInvoke, and using DynamicInvokeImpl? 使用委托和使用Func有什么区别 <T> /行动 <T> 在方法签名中? - What is the difference between using a delegate and using Func<T>/Action<T> in a method signature? 委托创建表达式和方法组转换之间有什么区别? - What's the difference between a delegate creation expression and a method group conversion? 我使用此委托的语法有什么问题? - What's wrong with my syntax for using this delegate? 使用EndOfStream和检查null之间有什么区别? - What's the difference between using EndOfStream and checking for null? 枚举和使用带常量的静态类有什么区别? - What's the difference between enums & using static classes with constants? 使用Serializable属性和实现ISerializable之间有什么区别? - What's the difference between using the Serializable attribute & implementing ISerializable? 使用 do.net 和 MSBuild 构建 .NET 应用程序有什么区别? - What's the difference between using dotnet and MSBuild for building .NET applications? 使用::和键入完全限定的名称空间有什么区别? - What's the difference between using :: and typing the fully qualified namepace? 使用DataBinder时,以下代码有什么区别? - What's the difference between the following code when using DataBinder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM