java design pattern template

background

Logically speaking, if someone writes well, then what else should I write? Nothing, I just feel like I can remember it more clearly if I write it down.
The definition of a template is probably something where most of the steps are the same for everyone, and even some steps are performed exactly the same. At this time, someone abstracted this matter into a template, so that people behind could complete it according to the template.

Define the algorithm skeleton in one operation, deferring some implementation steps to subclasses. It allows subclasses to redefine certain steps in the algorithm without changing the structure of the algorithm. Its essence is to define the algorithm skeleton and let subclasses implement it themselves.

scene

For example, the steps for a new employee to join the company: Submit necessary entry materials to HR (required for entry process review), take a physical examination, and go to the company to complete the onboarding process.

package com.aliyu.learn.pattern.template;

/**
 * 描述:入职步骤模板
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-20 4:26 下午
 */
public abstract class EntryTemplate {
    
    

    /**
     * 入职人的姓名(模板共有属性,在模板初始化时传入)
     */
    protected String personName;

    public EntryTemplate(String personName) {
    
    
        this.personName = personName;
    }

    /**
     * 提交材料,具体材料肯定是每个人都不一样
     * 交由新人自己实现
     * protected保证只能被子类访问
     * 非公共的抽成抽象方法,由子类自己实现
     */
    protected abstract void uploadData();

    /**
     * 入职体检,啥时候体检,去哪里体检都不一定一样
     * 交由新人自己实现
     */
    protected abstract void checkBody();

    /**
     * 公司报道,具体报道时间不一样
     * 交由新人自己实现
     */
    protected abstract void checkIn();

    /**
     * 但是大家都会经历这3个步骤
     * 公共的结构化的逻辑在抽象类中实现
     * 用final修饰,保证子类不会修改
     */
    public final void CheckInStep(){
    
    
        uploadData();
        checkBody();
        checkIn();
    }



}

ps: The public step logic is implemented in the abstract class (CheckInStep) and modified with final to prevent modification by subclasses. Non-public logic is abstracted into abstract methods and modified with protected to ensure that they can only be implemented by the subclass itself.

Simulate a Zhang San template:

package com.aliyu.learn.pattern.template;

/**
 * 描述:张三入职
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-20 4:49 下午
 */
public class EntryOne extends EntryTemplate{
    
    
    
    public EntryOne(String personName) {
    
    
        super(personName);
    }

    @Override
    protected void uploadData() {
    
    
        System.out.println("新人张三上传文档");
    }

    @Override
    protected void checkBody() {
    
    
        System.out.println("张三入职体检");
    }

    @Override
    protected void checkIn() {
    
    
        System.out.println("张三公司报道");
    }
}

John Doe’s template:

package com.aliyu.learn.pattern.template;

/**
 * 描述:李四入职
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-20 4:49 下午
 */
public class EntryLi extends EntryTemplate{
    
    

    public EntryLi(String personName) {
    
    
        super(personName);
    }

    @Override
    protected void uploadData() {
    
    
        System.out.println("李四上传文档");
    }

    @Override
    protected void checkBody() {
    
    
        System.out.println("李四入职体检");
    }

    @Override
    protected void checkIn() {
    
    
        System.out.println("李四公司报道");
    }
}

Test Zhang San and Li Si to "join the job" according to the template:

package com.aliyu.learn.pattern.template;

/**
 * 描述:
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-20 5:00 下午
 */
public class TestTemplate {
    
    

    public static void main(String[] args) {
    
    

        EntryLi entryLi = new EntryLi("李四");
        entryLi.checkInStep();

        EntryZhang entryZhang = new EntryZhang("张三");
        entryZhang.checkInStep();

    }
}

Insert image description here

hook method

Why is it called the hook method? do not know. The reason for this is that, for example, in the above three steps, if you have a physical examination report within one year, then there is no need for a physical examination. But this step may be a rare case, and you don't want to rewrite a template for this. Of course, you can say that people who don’t do physical examinations can just do nothing when implementing the physical examination method. This way of writing is inelegant and can easily lead to misunderstandings. If there is, there is, if there is not, there is not. You implemented the "physical check" method but did nothing?
Implementation code:

package com.aliyu.learn.pattern.template;

/**
 * 描述:入职步骤模板
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-20 4:26 下午
 */
public abstract class EntryTemplate {
    
    

    /**
     * 入职人的姓名(模板共有属性,在模板初始化时传入)
     */
    protected String personName;

    public EntryTemplate(String personName) {
    
    
        this.personName = personName;
    }

    /**
     * 提交材料,具体材料肯定是每个人都不一样
     * 交由新人自己实现
     * protected保证只能被子类访问
     * 非公共的抽成抽象方法,由子类自己实现
     */
    protected abstract void uploadData();

    /**
     * 入职体检,啥时候体检,去哪里体检都不一定一样
     * 交由新人自己实现
     */
    protected abstract void checkBody();

    /**
     * 公司报道,具体报道时间不一样
     * 交由新人自己实现
     */
    protected abstract void checkIn();

    /**
     * 钩子方法,默认返回true
     * 默认"体检"
     * @return
     */
    protected boolean isCheckBody(){
    
    
        return true;
    }

    /**
     * 但是大家都会经历这3个步骤
     * 公共的结构化的逻辑在抽象类中实现
     */
    public final void checkInStep(){
    
    
        System.out.println("入职人:" + personName);
        uploadData();
        //是否执行体检交由"钩子方法"判断,而钩子方法可以被子类重写
        if(this.isCheckBody()){
    
    
            checkBody();
        }
        checkIn();
    }



}

ps: Notice that it adds a hook method "isCheckBody", which returns true by default.
At the same time, a judgment is added to the skeleton method "checkInStep", and the hook method is used to judge whether to have a physical examination. The hook method can be overridden by subclasses, which means that subclasses can decide whether to participate in the physical examination.

Wang Wu who did not participate in the physical examination:

package com.aliyu.learn.pattern.template;

/**
 * 描述:王五的体检
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-21 9:41 上午
 */
public class EntryWang extends EntryTemplate{
    
    

    private boolean checkBodyFlag = true;

    public EntryWang(String personName) {
    
    
        super(personName);
    }

    public EntryWang(String personName, boolean checkBodyFlag) {
    
    
        super(personName);
        this.checkBodyFlag = checkBodyFlag;
    }

    @Override
    protected void uploadData() {
    
    
        System.out.println("王五上传文档");
    }

    @Override
    protected void checkBody() {
    
    
        System.out.println("王五入职体检");
    }

    @Override
    protected void checkIn() {
    
    
        System.out.println("王五公司报道");
    }

    @Override
    protected boolean isCheckBody(){
    
    
        return this.checkBodyFlag;
    }
}

ps: Added an attribute "physical examination or not" and the corresponding construction method, so that Wang Wu can specify whether he will participate in the physical examination when initializing.

Test code:

package com.aliyu.learn.pattern.template;

/**
 * 描述:
 * <p>作者: aliyu
 * <p>创建时间: 2021-10-20 5:00 下午
 */
public class TestHookTemplate {
    
    

    public static void main(String[] args) {
    
    

        EntryLi entryLi = new EntryLi("李四");
        entryLi.checkInStep();

        EntryWang entryWang = new EntryWang("王五", false);
        entryWang.checkInStep();

    }
}

Insert image description here
It can be seen that Wang Wu does not have a physical examination.

other

Guess you like

Origin blog.csdn.net/mofsfely2/article/details/120868318