Java design Pattern (iii) template mode

Source: Internet
Author: User

In an abstract class, the way in which methods are executed is publicly defined, subclasses can be implemented as required, but calls are made in the manner defined in the abstract class, typical applications such as banking processes, and brewing beverage processes. The following is a simple example, with boiling water brewing beverage, divided into four steps: boil, brew beverage, pour the drink into the cup, add seasoning.

1. Template abstract class

Points: 1) abstract class 2) Final modified template method

 Public Abstract classRefreshtemplate { Public Final voidRefresh () {//Boil the waterBiolwater (); //Processed BeverageBrew (); //Pour the drink into the cupPourincup (); //Add Seasoningaddcondiments (); }    Private voidBiolwater () {System.out.println ("Boil the Water"); }    protected Abstract voidBrew (); Private voidPourincup () {System.out.println ("Pour the drink into the cup"); }    protected Abstract voidaddcondiments ();}

2, coffee class, Inherit template class, rewrite brewing, add seasoning method

 Public class extends refreshtemplate {    protectedvoid  Brew () {        System.out.println ("brew coffee with boiling water") );    }     protected void addcondiments () {        System.out.println ("Add milk and sugar to the coffee");}    }

3. Tea class, Inherit template class, rewrite brewing, add seasoning method

 Public class extends refreshtemplate {    protectedvoid  Brew () {        System.out.println ("Soak in boiling water for 5 minutes ");    }     protected void addcondiments () {        System.out.println ("Add lemon");}    }

4. Testing

 Public class Model {    publicstaticvoid  main (string[] args) {        // refreshtemplate refresh = new Coffee ();        New Tea ();        Refresh.refresh ();    }}

Using the dynamic binding of Java (upward transformation), the final template method of the parent class is invoked to ensure that execution is performed in the manner defined by the abstract class.

Java design Pattern (iii) template 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.