Usage examples: The Template Method pattern is quite common in Java frameworks. Developers often use it to provide framework users with a simple means of extending standard functionality using inheritance.
Here are some examples of Template Methods in core Java libraries:
In javax.servlet.http.HttpServlet class, all the doXXX() methods send the HTTP 405 “Method Not Allowed” error by default. However, you can override any of those methods to send a different response.
Identification: Template Method can be recognized if you see a method in base class that calls a bunch of other methods that are either abstract or empty.
Overriding standard steps of an algorithm
In this example, the Template Method pattern defines an algorithm of working with a social network. Subclasses that match a particular social network, implement these steps according to the API provided by the social network.
networks
networks/Network.java: Base social network class
packagerefactoring_guru.template_method.example.networks;/** * Base class of social network. */publicabstractclassNetwork {String userName;String password;Network() {} /** * Publish the data to whatever network. */publicbooleanpost(String message) {// Authenticate before posting. Every network uses a different// authentication method.if (logIn(this.userName,this.password)) {// Send the post data.boolean result =sendData(message.getBytes());logOut();return result; }returnfalse; }abstractbooleanlogIn(String userName,String password);abstractbooleansendData(byte[] data);abstractvoidlogOut();}
networks/Facebook.java: Concrete social network
packagerefactoring_guru.template_method.example.networks;/** * Class of social network */publicclassFacebookextendsNetwork {publicFacebook(String userName,String password) {this.userName= userName;this.password= password; }publicbooleanlogIn(String userName,String password) {System.out.println("\nChecking user's parameters");System.out.println("Name: "+this.userName);System.out.print("Password: ");for (int i =0; i <this.password.length(); i++) {System.out.print("*"); }simulateNetworkLatency();System.out.println("\n\nLogIn success on Facebook");returntrue; }publicbooleansendData(byte[] data) {boolean messagePosted =true;if (messagePosted) {System.out.println("Message: '"+newString(data)+"' was posted on Facebook");returntrue; } else {returnfalse; } }publicvoidlogOut() {System.out.println("User: '"+ userName +"' was logged out from Facebook"); }privatevoidsimulateNetworkLatency() {try {int i =0;System.out.println();while (i <10) {System.out.print(".");Thread.sleep(500); i++; } } catch (InterruptedException ex) {ex.printStackTrace(); } }}
networks/Twitter.java: One more social network
packagerefactoring_guru.template_method.example.networks;/** * Class of social network */publicclassTwitterextendsNetwork {publicTwitter(String userName,String password) {this.userName= userName;this.password= password; }publicbooleanlogIn(String userName,String password) {System.out.println("\nChecking user's parameters");System.out.println("Name: "+this.userName);System.out.print("Password: ");for (int i =0; i <this.password.length(); i++) {System.out.print("*"); }simulateNetworkLatency();System.out.println("\n\nLogIn success on Twitter");returntrue; }publicbooleansendData(byte[] data) {boolean messagePosted =true;if (messagePosted) {System.out.println("Message: '"+newString(data)+"' was posted on Twitter");returntrue; } else {returnfalse; } }publicvoidlogOut() {System.out.println("User: '"+ userName +"' was logged out from Twitter"); }privatevoidsimulateNetworkLatency() {try {int i =0;System.out.println();while (i <10) {System.out.print(".");Thread.sleep(500); i++; } } catch (InterruptedException ex) {ex.printStackTrace(); } }}
Demo.java: Client code
packagerefactoring_guru.template_method.example;importrefactoring_guru.template_method.example.networks.Facebook;importrefactoring_guru.template_method.example.networks.Network;importrefactoring_guru.template_method.example.networks.Twitter;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;/** * Demo class. Everything comes together here. */publicclassDemo {publicstaticvoidmain(String[] args) throwsIOException {BufferedReader reader =newBufferedReader(new InputStreamReader(System.in));Network network =null;System.out.print("Input user name: ");String userName =reader.readLine();System.out.print("Input password: ");String password =reader.readLine();// Enter the message.System.out.print("Input message: ");String message =reader.readLine();System.out.println("\nChoose social network for posting message.\n"+"1 - Facebook\n"+"2 - Twitter");int choice =Integer.parseInt(reader.readLine());// Create proper network object and send the message.if (choice ==1) { network =newFacebook(userName, password); } elseif (choice ==2) { network =newTwitter(userName, password); }network.post(message); }}
OutputDemo.txt: Execution result
Input user name: Jhonatan
Input password: qswe
Input message: Hello, World!
Choose social network for posting message.
1 - Facebook
2 - Twitter
2
Checking user's parameters
Name: Jhonatan
Password: ****
..........
LogIn success on Twitter
Message: 'Hello, World!' was posted on Twitter
User: 'Jhonatan' was logged out from Twitter