How to use the chain of custody design model in C #

0


Design patterns are solutions to common problems and complexities in software design. As we saw earlier, they are classified into three distinct categories: creative, structural, and behavioral. The chain of custody design model falls under the behavioral category and can be used to reduce the coupling between the originator of a request and the receiver object handling the request.

The Definition of the band of four of the chain of custody design model:

Avoid pairing the sender of a request with the recipient by giving more than one object the ability to process the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Basically, in the chain of custody design model, you have a list or a chain of objects that can handle a request. Each object can either handle the request or pass the request to another manager. In this article, we will discuss the purpose of the chain of custody design model and how it can be implemented in C #.

The chain of custody design model

Participants in a typical implementation of the chain of custody model include an abstract manager, a concrete manager, and the customer or consumer. The abstract manager is the interface that is responsible for defining the contract. The concrete handler is the component that implements the operations defined in the abstract handler; the concrete manager processes the request itself or passes the request to a successor. Finally, the client component is responsible for initiating the request.

Now let’s see how to implement this design pattern in C #. Assuming you have Visual Studio installed on your system, you can follow these steps to create a console application project in the Visual Studio IDE.

  1. Open the Visual Studio IDE
  2. Click File -> New -> Project
  3. In the New Project dialog box, select the “Class Library (.Net Framework)” project template
  4. Specify the name and location of your project and click OK to save

The console application project created will include a default file named Class1.cs. You can rename this file as you like or create a new file. Now let’s dive into the code.

Create the abstract manager

Consider the following abstract base class.

public abstract class HandlerBase
    {
        protected HandlerBase successor;
        public void SetSuccessor(HandlerBase handlerBase)
        {
            successor = handlerBase;
        }
        public abstract void ProcessRequest(string request);
    }

The HandlerBase class contains two methods: the SetSuccessor and ProcessRequest methods. While the former is used to define a successor object to handle the request, the latter is the method that will process the request. We will implement these methods in the concrete classes presented below.

Create concrete managers

Consider the following two concrete classes.

public class ConcreteHandlerA : HandlerBase
   {
       public override void ProcessRequest(string request)
          {
              //Some code
          }
   }
public class ConcreteHandlerB : HandlerBase
   {
       public override void ProcessRequest(string request)
          {
              //Some code
          }
   }

Implement the chain of responsibilities

Finally, the chain of responsibilities must be put in place. The following piece of code illustrates how this can be achieved.

static void Main(string[] args)
        {
            string[] codes = { “A001”, “A002”, “A003” };
            HandlerBase handlerA = new ConcreteHandlerA();
            HandlerBase handlerB = new ConcreteHandlerB();
            handlerA.SetSuccessor(handlerB);
            handlerB.SetSuccessor(handlerA);
            foreach (var s in codes)
            {
                handlerA.ProcessRequest(s);
            }
            Console.Read();
        }

Refer to the code list above. Note how the concrete managerial bodies were created and the responsibilities delegated. (I’ll leave it to you to implement the appropriate ProcessRequest method for your needs.) Also note how the string values ​​were passed as a parameter to the ProcessRequest method.

It was only a structural implementation of the design model. Feel free to modify this implementation according to your needs. By leveraging the chain of custody design pattern, you can facilitate reuse and loose coupling in your application.

Copyright © 2018 IDG Communications, Inc.


Share.

Leave A Reply