Action pattern Template

Source: Internet
Author: User

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.

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.