Template method
Overview
Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
Applicability
1. One-time implementation of an invariant part of the algorithm, and the variable behavior is left to subclass to achieve. 2. The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication. 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. 3. Control sub-class extension.
participants
1.AbstractClass defines abstract primitive operations (primitiveoperation), and specific subclasses redefine them to implement the steps of an algorithm. implement a template method that defines the skeleton of an algorithm. the template method not only invokes primitive operations, it also invokes operations defined in AbstractClass or other objects. 2.ConcreteClass implements primitive operations to complete the steps associated with a particular subclass in the algorithm.
class Diagram
Example
AbstractClass
Public abstract class Template {public abstract void print (); public void Update () { System.out.println ("Start printing"); for (int i = 0; i < ten; i++) { print ();}} }
Concreteclass
public class Templateconcrete extends Template { @Override public void print () { System.out.println (" This is the implementation of the subclass ");} }
Test
public class Test {public static void Main (string[] args) { Template temp = new Templateconcrete (); Temp.update (); }}
result
Start printing This is the implementation of the subclass this is the implementation of the subclass this is the implementation of subclasses this is the implementation of the subclass this is the implementation of subclasses this is the implementation of subclasses this is the implementation of subclasses this is the implementation of subclasses this is the implementation of subclasses this is the implementation of subclasses
template Schema Definition: Defines the skeleton of an algorithm in an operation, delaying execution of some steps into its subclasses.
in fact, Java abstract class is originally template mode, so the use is very common. And it's easy to understand and use, so let's start with an example:
Public abstract class Benchmark
{
/**
* The following action is what we want to do in the subclass
* /
Public abstract void benchmark ();
/**
* Repeated execution of benchmark times
* /
Public final long repeat (int count) {
if (count <= 0)
return 0;
else {
Long startTime = System.currenttimemillis ();
for (int i = 0; i < count; i++)
benchmark ();
Long stoptime = System.currenttimemillis ();
return stoptime-starttime;
}
}
}
in the previous example, we wanted to repeat the benchmark () operation, but the specifics of benchmark () were not explained but deferred to the description in its subclasses:
Public class Methodbenchmark extends Benchmark
{
/**
* True definition of benchmark content
* /
Public void Benchmark () {
for (int i = 0; i < Integer.max_value; i++) {
System.out.printtln ("i=" +i);
}
}
}
at this point, template mode has been completed, is it very simple? See how to use:
Benchmark operation = new Methodbenchmark ();
Long Duration = Operation.repeat (Integer.parseint (Args[0].trim ()));
System.out.println ("The operation took" + Duration + "milliseconds");
Perhaps you have doubts about the use of abstract classes, now you should thoroughly understand it? As for the benefits of doing so, it is obvious that the extensibility is strong, later benchmark content changes, I just need to do one more inheriting subclass, do not have to modify other application code.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java templates mode (template mode)