/**
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