Multicast Delegate ft. Chain-Of-Responsibility Design Pattern

Firstly, let's understand what Chain Of Responsibility Design Pattern is.
 
It is one of the Behavioral Design Patterns out of 23 patterns defined in the Gang Of Four Design Patterns.  The usage of this pattern is not so prevalant and is categorized as 'Medium-Low' for the frequency of usage.
 
The pattern inherently implements 'Choreogrpahy' pattern of algorithm as opposed to Orchestration. To put it in an over-simplified context, if there are multiple objects handling a job, then there is no single super-object that controls the algorithm of who gets to solve the problem. Instead the start object A passes the batton to the next object B in line as it knows only about its own job and its successor, not who all are there in the game. Object B, if capable of doing the job, completes it and passes the signal back to the initiator. If not it passes the control to object C and so on. 
 
In order to discuss the usage for the Chain-Of-Responsibility pattern, let's check a simple use case. 
 
Multicast Delegate ft. Chain-Of-Responsibility Design Pattern
 
In this blog, I will not use interfaces and classes to demonstrate the use case. Instead will use multicast delegates to achieve the objective. 
 
Before I proceed, let me put my disclaimer,
  1. The use of multicast delegate is just for illustration of the usage of delegates using the aforementioned design pattern.
  2. The delegates are not substitutes for interfaces and classes. In fact, delegates represent methods inside classes.
  3. Since the delegate can represent methods that have the same signature and return type, it's kind of analogous to enforcing Interface methods inside different concrete classes that inherits that interface. Hence the below-mentioned approach.
Notes on Delegates
There are multiple authors on this platform who have written in detail on delegates and explained it aptly. However, just for the sake of context, a brief note is presented here.
 
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

Syntax for delegate declaration is,
  1. delegate <return type> <delegate-name> <parameter list>  

Multicasting of a Delegate


You can put multiple methods (matching the return and parameter list) to the same delegate instance. Using this property of delegates you can create an invocation list of methods that will be called when a delegate is invoked. This is called multicasting of a delegate.
 
With that as background, let's proceed on our example of -Implementing the Invoice approval process using multicast delegates that features the Chain-Of-Responsibility Design Pattern.
 
Step 1
 
Our Expense Invoice entity class,
  1. public class ExpenseInvoice  
  2. {  
  3.     public decimal Amount { getset; }  
  4.     public DateTime ExpenseDate { getset; }  
  5.     public string Item { getset; }  
  6.     public bool IsAmountApproved { getset; }  
  7. }  
Step 2
 
Let's define our class that will do the processing and starts with defining the delegate. We have defined a delegate that returns nothing but takes the ExpenseInvoice class as input that we defined in Step 1. Now this delegate can reference and represent and the method has the same signature - returns void and intakes ExpenseInvoice type.
  1. public class MultiCastDelegateExample  
  2. {  
  3.     private delegate void GetAmountApproved(ExpenseInvoice e);  
  4. }  
Step 3
 
Let me also define another generic delegate that would calculate the amount without the tax. This anonymous func will take a decimal value; calculate the actual expense without the tax (considered 10% of the amount in the invoice) and return the remaining amount as a decimal itself. We will use this delegate in our actual approval concrete methods.
  1. public class MultiCastDelegateExample  
  2. {  
  3.     private delegate void GetAmountApproved(ExpenseInvoice e);  
  4.     private readonly Func<decimaldecimal> AmountWithoutTax = (a) => { return a * 0.9M; };  
  5.   
  6. }  
Step 4
 
Now let's define our four different Approvers methods for the different thresholds of approval. Note that each of these methods has a matching signature of the delegate defined in Step 2 - returns void and accepts ExpenseInvoice type. Also, the amount value checked is the amount without the tax, for which we have used the second delegate defined (the anonymous function) in Step 3.
  1. public class MultiCastDelegateExample  
  2.  {  
  3.      private delegate void GetAmountApproved(ExpenseInvoice e);  
  4.      private readonly Func<decimaldecimal> AmountWithoutTax = (a) => { return a * 0.9M; };  
  5.   
  6.      private void ApprovalByManager(ExpenseInvoice e)  
  7.      {  
  8.          if (!e.IsAmountApproved)  
  9.          {  
  10.              bool approved = AmountWithoutTax(e.Amount) < 1000;  
  11.              if (approved) Console.WriteLine("The Manager approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  12.              e.IsAmountApproved = approved;  
  13.          }  
  14.      }  
  15.   
  16.      private void ApprovalByDeliveryManager(ExpenseInvoice e)  
  17.      {  
  18.          if (!e.IsAmountApproved)  
  19.          {  
  20.              bool approved = AmountWithoutTax(e.Amount) < 10000;  
  21.              if (approved) Console.WriteLine("The Delivery Manager approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  22.              e.IsAmountApproved = approved;  
  23.          }  
  24.      }  
  25.   
  26.      private void ApprovalByVicePresident(ExpenseInvoice e)  
  27.      {  
  28.          if (!e.IsAmountApproved)  
  29.          {  
  30.              bool approved = AmountWithoutTax(e.Amount) < 100000;  
  31.              if (approved) Console.WriteLine("The Vice President approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  32.              e.IsAmountApproved = approved;  
  33.          }  
  34.      }  
  35.   
  36.      private void ApprovalByPresident(ExpenseInvoice e)  
  37.      {  
  38.          if (!e.IsAmountApproved)  
  39.          {  
  40.              bool approved = AmountWithoutTax(e.Amount) < 1000000;  
  41.              if (approved) Console.WriteLine("The President approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  42.              e.IsAmountApproved = approved;  
  43.          }  
  44.      }  
  45.   
  46.  }  
Step 5
 
Now let's define the only public method of the class that would encapsulate the entire chain. We are using the delegate defined in Step 2 and assigned the first level of approver - the Manager.
  1. public void GetApproval(ExpenseInvoice ei)  
  2. {  
  3.     GetAmountApproved approval = ApprovalByManager;  
  4.     approval.Invoke(ei);  
  5. }  
Step 6
 
Hereafter, the sequence in which you add other methods will determine the order in which the chain is executed. This is multicasting the delegate.
  1. public void GetApproval(ExpenseInvoice ei)  
  2. {  
  3.     //creating the multicast delegate for approvals at different level progressively, if the lower level fails the limit  
  4.     GetAmountApproved approval = ApprovalByManager; // 1st level approver  
  5.     approval += ApprovalByDeliveryManager;          // 2nd level approver  
  6.     approval += ApprovalByVicePresident;            // 3rd level approver  
  7.     approval += ApprovalByPresident;                // 4th level approver  
  8.     //start the approval process  
  9.     approval.Invoke(ei);  
  10. }  
The finished class will look something like this,
  1. public class MultiCastDelegateExample  
  2. {  
  3.     private delegate void GetAmountApproved(ExpenseInvoice e);  
  4.     private readonly Func<decimaldecimal> AmountWithoutTax = (a) => { return a * 0.9M; };  
  5.   
  6.     public void GetApproval(ExpenseInvoice ei)  
  7.     {  
  8.         //creating the multicast delegate for approvals at different level progressively, if the lower level fails the limit  
  9.         GetAmountApproved approval = ApprovalByManager; // 1st level approver  
  10.         approval += ApprovalByDeliveryManager;          // 2nd level approver  
  11.         approval += ApprovalByVicePresident;            // 3rd level approver  
  12.         approval += ApprovalByPresident;                // 4th level approver  
  13.         //start the approval process  
  14.         approval.Invoke(ei);  
  15.     }  
  16.   
  17.     private void ApprovalByManager(ExpenseInvoice e)  
  18.     {  
  19.         if (!e.IsAmountApproved)  
  20.         {  
  21.             bool approved = AmountWithoutTax(e.Amount) < 1000;  
  22.             if (approved) Console.WriteLine("The Manager approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  23.             e.IsAmountApproved = approved;  
  24.         }  
  25.     }  
  26.   
  27.     private void ApprovalByDeliveryManager(ExpenseInvoice e)  
  28.     {  
  29.         if (!e.IsAmountApproved)  
  30.         {  
  31.             bool approved = AmountWithoutTax(e.Amount) < 10000;  
  32.             if (approved) Console.WriteLine("The Delivery Manager approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  33.             e.IsAmountApproved = approved;  
  34.         }  
  35.     }  
  36.   
  37.     private void ApprovalByVicePresident(ExpenseInvoice e)  
  38.     {  
  39.         if (!e.IsAmountApproved)  
  40.         {  
  41.             bool approved = AmountWithoutTax(e.Amount) < 100000;  
  42.             if (approved) Console.WriteLine("The Vice President approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  43.             e.IsAmountApproved = approved;  
  44.         }  
  45.     }  
  46.   
  47.     private void ApprovalByPresident(ExpenseInvoice e)  
  48.     {  
  49.         if (!e.IsAmountApproved)  
  50.         {  
  51.             bool approved = AmountWithoutTax(e.Amount) < 1000000;  
  52.             if (approved) Console.WriteLine("The President approved the amount {0} without the tax (10%)", AmountWithoutTax(e.Amount));  
  53.             e.IsAmountApproved = approved;  
  54.         }  
  55.     }  
  56.   
  57. }  
Step 7
 
Finally, the Main method (the client program) would initiate the approval process for a particular Expense Invoice. Here I have defined four different Expense Invoices at different amount levels which would make them flow into different levels progressively.
  1. static void Main(string[] args)  
  2. {  
  3.   
  4.     //Demo   
  5.     DelegateExamples de = new DelegateExamples();  
  6.     Domains.ExpenseInvoice ei1 = new Domains.ExpenseInvoice() { Amount = 1001.0M, ExpenseDate = Convert.ToDateTime("12-Mar-2021"), Item = "Stationary" };  
  7.     Domains.ExpenseInvoice ei2 = new Domains.ExpenseInvoice() { Amount = 10001.0M, ExpenseDate = Convert.ToDateTime("14-Mar-2021"), Item = "Computer HDD" };  
  8.     Domains.ExpenseInvoice ei3 = new Domains.ExpenseInvoice() { Amount = 100001.0M, ExpenseDate = Convert.ToDateTime("16-Mar-2021"), Item = "Hotel Room Rent" };  
  9.     Domains.ExpenseInvoice ei4 = new Domains.ExpenseInvoice() { Amount = 1000001.0M, ExpenseDate = Convert.ToDateTime("18-Mar-2021"), Item = "SUV" };  
  10.   
  11.     Console.WriteLine("The Amount {0} is under approval process.", ei1.Amount.ToString());  
  12.     de.GetApproval(ei1);  
  13.     Console.WriteLine();  
  14.     Console.WriteLine("The Amount {0} is under approval process.", ei2.Amount.ToString());  
  15.     de.GetApproval(ei2);  
  16.     Console.WriteLine();  
  17.     Console.WriteLine("The Amount {0} is under approval process.", ei3.Amount.ToString());  
  18.     de.GetApproval(ei3);  
  19.     Console.WriteLine();  
  20.     Console.WriteLine("The Amount {0} is under approval process.", ei4.Amount.ToString());  
  21.     de.GetApproval(ei4);  
  22.     Console.WriteLine();  
  23.     Console.ReadLine();  
  24. }  
Step 8
 
Execute the program and your output should be something like.
  1. The Amount 1001.0 is under approval process.  
  2. The Manager approved the amount 900.90 without the tax (10%)  
  3.   
  4. The Amount 10001.0 is under approval process.  
  5. The Delivery Manager approved the amount 9000.90 without the tax (10%)  
  6.   
  7. The Amount 100001.0 is under approval process.  
  8. The Vice President approved the amount 90000.90 without the tax (10%)  
  9.   
  10. The Amount 1000001.0 is under approval process.  
  11. The President approved the amount 900000.90 without the tax (10%)  

Conclusion

  1. Multicast Delegate allows you to create an invocation sequence.
  2. All methods part of the sequence must have the same signature - return type and the parameter list - type and count.
  3. Be aware of the parameter whether it's a value type or a reference type. The value type is passed at the root value to all methods in sequence. Even if the first method in the sequence changes the value of the input, it's not going to impact the second method in the sequence. The second method is going to process the root value only.
  4. You can make the method calls parallel by using async Tasks in the signature of the delegate. You have to implement additional logic to cumulate the response of all the tasks and then pass them to the calling client method.
Do let me know your feedback. Stay safe and stay blessed!