In this article, we will understand one of the simplest design pattern: Template Method Design Pattern.This pattern falls under the behavioral design patterns.
The pattern provides the outline or skeleton of an algorithm. The algorithm structure is provided by an abstract class. This abstract class may choose to provide some default implementation. Few functionalities are defined abstract. Their implementation is deferred for subclasses as per the need but without changing the algorithmâs structure. Few key points to remember for this pattern:
- Common parts of an algorithm should be implemented only once in the base class.
- Subclasses can redefine the behavior of some of the functionalities without affecting the algorithm structure.
- The base class defines a final template method providing a uniform implementation of algorithm parts.
xHere are ALL other Java Design Patterns, explained in detail with examplesx
Template Method Design Pattern by Example
For demonstration, let us assume an example where we will provide a service to open an email account.
Now the algorithm for this process can be:
- Accept desired email id
- Register the email id with chosen email provider
- Display confirmation message of email account creation.
In above algorithm step, 1 and 3 are common for all email provider and can be implemented by the base class. The second step will be unique to each email provider and they have to provide a specific implementation for that.
MailAccount Template Base class
public abstract class MailAccountTemplate { private void acceptInputs() { System.out.println("Accepting email id to create account"); } abstract void register(); public final void createAccount() { //accept the desired email address acceptInputs(); //register with respective Email Provider register(); //Display confirmation for account creation. displayConfirmation(); } private void displayConfirmation() { System.out.println("Account created!!"); } }
Note that above base class defines a template method. This method is our complete algorithm. The method has been declared as final so no subclass can override and alter the algorithm flow.
GmailAccount Subclass
public class GmailAccount extends MailAccountTemplate { @Override void register() { System.out.println("Registered your email account with Gmail"); } }
YahooAccount Subclass
public class YahooAccount extends MailAccountTemplate { @Override void register() { System.out.println("Registered your email account with Gmail"); } }
Running the example
public class TemplatePatternDemo { public static void main(String[] args) { MailAccountTemplate gmail = new GmailAccount(); gmail.createAccount(); MailAccountTemplate ymail = new YahooAccount(); ymail.createAccount(); } }
As I said earlier, this is one of the simplest patterns to implement. The output is:
Conclusion
In this article, we have understood Template Method Design Pattern. Few advantages of using this patten are:
- In this pattern, there will be no code duplication.
- Template pattern uses inheritance so we can reuse the code.
- This pattern provides flexibility to subclasses, so they can implement certain parts as per their specification.
You can download the code from our GitHub repository:
Download from Gitx
xHere are ALL other Java Design Patterns, explained in detail with examplesx