(1) Template method definition:
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.
(2) Sample code:
1 //get the execution time of a method2 Abstract classGetTime3 {4 Public Final voidgetTime ()5 {6 LongStart =System.currenttimemillis ();7 8 RunCode ();9 Ten LongEnd =System.currenttimemillis (); One ASystem.out.println ("MS:" + (end-start)); - } - Public Abstract voidRunCode (); the - } - - //Concrete Method Implementation + classSubtimeextendsGetTime - { + A Public voidRunCode () at { - - for(intx=0; x<4000; X + +) - { - System.out.print (x); - } in } - } to + //Main function - classTemplatedemo the { * Public Static voidMain (string[] args) $ {Panax Notoginseng //GetTime GT = new GetTime (); -Subtime GT =Newsubtime (); the gt.gettime (); + } A}
Java Learning Template method design pattern