Welcome to follow the official wechat account: FSA Full stack action 👋

I. Introduction of template method mode

The Template Method pattern defines an algorithm skeleton in an operation and delays some steps of the algorithm to a subclass so that the subclass can redefine some specific steps of the algorithm without changing the structure of the algorithm. It belongs to the behavior pattern

  • The core of

    • Abstract Template: Defines a Template method, usually a concrete method, that gives a top-level algorithm skeleton, while the logical skeleton steps are deferred to subclasses in the corresponding abstraction
      • Template methods: Define the skeleton of an algorithm that calls its contained base methods in some order
      • Basic method: is a step in the whole algorithm, including abstract method and concrete method
    • Concrete Template: Implements one or more abstract methods defined by the parent class that are the constituent steps of a top-level algorithmic logic
  • Application scenarios

    • Servlet in JavaWeb, the HttpService class provides a service() method
    • Multiple subclasses have methods that share the same logic and can be considered as template methods
    • When designing a system, the key steps required by the algorithm are known, and the execution sequence of these steps is determined, but the concrete implementation of some steps is unknown, and can be postponed to the subclass to complete
  • advantages

    • Good expansibility, to the unchanged code package, to the variable extension, in line with the open and closed principle
    • Improve code reuse by putting the same parts of code in an abstract parent class and different code in different subclasses
    • Reflection control is implemented by a parent class calling the operations of its subclasses and by extending the different behaviors to the concrete implementation of the subclasses
  • disadvantages

    • Each different implementation requires a subclass to implement, resulting in an increase in the number of classes, which can complicate the system

Supplement: For Android development, BaseActivity is an abstract template, onCreate() is a template method, and onCreate() usually calls initView(), initEvent(), and initData(), which are the three basic methods. It can be an abstract method or a concrete method.

Second, the template method pattern code implementation

Create an abstract template:

/** * Abstract template: Project management **@author GitLqr
 */
public abstract class AbstractProjectManager {

	/** * Define the template method, declare final, prevent subclass overwriting, the process is fixed: requirements review - Design - development - test - go live - operation */
	public final void processProject(a) {
		review();
		design();
		coding();
		test();
		online();
	}

	/** * Every project needs to be reviewed */
	public void review(a) {
		System.out.println("Project Requirements Review");
	}

	/** * Every project needs a design */
	public void design(a) {
		System.out.println("UI UE design");
	}

	/** * abstract method, implemented by concrete subclass, specific encoding time is different */
	public abstract void coding(a);

	/** * Abstract methods, implemented by concrete subclasses, there are many kinds of tests: automated tests, security tests, stress tests, manual tests */
	public abstract void test(a);

	/** * abstract method, implemented by concrete subclasses, online full release, gray release, downtime release */
	public abstract void online(a);
}
Copy the code

Create a specific template:

/** * Specific template: Payment service project management **@author GitLqr
 */
public class PayServiceProjectManager extends AbstractProjectManager {

	@Override
	public void coding(a) {
		System.out.println("Development took 30 days.");
	}

	@Override
	public void test(a) {
		System.out.println("Functional testing, safety testing, stress testing.");
	}

	@Override
	public void online(a) {
		System.out.println("Full online"); }}/** * Specific template: User service project management **@author GitLqr
 */
public class UserServiceProjectManager extends AbstractProjectManager {

	@Override
	public void coding(a) {
		System.out.println("Development took 10 days.");
	}

	@Override
	public void test(a) {
		System.out.println("Functional testing, stress testing, manual testing.");
	}

	@Override
	public void online(a) {
		System.out.println("Grayscale release, full online"); }}Copy the code

Use:

public static void main(String[] args) {
    AbstractProjectManager projectManager;

    projectManager = new PayServiceProjectManager();
    projectManager.processProject();

    System.out.println("= = = = = = = = = = = = = = = = =");

    projectManager = new UserServiceProjectManager();
    projectManager.processProject();
}
Copy the code

If this article is helpful to you, please click on my wechat official number: FSA Full Stack Action, which will be the biggest incentive for me. The public account not only has Android technology, but also iOS, Python and other articles, which may have some skills you want to know about oh ~