Consider you need to develop some searching engine. Engine will look for different messages that has been sent. Searching process consists with 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 encapsulate algorithm of searching, but you also want to leave ability override behavior of some operations for specific messages. How could you accomplish this easily?

TEMPLATE METHOD

This pattern is intuitive as well as 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 naive implementation of this pattern, because I call primitive operations one by one in my template method. In real world you could hold complex algorithm built on your primitive operations. And you will just need to override parts which differs 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

//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 overriden
    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 overriden
    protected void createImportanceCriteria(){
        System.out.println(“Special importance criteria has been formed: USELESS”);
    }
}

Following usage:

MessagesSearcher searcher = new UselessMessagesSearcher(null, “Sally”);
searcher.Search();
searcher = new ImportantMessagesSearcher(null, “Killer”);
searcher.Search();

Produces output:
   

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 have put something in my mind.