Home > Java > javaTutorial > body text

Algorithm framework definition and usage of Java template method pattern

WBOY
Release: 2023-05-08 16:46:16
forward
1484 people have browsed it

Introduction

The template method pattern in Java is a behavioral design pattern that is used to define the framework of an algorithm and allow subclasses to rewrite certain steps without changing the structure of the algorithm.

This mode is based on the open/closed principle, that is, it is open to extensions and closed to modifications. In the template method mode, the parent class defines the skeleton of an algorithm, and the subclass can customize certain functions by implementing the abstract method of the parent class. The behavior of the steps and the entire algorithm steps will not change due to changes in subclasses

The template method pattern usually includes an abstract class and several concrete subclasses. The abstract class defines the framework of the algorithm, including some basic Method and a template method. A template method is a template that contains the steps of an algorithm. Each step is implemented by an abstract method or a default method. Specific subclasses can implement their own behavior by implementing these abstract methods.

Abstract classes can also have hook functions. The hook function is usually a virtual function in the abstract class. Its default implementation is empty, so that subclasses can choose to override the function to implement specific behaviors. In the template method pattern ,Hook functions are usually used to perform specific operations or check conditions in some specific steps of an algorithm. Another use of hook functions is to provide a way to extend the algorithm. Subclasses can add additional steps by overriding the hook function or Modify the behavior of the algorithm. In short, the hook function is an important concept in the template method pattern, which can be used to affect the behavior of the algorithm or extend the functionality of the algorithm.

Implementation

First define the coffee abstract class

package com.fanqiechaodan.templatemethod;
/**
 * @Classname CoffeeMaker
 * @Description 咖啡抽象类
 */
public abstract class CoffeeMaker {
    public final void makeCoffee() {
        boilWater();
        brewCoffee();
        pourInCup();
        if (isCondiments()) {
            addCondiments();
        }
    }
    private void boilWater() {
        System.out.println("煮水");
    }
    protected abstract void brewCoffee();
    private void pourInCup() {
        System.out.println("倒入杯子中");
    }
    /**
     * 钩子函数;是否加入调料,由子类决定是否重写
     *
     * @return
     */
    protected boolean isCondiments() {
        return true;
    }
    protected abstract void addCondiments();
}
Copy after login

Secondly define two specific subclasses

public class AmericanoMaker extends CoffeeMaker{
    @Override
    protected void brewCoffee() {
        System.out.println("冲泡美式咖啡");
    }
    @Override
    protected void addCondiments() {
        System.out.println("加入牛奶和糖");
    }
    /**
     * 钩子函数;是否加入调料,由子类决定是否重写
     *
     * @return
     */
    @Override
    protected boolean isCondiments() {
        return false;
    }
}
public class LatteMaker extends CoffeeMaker{
    @Override
    protected void brewCoffee() {
        System.out.println("冲泡浓缩咖啡");
    }
    @Override
    protected void addCondiments() {
        System.out.println("添加奶泡和焦糖酱");
    }
}
Copy after login

Test

package com.fanqiechaodan.templatemethod;
/**
 * @author fanqiechaodan
 * @Classname Demo
 * @Description 模板方法模式
 * @Date 2023/3/9 18:57
 */
public class Demo {
    public static void main(String[] args) {
        CoffeeMaker americanoMaker = new AmericanoMaker();
        americanoMaker.makeCoffee();
        System.out.println();
        CoffeeMaker latteMaker = new LatteMaker();
        latteMaker.makeCoffee();
    }
}
Copy after login

Algorithm framework definition and usage of Java template method pattern

The above is the detailed content of Algorithm framework definition and usage of Java template method pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!