I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.
In this article, we will see what is a “Multicast Delegate” and how we create and use it. Multicast delegates are the combination two or more delegates of same type and they together form a Delegate Chain. Each participant in the delegate chain should have a void return type.
In the code, we will take an example of an Order Processing System that makes use of the Multicast Delegate. First, we will create the OrderShipment Class and then we will move to the client code. In the client code, we will use our OrderShipment Class and Multicast Delegate.
2. OrderShipment Class
This Class breaks the order processing into a small group of functions. Moreover, all these functions will match a particular delegate type. This will make these functions eligible for delegate chaining.
1) First, we declare a simple delegate. Later, we will use this for the purpose delegate chaining. The delegate accepts Order Id and Customer Id as a parameter. Also, it returns nothing. Please keep in mind, multicast delegate principle work only for void return types. There is no restriction on the parameters it receives. Below is the Delegate declaration:
2) We split the order processing into five small functions. We will make these functions to form Delegate Chaining. The functions are shown below:
Note, in these functions, there is nothing more than the call to Console output. But, we obviously see, how these functions will be in real-world applications.
3) This class has a Member function that accepts the Multicast delegate as a parameter and then makes a call to it. The client will create the delegate chain based on the above five functions and then calls this Member function:
We completed the Implementation of this class. Now, we will go for Delegate chaining.
3. Client Code - Delegate Chaining
The client will process the order shipment differently for three types of customers. The customer types are:
Regular customers who makes purchases monthly twice or more.
The VIP customer who has built up a good relation.
For Normal customer there is no discount and surprising gifts. The regular customer will have surprising gifts based on the order cost. And, VIP customer has a discount as well as gifts. Now, lets us go through how the client code makes use of the Multicast Delegates.
1) First, we create the instance of OrderShipment Class. Code is below:
2) Next, we declare a delegate of type OrderProcessingMethods. Later, we will use this delegate variable as a Multicast Delegate.
3) Next, we create five delegate instances and they point to one of the five methods implemented by the OrderShipment class.
4) Before processing the order for normal customer, a Delegate chain is formed by adding the Delegate created on the previous step. Once the individual delegates are combined using the + operator, we store the result in the orderprocess Delegate. Now, the orderprocess Delegate holds the chain of delegates which we call as a Multicast Delegate. We pass this Delegate Train to the OrderShipment class member function ProcessOrderShipment. When we call this function, the Delegate invokes all the functions currently in the chain. So, for the normal customer we do not want to provide a gift and/or discounts. Hence, those corresponding functions are not part of the Delegate chain. Also, note that the chained functions are called in the same order they are added to the chain. The chaining of function is shown below
The code we write to form this chain is below:
5) Next comes the VPI customer. As he is eligible for the gift as well as discounts, we need to add the corresponding functions to the multicast delegate orderprocess. Before we proceed, we should know the current delegates in the chain and also its placement. Process5 delegate is for order confirmation, which we should move to the last in the chain. So, process5 delegate removed from the chain, then process3 and process4 delegates are added to the chain. Finally, process5 delegate is put back before making the call to ProcessOrderShipment. Note the usage of the += operator. To add a delegate you can use += operator. And to remove a delegate from the chain, you can use -= operator.
6) Now, we will re-arrange the chain for Regular Customer. We now know how delegate chaining works and hence no explanation is required. Below is the code:
The complete code example and its output are given below: