Abstract class, unchanging code is written, to change the part left to the subclass to achieve:
Package Behavior _ template method mode;//template design mode means that the invariant positioning part is written out, the change of the part left to the subclass to achieve public abstract class GetTime {public long GetTime () throws Exception{long start = System.currenttimemillis ();//Start Time/*//for loop for (int i=0;i<10000;i++) {System.out.println (i) ;} *//*//video /design mode/src/avi/a.avibufferedinputstream input= new Bufferedinputstream (New FileInputStream ("/design mode/src/ Avi/a.avi ")); Bufferedoutputstream output = new Bufferedoutputstream (New FileOutputStream ("B.avi")); byte[] B = new Byte[1024];int len= 0;while ((Len=input.read (b))!=-1) {output.write (B,0,len);} Output.close (); Input.close ();//Give me another test of code: the multi-threaded operation of the collection operation, Common API, then you also write code, Trouble!!!!!!!!!!!!!!!! */code ();//Sub-class to write Long end = System.currenttimemillis ();//end Time return end-start;} Write an abstract method, the subclass itself to implement the method of the specific operation (content) public abstract void Code ();//left subclass implementation}
For loop run time class:
Package Behavior _ template method mode; public class Forgettime extends gettime{@Overridepublic void code () {for (int i=0;i<10000;i++) { System.out.println (i);}}}
Io stream reads the video and asks for its run time class:
Package Behavior _ template method mode; import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;public class IOGetTime extends gettime{@Overridepublic Void Code () {try {bufferedinputstream input = new Bufferedinputstream (New FileInputStream ("avi/ A.avi ")); Bufferedoutputstream output = new Bufferedoutputstream (New FileOutputStream ("B.avi")); byte[] B = new Byte[1024];int len= 0;while ((Len=input.read (b))!=-1) {output.write (B,0,len);} Output.close (); Input.close ();} catch (IOException e) {e.printstacktrace ();}}}
Test class:
Package Behavior _ template method mode; public class Gettimemain {public static void main (string[] args) throws Exception {//gettime gtime = NE W GetTime ();//system.out.println (Gtime.gettime () + "millisecond")///For abstract programming GetTime GT = new Forgettime (); System.out.println (Gt.gettime () + "MS");//re-assigned to read IO stream. avigt = new Iogettime (); System.out.println (Gt.gettime () + "MS");}}
Java design Pattern Template method pattern or template design pattern (belongs to the behavioral type)