This is the 14th day of my participation in the August Text Challenge.More challenges in August

In Dr. Yan Hong’s book JAVA and Pattern, the Template Method pattern is described as follows: Template Method pattern is the behavior pattern of classes. Prepare an abstract class that implements some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods that force subclasses to implement the rest of the logic. Different subclasses can implement these abstract methods in different ways, resulting in different implementations of the remaining logic. This is the purpose of the template method pattern.

In the template method pattern, through the template in the abstract class skeleton, define an operation algorithm to extract some steps in concrete the abstract method, force method according to the need to rewrite a subclass, so that at the time of use, the subclass does not change the algorithm structure can be redefined specific steps of the algorithm. The template method pattern is a basic technique based on inherited code reuse.

Template method pattern structure

Template method pattern UML

The roles involved in the template method pattern

  • AbstractTemplate: an AbstractTemplate class that defines the basic skeleton of an algorithm and one or more abstract operations that are implemented by subclasses.
  • ConcreteTemplate: ConcreteTemplate roles that implement steps in the algorithm skeleton and perform functions associated with a particular subclass.

Case presentation

Here we take login as a demonstration example. As we all know, the login module has member login and ordinary user login, which are different in the processing of login permissions.

Define the abstract template login module

/** * Declare an abstract template class@author Iflytek_dsw
 *
 */
abstract class AbstractLogin {
	/** * The login process can be divided into the following steps: * 1. * 2. User authentication; * /
	public void login(String userName, String pwd){
		encryptPwd(pwd);
		loginUser(userName,pwd);
	}
	
	protected abstract void loginUser(String userName, String pwd);
	
	/** * Password encryption *@param pwd
	 */
	private void encryptPwd(String pwd){
		System.out.println("Password encryption"); }}Copy the code

In the abstract login module, we define an abstract function that declares a method encryptPwd and a loginUser.

Define a concrete template method class

/** * Common user login *@author Iflytek_dsw
 *
 */
class NormalLogin extends AbstractLogin{

	@Override
	public void loginUser(String userName, String pwd) {
		System.out.println("Common User Login :"+ userName); }}/** * Member user login *@author Iflytek_dsw
 *
 */
class MemberLogin extends AbstractLogin{

	@Override
	public void loginUser(String userName, String pwd) {
		System.out.println("Member User Login"+ userName); }}Copy the code

The login methods vary according to the rights and roles. That is, subclasses override template methods reserved by their parent classes.

The client

public class Client {

	/ * * *@param args
	 */
	public static void main(String[] args) {
		AbstractLogin normalLogin = new NormalLogin();
		AbstractLogin memberLogin = new MemberLogin();
		
		normalLogin.login("Andoter's Study Notes."."dsw123");
		memberLogin.login("Andoter study Notes."."andoter123"); }}Copy the code

The execution result

Password encryption Common user login Password encryption Member user loginCopy the code

As we can see from the above example, the core of the template approach is the skeleton structure of the fixed algorithm, and its UML diagram is very similar to the policy pattern, although the two patterns are fundamentally quite different. The policy mode focuses on different policies, while the template method mode focuses on the code uncertainty of key nodes in a process, and the scope is smaller than the policy mode.

Methods in template methods

Based on the introduction above, methods in the template method can be divided into two broad categories: template method and basic method.

Template method

  • A template method is a method defined in an abstract class that combines basic operations into a method of aggregation or a method of aggregate behavior.
  • An abstract class can have any number of template methods, not just one. Each template method can call any number of concrete methods.

Basic method

  • Abstract method: An abstract method is declared by an abstract class and implemented by concrete subclasses. Abstract methods are identified by the abstract keyword in the Java language.
  • Concrete method: A concrete method is declared and implemented by an abstract class; subclasses do not implement or replace it.
  • Hook methods: A hook method is declared and implemented by an abstract class, and subclasses extend it. An abstract class usually presents an empty implementation as the default implementation of a method.

Advantages and disadvantages of the template approach

advantages

  • Good reuse by centralizing common methods in template classes and extracting different parts of common methods into subclasses.
  • Expansibility is good, encapsulate invariable, extend variable part.
  • Easy to maintain

disadvantages

  • The skeleton is fixed and upgrading is not easy.