简体   繁体   中英

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

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:

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:

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:

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

After using Delegate

*** Photo.cs and PhotoFilter.cs are remain unchanged

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:

  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:

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM