Design Pattern-Template Method

Source: Internet
Author: User

Design Pattern-Template Method
Description:


Define the skeleton of an algorithm in a method. Delay some steps to the subclass. The template method allows the subclass to redefine some steps in the algorithm without changing the algorithm structure.
HOOK: defines an empty method or default method, and provides the user subclass to overwrite and implement its own judgment and function based on the situation. The hook can affect the algorithm flow in the abstract class. You can set a hook when the algorithm is optional. Make the subclass have the ability to make some decisions for its abstract class.

Class diagram:


The following program simulates the process of making different drinks.

1. Define a beverage abstract class with caffeine

package net.dp.templatemethod.barista; public abstract class CaffeineBeverage {       final void prepareRecipe() {        boilWater();        brew();        pourInCup();        addCondiments();    }      abstract void brew();       abstract void addCondiments();      void boilWater() {        System.out.println("Boiling water");    }       void pourInCup() {        System.out.println("Pouring into cup");    }}
2. Implement the beverage abstract class

package net.dp.templatemethod.barista; public class Tea extends CaffeineBeverage {    public void brew() {        System.out.println("Steeping the tea");    }    public void addCondiments() {        System.out.println("Adding Lemon");    }}

package net.dp.templatemethod.barista; public class Coffee extends CaffeineBeverage {    public void brew() {        System.out.println("Dripping Coffee through filter");    }    public void addCondiments() {        System.out.println("Adding Sugar and Milk");    }}

3. Test

package net.dp.templatemethod.barista; public class BeverageTestDrive {    public static void main(String[] args) {          Tea tea = new Tea();        Coffee coffee = new Coffee();          System.out.println("\nMaking tea...");        tea.prepareRecipe();          System.out.println("\nMaking coffee...");        coffee.prepareRecipe();     }}



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.