Click here to Skip to main content
13,896,676 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked
Posted 20 Jun 2011
Licenced CPOL

Template method pattern and Action in C#

, 21 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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.

public abstract class TemplateMethod
    private Action CommonMethodSetToExecute;

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

    private void DefaultMethodSetToExecute()

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

Here is the implementer of this base class:

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:

class Program
    static TemplateMethodImp templateMethodImpObject = new TemplateMethodImp();
    static void Main(string[] args)
    static void ChangedBusinessLogic()


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


About the Author

You may also be interested in...


Comments and Discussions

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 21 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid