Template Method Pattern | Behavioral design pattern 

Template Method 

 The Template Method Pattern is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure. The key components include an abstract class with a template method and concrete subclasses that implement specific steps.


Simple C# example:


using System;

// Abstract class defining the template method

abstract class AbstractClass

{

    // Template method with the algorithm's skeleton

    public void TemplateMethod()

    {

        CommonOperation1();

        SpecificOperation();

        CommonOperation2();

    }


    // Abstract methods to be implemented by subclasses

    protected abstract void SpecificOperation();

    protected abstract void CommonOperation1();

    protected abstract void CommonOperation2();

}


// Concrete class implementing specific steps

class ConcreteClassA : AbstractClass

{

    protected override void SpecificOperation()

    {

        Console.WriteLine("Specific operation for ConcreteClassA");

    }


    protected override void CommonOperation1()

    {

        Console.WriteLine("Common operation 1 for ConcreteClassA");

    }


    protected override void CommonOperation2()

    {

        Console.WriteLine("Common operation 2 for ConcreteClassA");

    }

}


// Another concrete class with different implementation

class ConcreteClassB : AbstractClass

{

    protected override void SpecificOperation()

    {

        Console.WriteLine("Specific operation for ConcreteClassB");

    }


    protected override void CommonOperation1()

    {

        Console.WriteLine("Common operation 1 for ConcreteClassB");

    }


    protected override void CommonOperation2()

    {

        Console.WriteLine("Common operation 2 for ConcreteClassB");

    }

}


class Program

{

    static void Main()

    {

        AbstractClass objA = new ConcreteClassA();

        AbstractClass objB = new ConcreteClassB();


        objA.TemplateMethod();

        Console.WriteLine("------");

        objB.TemplateMethod();

    }

}



In this example, `AbstractClass` provides a template method (`TemplateMethod`) with common steps and abstract methods (`SpecificOperation`, `CommonOperation1`, `CommonOperation2`) that concrete subclasses (`ConcreteClassA` and `ConcreteClassB`) must implement. The template method orchestrates the overall algorithm, allowing subclasses to customize specific parts of it.


A real-world example of the Template Method Pattern can be found in various software frameworks and libraries. Let's consider the development of a web application framework where the framework provides a template for handling HTTP requests, and developers can customize specific parts of the handling process.


using System;


// Abstract class representing a web application framework

abstract class WebApplicationFramework

{

    // Template method for handling HTTP requests

    public void HandleRequest()

    {

        AuthenticateUser();

        ProcessRequest();

        LogRequest();

    }


    // Abstract methods to be implemented by subclasses

    protected abstract void AuthenticateUser();

    protected abstract void ProcessRequest();

    protected abstract void LogRequest();

}


// Concrete class implementing a specific web application

class MyWebApplication : WebApplicationFramework

{

    protected override void AuthenticateUser()

    {

        Console.WriteLine("User authentication logic for MyWebApplication");

    }


    protected override void ProcessRequest()

    {

        Console.WriteLine("Processing HTTP request for MyWebApplication");

        // Custom logic specific to the application

    }


    protected override void LogRequest()

    {

        Console.WriteLine("Logging HTTP request for MyWebApplication");

    }

}


class Program

{

    static void Main()

    {

        WebApplicationFramework myApp = new MyWebApplication();

        myApp.HandleRequest();

    }

}



In this scenario, `WebApplicationFramework` serves as the abstract class defining the template method (`HandleRequest`). Concrete subclasses, like `MyWebApplication`, extend the framework and provide their implementations for specific steps such as user authentication, request processing, and logging. The template method ensures the overall sequence of operations is maintained while allowing customization for different web applications.





Comments