Showing posts with label Abstract Class. Show all posts
Showing posts with label Abstract Class. Show all posts

Sunday, December 16, 2018

Template Method Pattern

Template Method design pattern to define an algorithm as skeleton of operations and leave the details to be implemented by child classes. Template Method Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –


Template Method pattern used to define basic steps or structure of an algorithm and allow the implementation of each steps in derived class. Template method pattern has two components. One is an abstract class and one or more concrete classes. An abstract class is the Template class, which defines algorithmic steps and structure and preserves it across multiple implementations.  

Major components of Template Method pattern –


Abstract Class – This is an abstract class contains template method and abstract operations.
Concrete Class – This class inherits Abstract class and overrides abstract methods.

Let’s have a look on below example of Template Method design pattern.

Code –
//Template abstract class
public abstract class OrderTemplate
{
    public abstract void SelectProduct();
    public abstract void Payment();
    public abstract void Deliver();
    public void ProcessOrder()
    {
        SelectProduct();
        Payment();
        Deliver();
    }
}
//concrete classe
public class OnLineOrder : OrderTemplate
{
    public override void Deliver()
    {
        Console.WriteLine("Product shipped via courier");
    }

    public override void Payment()
    {
        Console.WriteLine("Online payment done successfully");
    }

    public override void SelectProduct()
    {
        Console.WriteLine("Product added to cart successfully");
    }
}
//concrete class
public class StoreOrder : OrderTemplate
{
    public override void Deliver()
    {
        Console.WriteLine("Product delivered to customer at counter");
    }

    public override void Payment()
    {
        Console.WriteLine("Paid at counter");
    }

    public override void SelectProduct()
    {
        Console.WriteLine("Product is selected by customer from rack in store");
    }
}
class Program
{
    //entry point
    static void Main(string[] args)
    {
        Console.WriteLine("----------- Online Order Process -----------");
        OrderTemplate onlineOrder = new OnLineOrder();
        onlineOrder.ProcessOrder();
        Console.WriteLine("----------- Retail Store Order Process -----------");
        OrderTemplate storeOrder = new StoreOrder();
        storeOrder.ProcessOrder();
        Console.Read();
    }
}

Output –



As per above example, OrderTemplate abstract class contains SelectPrduct, Payment and Deliver methods. ProcessOrder method is Tempate method that calls these methods in sequence. OnLineOrder and StoreOrder classes derived from OrderTemplate abstract class and overrides abstracts methods differently in each class.

You can download full code from Gist.

I hope this article helps you to know more about Template Method Design Pattern. Please leave your feedback in comments section below.

References –

See Also –

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 –