Java Implementation Template Method (Templatemethod) mode

Source: Internet
Author: User

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

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.