Class diagram
/** * Business process template, providing basic framework * @author Stone * */public abstract class Basetemplate {public abstract void Part1 ();p ublic Abstract V OID Part2 ();p ublic abstract void Part3 ();//here for strict experimental results, use final cannot be overridden public final void Usetemplatemethod () {part1 ();p Art2 ();p art3 ();}}
/** * Template Implementation Method 1 * @author Stone * */public class Templatemethod extends Basetemplate {@Overridepublic void Part1 () {SYSTEM.O Ut.println ("template Method 1");} @Overridepublic void Part2 () {System.out.println ("template Method 2"); @Overridepublic void Part3 () {System.out.println ("template Method 3");}}
/** * Template Implementation Method 2 * @author Stone * */public class TemplateMethod2 extends Basetemplate {@Overridepublic void Part1 () {System. Out.println ("Template Method 11");} @Overridepublic void Part2 () {System.out.println ("template Method 22"); @Overridepublic void Part3 () {System.out.println ("template Method 33");}}
/** * Template Method (Templatemethod) mode: Behavioral mode, defines a series of operations of the skeleton, the implementation of the module delay to the implementation of the class or sub-class implementation. * The difference from the builder model: * Builder mode, using a combination method, built by the builder of the Commander Director Command combination, the construction process is controlled by the director, the built module method is implemented by the Builder * Template method mode, defined by the template operation flow, subclasses or implementation class Implement specific module methods * In summary, the Builder mode is flexible in Template method mode * @author Stone * */public class Test {public static void main (string[] args) {basetemplate TM = new Templatemethod (); Tm.usetemplatemethod (); System.out.println (""); Basetemplate tm2 = new TemplateMethod2 (); Tm2.usetemplatemethod ();}}
Print
Template Method 1 Template Method 2 Template Method 3 Template Method 11 Template Method 22 Template Method 33
Java Implementation Template Method (Templatemethod) mode