Template method Mode

Source: Internet
Author: User

/**
Design pattern------Template Method Pattern:
Defines a method that gets the time the program runs, requiring that any program run can be implemented,
The time that the program was obtained.

First get the program run time method unchanged and gettime (), change is to run the program RunCode ().
You can then define an abstract class, which is decorated with the final gettime (), with the abstract RunCode ().

The subclass inherits the abstract class, and the RunCode () is then overwrite. Implements the time to get different programs running.

Leak out the indeterminate method and let the subclass overwrite it.


A more formal concept:
1) Template method pattern is based on the inheritance of code reuse Basic Technology, template method pattern structure and usage is also one of the core object-oriented design.
In the template method pattern, you can put the same code in the parent class and put different method implementations in different subclasses.
2) In the template method pattern, we need to prepare an abstract class that implements some of the logic in the form of concrete methods and concrete constructors.
Then declare some abstract methods to let subclasses implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus making the remaining logic
There are different implementations, which is the intent of the template method pattern. The template method pattern embodies many important ideas of object-oriented, and is a mode with high frequency.
*/
Abstract class gettime{
Public final void GetTime (String className) {
Long start = System.currenttimemillis ();
RunCode ();
Long end = System.currenttimemillis ();
System.out.println (classname+ "Run Time is" + (End-start));
}
public abstract void RunCode ();
}
Class Subtime extends gettime{
public void RunCode () {
for (int i=0;i<1000;i++) {
System.out.print (i);
}
}
}
Class Whiletime extends gettime{
public void RunCode () {
int i=0;
while (i<=1000) {
i++;
System.out.print (i);
}
}
}
public class template{
public static void Main (String []args) {
Subtime st = new Subtime ();
St.gettime ("St");
Whiletime wt = new Whiletime ();
St.gettime ("wt");
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Template method Mode

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.