January 23, 2010 Design Patterns
January 23, 2010 Design Patterns
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?
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
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:
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: 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.
Markdown | Result |
---|---|
*text* | text |
**text** | text |
***text*** | text |
`code` | code |
~~~ more code ~~~~ |
more code |
[Link](https://www.example.com) | Link |
* Listitem |
|
> Quote | Quote |
For more illustrative example you should use 'final' keyword with template method:
public final String Search()
Otherwise this method will be virtual implicitly, and may be overridden in derived classes.
Yes, you are probably right. This adds more clarity to how this should work.
Thank you.