责任链模式【行为模式】

责任链模式

Avoid coupling the sender of a request to its receiver by giving more than one object
a chance to handle the request. Chain the receiving objects and pass the request along
until an object handle it.
避免请求发送方与请求处理方耦合,责任链模式使得多个对象都有机会处理请求。
将请求处理方连成一条链,并将请求沿着处理链传递,直到有接收者处理该请求为止。
public class ResponsibilityChain {
    /**
     * 责任链模式:
     * Avoid coupling the sender of a request to its receiver by giving more than one object
     * a chance to handle the request. Chain the receiving objects and pass the request along
     * until an object handle it.
     * 避免请求发送方与请求处理方耦合,责任链模式使得多个对象都有机会处理请求。
     * 将请求处理方连成一条链,并将请求沿着处理链传递,直到有接收者处理该请求为止。
     */
    @Test
    public void all() {
        final ProblemImpl easy = ProblemImpl.builder()
                .type(ProblemType.EASY)
                .desc("注解的元注解有哪些").build();
        final ProblemImpl middle = ProblemImpl.builder()
                .type(ProblemType.MIDDLE)
                .desc("AtomicInteger 实现原理").build();
        final ProblemImpl hard = ProblemImpl.builder()
                .type(ProblemType.HARD)
                .desc("Jvm 内存结构").build();

        final IHandle context = HandlerContext.get();
        /**
         * 问题总是从责任链的头部开始执行
         */
        context.handle(easy);
        context.handle(middle);
        context.handle(hard);
    }
}

/**
 * 1)请求处理方法支持的请求类型
 */
enum ProblemType {
    EASY, MIDDLE, HARD, POWER_LESS
}
/**
 * 2)请求详情接口,包括请求类型
 */
interface IProblem {
    ProblemType type();

    String desc();
}

/**
 * 3)抽象的处理者,该处理者封装了请求处理过程。
 */
@Data
@Slf4j
@RequiredArgsConstructor
abstract class AHandler {
    private final ProblemType type;
    private final AHandler nextHandler;

    public final void handle(IProblem problem) {
        if (type == problem.type()) {
            log.info("问题被解决 {}", problem.desc());
        } else if (nextHandler != null) {
            nextHandler.handle(problem);
        } else {
            log.info("问题无人解决");
        }
    }
}
/**
 * 4)具体的请求实现
 */
@Builder
@AllArgsConstructor
class ProblemImpl implements IProblem {
    private final ProblemType type;
    private final String desc;

    @Override
    public ProblemType type() {
        return type;
    }

    @Override
    public String desc() {
        return desc;
    }
}
/**
 * 5)具体的请求处理者
 */
class EasyHanlder extends AHandler {

    public EasyHanlder(AHandler nextHandler) {
        super(ProblemType.EASY, nextHandler);
    }

    private EasyHanlder(ProblemType type, AHandler nextHandler) {
        super(type, nextHandler);
    }

}
/**
 * 6)具体的请求处理者
 */
class HardHandler extends AHandler {

    public HardHandler(AHandler nextHandler) {
        super(ProblemType.HARD, nextHandler);
    }

    private HardHandler(ProblemType type, AHandler nextHandler) {
        super(type, nextHandler);
    }
}
/**
 * 7)对外提供的门面接口
 */
interface IHandle{
    void handle(IProblem problem);
}
/**
 * 8)处理请求的上下文
 */
class HandlerContext implements IHandle {
    public final AHandler handler;

    private static final HandlerContext CONTEXT = new HandlerContext();

    private HandlerContext() {
        handler = new EasyHanlder(new HardHandler(null));
    }

    public static final HandlerContext get() {
        return CONTEXT;
    }

    @Override
    public void handle(IProblem problem) {
        handler.handle(problem);
    }
}

猜你喜欢

转载自www.cnblogs.com/zhuxudong/p/10164755.html