Wednesday, May 31, 2017

Template Pattern


As the name Template suggests, is a kind of layout available for some kind of process that we can modify as needed and use it. We have some sub-tasks to complete a big task that we can use as per our situation requirements. So technically speaking, we make the sub-tasks as abstract and implement them, as per our needs, in different cases. But one thing remains common; the overall steps of the entire procedure or the algorithm, in other words first Step 1, then Step 2, then Step 3 and so on, that remain the same in their execution order

What is Template Method Pattern:

“According to the GoF's definition: Defines the skeleton of an algorithm in an operation, deferring some steps to sub-classes. The Template Method lets sub-classes redefine certain steps of an algorithm without changing the algorithm's structure”

Example:

Let us have a class for Data Exporter and below are the steps performed (this is same for all type of data converter i.e. the algorithm structure remains same)
Step 1: Read Data
Step 2: Format Data
Step 3: Export Data
[Code]

The implementation of the ReadData and FormatData will not change as per the requirements. The only changeable part is the ExportData function whose implementation will change based on the target file to export to.

abstract class DataExporter
{
    // This will not vary as the data is read from sql only
    public void ReadData()
    {
        Console.WriteLine("Reading the data from SqlServer");
    }

    // This will not vary as the format of report is fixed.
    public void FormatData()
    {
        Console.WriteLine("Formating the data as per requriements.");
    }

    // This may vary based on target file type choosen
    public abstract void ExportData();        

    // This is the template method that the client will use.
    public void ExportFormatedData()
    {
        this.ReadData();
        this.FormatData();
        this.ExportData();
    }
}

So if we need to export the data to an excel file we need a ConcreteClass for that, we will create a concrete class ExcelExporter

class ExcelExporter : DataExporter
{
    public override void ExportData()
    {
        Console.WriteLine("Exporting the data to an Excel file.");
    }
}

Also another concrete class PDFExporter

class PDFExporter : DataExporter
{
    public override void ExportData()
    {
        Console.WriteLine("Exporting the data to a PDF file.");
    }
}

DataExporter class will be used by the application and the required implementation of the algorithm will be taken from the derived classes.

static void Main(string[] args)
{
    DataExporter exporter = null;

    // Lets export the data to Excel file
    exporter = new ExcelExporter();
    exporter.ExportFormatedData();

    Console.WriteLine();

    // Lets export the data to PDF file
    exporter = new PDFExporter();
    exporter.ExportFormatedData();
}

So we have seen that the actual algorithm to export the data remains the same but the part where the file type to export to can be moved into the derived classes. The template method will remain oblivious to the implementation and will run the algorithm. At run time it will call the derived classes overridden function to execute the required functionality.

Another example is web page creation, all web pages have a something common Header and Footer but their content is different, and the algorithm is same creating the webpage (i.e. step 1: create header, step:2 create page content, step 3: create footer)




No comments:

Post a Comment