Design mode Chapter 02 Template design mode

Source: Internet
Author: User
Tags abstract

I. Demand analysis

The steps to solve a certain kind of thing are fixed, at which point we can provide a template code for this kind of thing, thereby increasing the efficiency

Second, the pattern design:

1. Write a solution to one thing in this category (including the template section and the change section)
2. Extract the changed parts into a method that describes them as abstract methods
3. Use final cosmetic template method to prevent being rewritten

Third, code implementation

Use a simple little example to illustrate: the execution time of the calculation program

3.1 Tool Code

Abstract class calruntime{

Public final void GetTime () {
Long starttime = System.currenttimemillis (); The time when the record started
Code ();
Long endtime = System.currenttimemillis (); The time at which the record ended.
System.out.println ("Running Time:" + (Endtime-starttime));
}

public abstract void Code ();
}

3.2 Test Code

Class Testcal extends Calruntime
{
    public static void Main (string[] args)
  & nbsp {
        testcal  testcal = new testcal ();
        testcal.gettime ();
   }
    
    public  Void Code () {
        //your code
   }

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.