Sunday, March 26, 2017

Chain of Responsibility Pattern


Chain of Responsibility design pattern can be used to process multiple types of request and each type of request can be handled by multiple handlers. Chain of Responsibility design pattern falls under behavioral pattern of GOF (Gang of Four) pattern.

When to use –


Chain of Responsibility pattern can be used when multiple types of request are handled by multiple handlers. If one handler is not able to process your request, then it will pass to another handler. In this pattern, each handler has reference to next handler. If one handler can’t process request, then it passes it to next handler.

Major components of Chain of Responsibility pattern –


Handler – This is an abstract class. This holds reference of next handler and also has abstract method to handler request.

Concrete Handler – This is concrete class which inherits from Handler class and implements abstract method to handle request.

Client – This is client class and create requests and passes to different handlers in the chain of responsibility pattern.

See below example of Chain of responsibility pattern.

Code –

//Handler class
public abstract class Approver
{
    public abstract void HandleRequest(int requestAmount);
    protected Approver NextApprover;
    public void SetNextApprover(Approver approver)
    {
        NextApprover = approver;
    }
}
//Concrete Handler class
public class Manager : Approver
{
    public override void HandleRequest(int requestAmount)
    {
        if (requestAmount < 100000)
            Console.WriteLine("Manager has approved amount - {0}", requestAmount);
        else if (NextApprover != null)
            NextApprover.HandleRequest(requestAmount);
    }
}
//Concrete Handler class
public class SeniorManager : Approver
{
    public override void HandleRequest(int requestAmount)
    {
        if (requestAmount < 200000)
            Console.WriteLine("Senior Manager has approved amount - {0}", requestAmount);
        else if (NextApprover != null)
            NextApprover.HandleRequest(requestAmount);
    }
}
//Concrete Handler class
public class Director : Approver
{
    public override void HandleRequest(int requestAmount)
    {
        if (requestAmount < 300000)
            Console.WriteLine("Director has approved amount - {0}", requestAmount);
        else if (NextApprover != null)
            NextApprover.HandleRequest(requestAmount);
    }
}

//Client Class
class Program
{
    static void Main(string[] args)
    {
        //Create classes of chain of responsibility
        Approver Manager = new Manager();
        Approver SeniorManager = new SeniorManager();
        Approver Director = new Director();

        //Set next request approver or handler
        Manager.SetNextApprover(SeniorManager);
        SeniorManager.SetNextApprover(Director);

        //Create requests.
        Manager.HandleRequest(75000);
        Manager.HandleRequest(150000);
        Manager.HandleRequest(250000);

        Console.ReadLine();
    }
}

Output –



As you can see in above example, Approver abstract class is inherited to respective approvers. In client class, instance of each concrete handler class created as handler class and set its next approver or handler for each concrete handler class. At last manager class handle request method called all the times. If manager class not able to process request it will pass on to its next approver and so on in chain of responsibilities classes.

You can download full code from Gist.

I hope this article helps you to know more about Chain of Responsibility pattern. Please leave your feedback in comments section below.

References –

See Also –

Sunday, February 26, 2017

How to cancel a Parallel.For and Parallel.Foreach loop – Task Parallel Library


In my previous article, I explained about Parallel.For and Parallel.Foreach loop in detail. In this article I will explain, how to cancel running or ongoing Parallel.For and Parallel.Foreach loop on certain event.

Parallel.For and Parallel.Foreach has overload methods to pass ParallelOptions to accept cancel request from user. We need to set CancelationToken to ParallelOptions and pass it to Parallel.For and Parallel.Foreach methods. When user request for cancelling parallel loop via cancellation token, then the parallel loop will throw an OperationCanceledException and cancel the parallel loop. See below example for more information.

Code –


Output –




As you can see in above example, when user request for cancel the parallel loop, cancellation token of parallel options will throw an OperationCanceledException and cancel the running parallel loop.


References –

See also –


Saturday, January 14, 2017

How to find and remove duplicate files in directory using C# code?


In today’s world It is common to have multiple similar images/videos in your computer and it takes lots of your disk space without your knowledge. I am inspired to write this article because I am getting lots of similar images, videos and documents in WhatsApp daily and it is very difficult for me to find duplicate files from directory. In this article, I’ll share C# code to find duplicate files from directory and also remove them.

Let’s look on below folder which contains multiple duplicate images. I want to find all duplicate images from this folder and delete them.


See below code to find and delete duplicate file from this folder.



Output -



As you can see duplicate files are deleted from your directory.



This programme can be used to delete any duplicate files like images, videos, documents etc.

I hope this code snippet helps you to find and remove duplicate files in your directory and make your life easy. Please leave your feedback in comments below.

See also –