Java design Pattern Template method pattern or template design pattern (belongs to the behavioral type)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.