Template definition:
Define
Skeleton to delay the execution of some steps to its subclass.
In fact, the abstract class of Java is
Template mode, so it is widely used. It is easy to understand and use. Let's start with an example:
Public Abstract class Benchmark {
/**
*
The following operations we want to complete in the subclass:
*/
Public Abstract void benchmark ();
/** * Repeated benchmark execution 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 above example, we want to repeat
The benchmark () operation is not described for the specific content of benchmark (), but is delayed to the description in its subclass:
Public Class methodbenchmark extends Benchmark {
/**
*
Real Definition Benchmark
Content
*/
Public Void benchmark (){
For (INT I = 0; I <integer. max_value; I ++ ){
System. Out. printtln ("I =" + I );
}
} }
|
So far, the template mode has been completed, yes
Isn't it easy? See how to use:
Benchmark operation = new
Methodbenchmark ();
Long Duration =
Operation. Repeat (integer. parseint (ARGs [0]. Trim ()));
System. Out. println ("
Operation took "+ duration +" milliseconds ");
Maybe you used to wonder what the abstract class is for. Now you
Should I fully understand it? As for the benefits of doing so, it is clear that the scalability is strong, and the benchmark content will change in the future, I only need to make another inheritance subclass, without having to modify other application code.