Click here to Skip to main content
15,861,366 members
Articles / Programming Languages / C#
Tip/Trick

Template method pattern and Action in C#

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
21 Jun 2011CPOL1 min read 30K   11   5
An abstract base class with abstract methods and a common method with implementation which will be used by the implementer classes of the abstract class.

Template method is a handy pattern to run common functionality of a base class from the child class. The skeleton of this pattern is there will an abstract base class with abstract methods and a common method with implementation which will be used by the implementer classes of the abstract class. So all objects of the child type can use the common method functionality. Using the following example, we can change the logic of the common methods from the user of those classes.


This will be clear when you see the following example. First of all, we have an abstract class.


C#
public abstract class TemplateMethod
{
    private Action CommonMethodSetToExecute;

    public abstract void DisplayOne();
    public abstract void DisplayTwo();
    public abstract void DisplayThree();

    private void DefaultMethodSetToExecute()
    {
        DisplayOne();
        DisplayTwo();
    }

    public void Execute(Action CommonMethodSetToExecute = null)
    {
        if (CommonMethodSetToExecute == null)
        {
            CommonMethodSetToExecute = DefaultMethodSetToExecute;
        }
        CommonMethodSetToExecute();
    }
}

Here is the implementer of this base class:


C#
public class TemplateMethodImp : TemplateMethod
{
    public override void DisplayOne()
    {
        Console.WriteLine("Display method from the implmentor");
    }

    public override void DisplayTwo()
    {
        Console.WriteLine("Display two method from the implmentor");
    }

    public override void DisplayThree()
    {
        Console.WriteLine("Display three method from the implmentor");
    }
}

All the implementers of TemplateMethod, for example, TemplateMethod1, TemplateMethod2, TemplateMethod3..... TemplateMethodN have the Execute method in common, and it can be executed from instances of the TemplateMethod1, TemplateMethod2.... TemplateMethodN classes. But if we have a circumstance for example, where the TemplateMethod2 class needs to execute the Execute method with a different logic, I introduce an Action named CommonMethodSetToExecute in the base class and the Execute method has an optional CommonMethodSetToExecute parameter with the default value null. So when we need to execute the Execute method, we either pass our new business logic via the CommonMethodSetToExecute action or just use the default value set for CommonMethodSetToExecute.


The usage of this Template is as below:


C#
class Program
{
    static TemplateMethodImp templateMethodImpObject = new TemplateMethodImp();
    static void Main(string[] args)
    {
        templateMethodImpObject.Execute(templateMethodImpObject.DisplayTwo);
        templateMethodImpObject.Execute(ChangedBusinessLogic);
    }
    static void ChangedBusinessLogic()
    {
        templateMethodImpObject.DisplayOne();
        templateMethodImpObject.DisplayThree();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia

Comments and Discussions

 
QuestionThanks for this wonderful Article. Pin
bilaskaizen24-Oct-13 6:19
bilaskaizen24-Oct-13 6:19 
GeneralReason for my vote of 4 A neat trick. Pin
manish.kungwani30-Jun-11 6:04
manish.kungwani30-Jun-11 6:04 
GeneralRe: Thanks Manish :) Pin
Mohammad A Rahman30-Jun-11 11:19
Mohammad A Rahman30-Jun-11 11:19 
QuestionStrategy + Template Method Pin
netindustries27-Jun-11 17:25
netindustries27-Jun-11 17:25 
AnswerRe: Strategy + Template Method Pin
Mohammad A Rahman27-Jun-11 18:08
Mohammad A Rahman27-Jun-11 18:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.