What is the template method, which needs to be considered, look below:
1 Template Method2 Overview3 defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. 4 5 Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. 6 7 8 Applicability91One-time implementation of an invariant part of an algorithm, and the variable behavior is left to subclasses to implement. Ten One2The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication. A first identify the differences in the existing code and separate the differences into new operations. - Finally, replace these different code with a template method that invokes these new operations. - the3. Control the subclass extension. - - - participants +1. AbstractClass - defines the abstract primitive operations (primitiveoperation), in which subclasses redefine them to implement the steps of an algorithm. + implement a template method that defines the skeleton of an algorithm. A the template method not only invokes primitive operations, it also invokes operations defined in AbstractClass or other objects. at -2. Concreteclass -Implements primitive operations to complete the steps associated with a particular subclass in the algorithm.
Test class:
1 public class Test { 2 public static void main (string[] args) { 4 Temp Late temp = new Templateconcrete (); 5 Temp.update (); 6 7 }
public abstract class Template { public abstract void print (); public void update () {System.out.println ( "Start printing" ); for (int i = 0; i <; I++) {print (); } }}
1 Public class extends Template {23 @Override4public void print () {5 System.out.println ("This is the implementation of subclasses"); 6 }7 }
Description: Small series think this kind of template method limits the subclass extension.
Java design pattern--Behavioral pattern--template method