Chain of Responsibility Model--Pass the interview

Primer

The HR of Xiaoshuai Company asked Xiaoshuai to design the interview process for the technicians of the company. The interview at the beginning of the design is divided into three rounds, the first round is a technical interview, the second round is a written test, and the third round is a boss interview.

Later, as more and more people came for interviews, the company's staff was tense, and technical interviews could not be arranged. Xiaoshuai discussed with HR and added a round of phone interviews before the technical interviews for a preliminary screening. HR asked Xiaoshuai to maintain sufficient flexibility when designing the interview process. He can add or delete interview nodes at any time. For example, in the future, it may be necessary to add an ideological and moral interview link, cancel the boss interview link, and so on.

Xiaoshuai thought about it carefully, isn't this the chain of responsibility model?

chain of responsibility model

Chain of Responsibility: Allows you to send requests along a chain of handlers. After receiving the request, each processor can process the request or pass it to the next processor in the chain. The Chain of Responsibility pattern is a behavioral design pattern.

insert image description here

Xiaoshuai wrote down the function of the interview using the chain of responsibility model.

Event handling class:

/**
 * 事件处理类
 */
public abstract class Handler {
    
    

    /**
     * 下一个处理者
     */
    protected Handler nextHandler = null;

    /**
     * 设置一下个处理者
     * @param nextHandler
     * @return
     */
    public Handler setNextHandler(Handler nextHandler) {
    
    
        this.nextHandler = nextHandler;
        return nextHandler;
    }

    /**
     * 处理的方法
     * @param abilityValue 面试者的能力值
     */
    public abstract void handle(int abilityValue);
}

Phone interview category:

/**
 * 电话面试
 */
public class TelephoneInterviewHandler extends Handler{
    
    

    /**
     * 合格能力值
     */
    final static int PASS_ABILITY_VALUE = 60;

    /**
     * 处理的方法
     */
    @Override
    public void handle(int abilityValue) {
    
    
        if(abilityValue > PASS_ABILITY_VALUE) {
    
    
            System.out.println("电话面试通过。");
            if(nextHandler != null) {
    
    
                nextHandler.handle(abilityValue);
            } else {
    
    
                System.out.println("成功入职!");
            }
        } else {
    
    
            System.out.println("电话面试失败。");
        }
    }
}

In-person interview class:

/**
 * 当面面试
 */
public class FaceInterviewHandler extends Handler{
    
    

    /**
     * 合格能力值
     */
    final static int PASS_ABILITY_VALUE = 70;

    /**
     * 处理的方法
     *
     * @param abilityValue 能力值
     */
    @Override
    public void handle(int abilityValue) {
    
    
        if(abilityValue > PASS_ABILITY_VALUE) {
    
    
            System.out.println("当面面试通过。");
            if(nextHandler != null) {
    
    
                nextHandler.handle(abilityValue);
            } else {
    
    
                System.out.println("成功入职!");
            }
        } else {
    
    
            System.out.println("当面面试失败。");
        }
    }
}

Written test category:

/**
 * 笔试
 */
public class WrittenTestHandler extends Handler{
    
    

    /**
     * 合格能力值
     */
    final static int PASS_ABILITY_VALUE = 80;

    /**
     * 处理的方法
     *
     * @param abilityValue 能力值
     */
    @Override
    public void handle(int abilityValue) {
    
    
        if(abilityValue > PASS_ABILITY_VALUE) {
    
    
            System.out.println("笔试通过。");
            if(nextHandler != null) {
    
    
                nextHandler.handle(abilityValue);
            } else {
    
    
                System.out.println("成功入职!");
            }
        } else {
    
    
            System.out.println("笔试失败。");
        }
    }
}

Boss interview class:

/**
 * 老板面试
 */
public class BossInterviewHandler extends Handler{
    
    

    /**
     * 合格能力值
     */
    final static int PASS_ABILITY_VALUE = 90;

    /**
     * 处理的方法
     *
     * @param abilityValue 能力值
     */
    @Override
    public void handle(int abilityValue) {
    
    
        if(abilityValue > PASS_ABILITY_VALUE) {
    
    
            System.out.println("老板面试通过。");
            System.out.println("成功入职!");
        } else {
    
    
            System.out.println("老板面试失败。");
        }
    }
}

Client class:

/**
 * 客户端类
 */
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Handler telephoneInterviewHandler = new TelephoneInterviewHandler();
        telephoneInterviewHandler.setNextHandler(new FaceInterviewHandler())
                .setNextHandler(new WrittenTestHandler())
                .setNextHandler(new BossInterviewHandler());
        telephoneInterviewHandler.handle(91);
    }
}

output:

电话面试通过。
当面面试通过。
笔试通过。
老板面试通过。
成功入职!

If you don’t need a boss interview, just remove the boss interview link:

/**
 * 客户端类
 */
public class Client {
    
    
    public static void main(String[] args) {
    
    
        Handler telephoneInterviewHandler = new TelephoneInterviewHandler();
        telephoneInterviewHandler.setNextHandler(new FaceInterviewHandler())
                .setNextHandler(new WrittenTestHandler());
        telephoneInterviewHandler.handle(85);
    }
}

output:

电话面试通过。
当面面试通过。
笔试通过。
成功入职!

Summarize

When the program needs to process requests in different ways, and the processing method is uncertain and may be adjusted dynamically, the chain of responsibility mode is suitable for use. This mode can connect multiple processors into a chain, and all requests will pass through the processors on the chain in strict order, and you can also dynamically adjust processors, add, delete or adjust their order.

advantage

  • You can flexibly control the order of request processing and the objects to be processed.
  • Comply with the single responsibility principle, decoupling the client and handler classes.
  • Complies with the principle of opening and closing, and can easily add processors.

shortcoming

  • Some requests may not be processed.
  • Increased the complexity of the code, there may be many handler classes.

Guess you like

Origin blog.csdn.net/zhanyd/article/details/118804121