Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Template Method

0.00/5 (No votes)
6 Feb 2010CPOL1 min read 5.9K  
Template method

Consider you need to develop some searching engine. Engine will look for different messages that have been sent. Searching process consists of a few operations which make sense for each of message, BUT could have some characteristic which differs. You want to write Searcher, which will allow you to encapsulate the algorithm of searching, but you also want to leave the ability of override behavior for some operations for specific messages. How could you accomplish this easily?

Template Method 

This pattern is intuitive as well as the realization of it. You will need base class which holds primitive operations and some Template method (Search) which operates with those operations. Each operation could be overridden in derived classes.

I wrote a naive implementation of this pattern, because I call primitive operations one by one in my template method. In the real world, you could hold a complex algorithm built on your primitive operations. And you will just need to override parts which differ from standard implemented in base class. Please note that you could make primitive operations abstract. This will require implementation in each derived class.

My example implementation is as follows:

C#
//base class
public class MessagesSearcher {

    protected Date DateSent;
    protected String PersonName;
    protected int ImportanceLevel;

    public MessagesSearcher(Date dateSent, String personName, int importanceLevel){
        DateSent = dateSent;
        PersonName = personName;
        ImportanceLevel = importanceLevel;
    }
    
    //primitive operations
    protected void createDateCriteria(){
        System.out.println("Standard date criteria has been applied.");
    }
    protected void createSentPersonCriteria(){
        System.out.println("Standard person criteria has been applied.");
    }   
    protected void createImportanceCriteria(){
        System.out.println("Standard importance criteria has been applied.");
    }
    
    //TEMPLATE METHOD
    public String Search(){
        createDateCriteria();
        createSentPersonCriteria();
        System.out.println(
            "Template method does some verification accordingly to search algo.");
        createImportanceCriteria();
        System.out.println(
            "Template method verifies if message could be so important or" +
            "useless from person provided in criteria.");
        System.out.println();
        return "Some list of messages...";
    }
}

public class ImportantMessagesSearcher extends MessagesSearcher{

    public ImportantMessagesSearcher(Date dateSent, String personName) {
        super(dateSent, personName, 3); // 3 means important message
    }
    
    //primitive operation overridden
    protected void createImportanceCriteria(){
        System.out.println("Special importance criteria has been formed: IMPORTANT");
    }
}

public class UselessMessagesSearcher extends MessagesSearcher {

    public UselessMessagesSearcher(Date dateSent, String personName) {
        super(dateSent, personName, 1); // 1 means useless message
    }
    
    //primitive operation overridden
    protected void createImportanceCriteria(){
        System.out.println("Special importance criteria has been formed: USELESS");
    }
}

Following usage:

C#
MessagesSearcher searcher = new UselessMessagesSearcher(null, "Sally");
searcher.Search();
searcher = new ImportantMessagesSearcher(null, "Killer");
searcher.Search();

Produces output:

C#
Standard date criteria has been applied.
Standard person criteria has been applied.
Template method does some verification accordingly to search algo.
Special importance criteria has been formed: USELESS
Template method verifies if message could be so important or useless from person
   provided in criteria.

Standard date criteria has been applied.
Standard person criteria has been applied.
Template method does some verification accordingly to search algo.
Special importance criteria has been formed: IMPORTANT
Template method verifies if message could be so important or useless from person
   provided in criteria.

Hope my example was not painful to understand. Honestly, I do not see it to be 100% clear example on this pattern, but I think I did a great job on this. Of course, you could find dozens of examples of it over internet. But this one is mine and it has put something in my mind.

License

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