package cn.bunny.pattern7.demo1;

import lombok.Setter;

public class Chain {

    // 过滤垃圾请求，为了演示需要清除【垃圾，辣鸡，腊鸡】这些词
    public static void main(String[] args) {
        Handler handlerA = new ConcreteHandlerA();
        Handler handlerB = new ConcreteHandlerB();
        Handler handlerC = new ConcreteHandlerC();

        // 设置责任链
        handlerA.setNextHandler(handlerB);
        handlerB.setNextHandler(handlerC);

        // 发起请求
        handlerA.handleRequest("过滤垃圾请求，为了演示需要清除【垃圾，辣鸡，腊鸡】这些词");
    }

    // ConcreteHandler类
    static class ConcreteHandlerA extends Handler {
        @Override
        public void handleRequest(String request) {
            // 处理"垃圾"
            String updatedRequest = request.replaceAll("垃圾", "");
            System.out.println("Handler A处理后: " + updatedRequest);

            if (nextHandler != null) {
                nextHandler.handleRequest(updatedRequest);  // 传递修改后的请求
            }
        }
    }

    static class ConcreteHandlerB extends Handler {
        @Override
        public void handleRequest(String request) {
            // 处理"辣鸡"
            String updatedRequest = request.replaceAll("辣鸡", "");
            System.out.println("Handler B处理后: " + updatedRequest);

            if (nextHandler != null) {
                nextHandler.handleRequest(updatedRequest);  // 传递修改后的请求
            }
        }
    }

    static class ConcreteHandlerC extends Handler {
        @Override
        public void handleRequest(String request) {
            // 处理"腊鸡"
            String updatedRequest = request.replaceAll("腊鸡", "");
            System.out.println("Handler C处理后: " + updatedRequest);

            if (nextHandler != null) {
                nextHandler.handleRequest(updatedRequest);  // 传递修改后的请求
            }
        }
    }

    // Handler类
    @Setter
    abstract static class Handler {
        protected Handler nextHandler;

        public abstract void handleRequest(String request);
    }
}

