责任链模式

52 阅读2分钟

用途

关注的是请求的处理流程,它将多个处理器连接起来形成一个处理链,每个处理器负责处理一部分请求,或者将请求传递给下一个处理器。 主要用于处理请求的分发和处理,可以用于动态地组织和调整处理器的顺序和层次。 例如 Spring Cloud Alibaba Sentinel 中的设计模型

简单责任链模式

public class Payment {    
    private boolean success;    
    // 其他参数略....  
    public boolean isSuccess() {    
        return success;    
    }    
    public void setSuccess(boolean success) {    
        this.success = success;    
    }    
}
public interface PaymentProcessor {    
    /**    
    * 节点处理      
    * @param context    
    */    
    void handle(Payment context);    
}
@Order(1)    
@Component    
public class CreditCardProcessor implements PaymentProcessor {    
    @Override    
    public void handle(Payment context) {    
        System.out.println("Processed credit card payment.");    
    }    
}  
  
@Order(2)    
@Component    
public class PayPalProcessor implements PaymentProcessor {    
    @Override    
    public void handle(Payment context) {    
        System.out.println("Processed PayPal payment.");    
  
    }    
}
/**
* 管理这些实现类
* 采用spring注入list的形式,list顺序为上面实现类@Order的顺序
*/
@Service    
public class PaymentHandleChainService {    
    @Autowired    
    private List<PaymentProcessor> paymentProcessors;    
  
    public void execute(Payment payment) {    
        for (PaymentProcessor paymentProcessor : paymentProcessors) {    
            paymentProcessor.handle(payment);    
        }    
    }    
}
/**
* 测试类
*/
@RunWith(SpringRunner.class)    
@SpringBootTest(classes = SpringExampleApplication.class)    
public class PaymentServiceTest {    
    @Autowired    
    private PaymentHandleChainService paymentHandleChainService;    
  
    @Test    
    public void test() {    
        paymentHandleChainService.execute(new Payment());    
    }    
}

抽象类责任链

/**
* 抽象类
*/
public abstract class AbstractPaymentProcessor {    
    /**    
    * 下一个节点    
    */    
    protected AbstractPaymentProcessor next = null;    
  
    public void execute(Payment context) throws Exception {    
        // 上层未执行成功,不再执行    
        if (!context.isSuccess()) {    
            return;    
        }    
        // 执行当前阶段    
        doHandler(context);    
        // 判断是否还有下个责任链节点,没有的话,说明已经是最后一个节点    
        if (getNext() != null) {    
            getNext().execute(context);    
        }    
    }    
  
    public AbstractPaymentProcessor getNext() {    
        return next;    
    }    
  
    public void setNext(AbstractPaymentProcessor next) {    
        this.next = next;    
    }    
  
    public abstract void doHandler(Payment content) throws Exception;    
  
    public static class Builder {    
        private AbstractPaymentProcessor head;    
        private AbstractPaymentProcessor tail;    
  
        public Builder addHandler(AbstractPaymentProcessor handler) {    
            if (this.head == null) {    
                this.head = handler;    
            } else {    
                this.tail.setNext(handler);    
            }    
            this.tail = handler;    
            return this;    
        }    
  
        public AbstractPaymentProcessor build() {    
            return this.head;    
        }    
    }    
}
/**
* 实现类
*/
@Component    
public class CreditCard2Processor extends AbstractPaymentProcessor {    
    
    @Override    
    public void doHandler(Payment content) throws Exception {    
        System.out.println("Processed credit card payment.");    
    }    
}  
  
@Component    
public class PayPal2Processor extends AbstractPaymentProcessor {    
    
    @Override    
    public void doHandler(Payment content) throws Exception {    
        System.out.println("Processed PayPal payment.");    
    }    
}
/**
* 测试类
*/
@Test    
public void test2() throws Exception {    
    paymentHandleChainService.execute(new Payment());    
    new AbstractPaymentProcessor.Builder()    
        .addHandler(creditCard2Processor)    
        .addHandler(payPal2Processor)    
        .build().execute(new Payment());    
}