The template method of Java design pattern

Source: Internet
Author: User

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

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.