Template mode in Java:
Understanding: When defining a function, part of the function is deterministic, but there is another part of the uncertainty, and the identified part is in the use of the indeterminate part, then the indeterminate part is exposed, by the subclass of the class to complete.
That is, in a method in a class, partially determined, and the method includes an indeterminate method, which is an abstract method, with which the class is an abstract class, inheriting the abstract method can implement the indeterminate method.
Intent: The template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern. Is the basic technique of code reuse based on inheritance.
The behavior represented by a template method is called a top-level behavior, and its logic is called top-level logic.
1 /*2 requirement: Gets the time that a program runs. 3 principle: Get the program start and end time and subtract. 4 5 Acquisition Time: System.currenttimemillis ();6 7 This type of problem can be solved by completing the optimization of the current code. 8 9 this way, the template method design pattern. Ten One What is a template method? A when defining a feature, part of the feature is deterministic, but part of it is indeterminate, and the identified part is using the indeterminate part, - then the uncertain parts are exposed. It is done by subclasses of the class. - the - */
Abstract classgettime{ Public Final voidGetTime () {LongStart =System.currenttimemillis (); RunCode (); LongEnd =System.currenttimemillis (); System.out.println ("MS:" + (end-start)); } Public Abstract voidRunCode ();}classSubtimeextendsgettime{ Public voidRunCode () { for(intx=0; x<4000; X + +) {System.out.print (x); } }}classtemplatedemo{ Public Static voidMain (string[] args) {//GetTime GT = new GetTime ();Subtime GT =NewSubtime (); Gt.gettime (); }}
Reference Address: http://www.cnblogs.com/java-my-life/archive/2012/05/14/2495235.html
The template method of Java design pattern