Delegates is a way to support addition of multiple features
(eg: methods) to a framework without the need to recompile the core class that
uses these methods. Delegate is a
reference to a function/s and is itself an object (derived from
MulticastDelegate) that knows how to call a method or group of methods.
Delegates are used when an eventing design pattern is used
OR when the caller does not need to access other methods or properties on the
object implementing the method/s. If this is not the case, we use interfaces to
achieve the same purpose.
Example
Methods defined in –
public class BloodThinners
{
public void ApplyAntiCoagulant(Blood blood)
{
Console.WriteLine("Applying AntiCoagulant");
}
public void ApplyCirculation(Blood blood)
{
Console.WriteLine("Applying Circulation");
}
public void NaturalThin(Blood blood)
{
Console.WriteLine("Applying Natural thining..");
}
}
Using custom Delegate –
1. public class BloodProcessor
2. {
3. public delegate void BloodThinnerHandler(Blood blood);
4. public void Process(string path, BloodThinnerHandler
thinnerHandler)
5. {
6. var blood = Blood.Load(path);
7.
8. var thinners = new BloodThinners();
9. thinnerHandler(blood);
10.
11. blood.Save();
12.
13. }
14. }
We define the custom delegate BloodThinnerHandler to handle
the groups of methods that return void and that passed in object of type Blood
(eg: the group of methods in the Class BloodThinners).
Using Generic
Delegates
We can use generic delegates – Action<> and
Func<> to reduce the code. Action<> can be used to represent method
that takes 16 param and returns nothing Func<> can be used to represent
method that takes 16 params and returns one param.
public class BloodProcessor
{
//public delegate void BloodThinnerHandler(Blood blood);
public void Process(string path, Action<Blood> thinnerHandler)
{
var blood = Blood.Load(path);
var thinners = new BloodThinners();
thinnerHandler(blood);
blood.Save();
}
}
Above uses delegate thinnerHandler of type generic-delegate
(Action) that takes one argument.
Used as
class Program
{
static void Main(string[] args)
{
string path = "path";
var bloodProcessor = new BloodProcessor();
var BloodThinners = new BloodThinners();
//BloodProcessor.BloodThinnerHandler thinnerHandler = BloodThinners.ApplyAntiCoagulant;
Action<Blood> thinnerHandler = BloodThinners.ApplyAntiCoagulant;
thinnerHandler += BloodThinners.ApplyCirculation;
thinnerHandler += BloodThinners.NaturalThin;
thinnerHandler += RemoveFat;
bloodProcessor.Process(path,thinnerHandler);
}
public static void RemoveFat(Blood blood)
{
Console.WriteLine("Remove Blood Fat");
}
}