1、编写抽象处理者角色Handler
package com.mazhichu.designpatterns.responsibility; import java.math.BigDecimal; /** * @ClassName: DealWorkInjuryRequestHandler * @Description: 抽象处理者角色 * @Author: Moore * @Date: 2020-06-29 9:39 * @Version: V1.0 */ public abstract class DealWorkInjuryRequestHandler { /** * 如果处理不了,设置后继处理者 */ protected DealWorkInjuryRequestHandler successor; /** * 处理请求的方法 * @param compensationPayments */ public abstract void dealWorkInjury(BigDecimal compensationPayments); public void setSuccessor(DealWorkInjuryRequestHandler successor) { this.successor = successor; } }2、编写具体处理者角色老李的亲戚老赵。
package com.mazhichu.designpatterns.responsibility; import java.math.BigDecimal; /** * @ClassName: LaoZhaoHandler * @Description: 具体处理者角色:老赵 * @Author: Moore * @Date: 2020-06-29 9:40 * @Version: V1.0 */ public class LaoZhaoHandler extends DealWorkInjuryRequestHandler{ @Override public void dealWorkInjury(BigDecimal compensationPayments) { System.out.println("老赵:老李别担心,赔偿款一共"+compensationPayments+"元,我已经找了他们大老板去处理了。"); successor.dealWorkInjury(compensationPayments); } }3、编写具体处理者角色承建公司大老板。
package com.mazhichu.designpatterns.responsibility; import java.math.BigDecimal; /** * @ClassName: LaoZhaoHandler * @Description: 具体处理者角色:承建公司大老板 * @Author: Moore * @Date: 2020-06-29 9:40 * @Version: V1.0 */ public class BossHandler extends DealWorkInjuryRequestHandler{ @Override public void dealWorkInjury(BigDecimal compensationPayments) { if(compensationPayments.compareTo(BigDecimal.valueOf(100000))>0){ System.out.println("大老板"+compensationPayments+"赔偿款,先拖拖,找人也不行。"); }else { System.out.println("大老板"+compensationPayments+"赔偿款,才这点钱,让项目经理去处理。"); successor.dealWorkInjury(compensationPayments); } } }4、编写具体处理者角色包工头
package com.mazhichu.designpatterns.responsibility; import java.math.BigDecimal; import java.math.MathContext; /** * @ClassName: LaoZhaoHandler * @Description: 具体处理者角色:承建公司包工头 * @Author: Moore * @Date: 2020-06-29 9:40 * @Version: V1.0 */ public class ContractorHandler extends DealWorkInjuryRequestHandler{ @Override public void dealWorkInjury(BigDecimal compensationPayments) { if(compensationPayments.compareTo(BigDecimal.valueOf(30000))>0){ System.out.println("包工头:上面压我,倒霉玩意,"+compensationPayments+"赔偿款,我要赔"+compensationPayments.multiply(BigDecimal.valueOf(0.3), MathContext.DECIMAL32)); }else { System.out.println("包工头:上面压我,倒霉玩意,我要自己掏"+compensationPayments+"赔偿款。"); } } }5、我们看看老李的维权之路是怎样的,编写客户端请求发送者角色老李。
package com.mazhichu.designpatterns.responsibility; import java.math.BigDecimal; /** * @ClassName: Laoli * @Description: 请求发送者角色:老李 * @Author: Moore * @Date: 2020-06-29 10:44 * @Version: V1.0 */ public class Laoli { public static void main(String[] args) { DealWorkInjuryRequestHandler lzHandler = new LaoZhaoHandler(); DealWorkInjuryRequestHandler bossHandler = new BossHandler(); DealWorkInjuryRequestHandler manageHandler = new ManageHandler(); DealWorkInjuryRequestHandler contractorHandler = new ContractorHandler(); lzHandler.setSuccessor(bossHandler); bossHandler.setSuccessor(manageHandler); manageHandler.setSuccessor(contractorHandler); lzHandler.dealWorkInjury(BigDecimal.valueOf(120000)); System.out.println("-------------------------------------------------\n"); lzHandler.dealWorkInjury(BigDecimal.valueOf(80000)); System.out.println("-------------------------------------------------\n"); lzHandler.dealWorkInjury(BigDecimal.valueOf(20000)); } }6、查看运行结果:
可以看出,在代码实例中我们创建了四个处理者对象,并指定第一个处理者对象的下家是第二个处理者对象,一直到第四个处理对象包工头为止。然后客户端老李将请求传递给第一个处理者对象老赵,老李不用再关心具体是谁处理了工伤赔偿。他只知道,如果没有老赵,他最终可能只能认命,听话不闹事就能拿到工资就好。
老李是太胆小又怕事,其实他完全可以拿着伤残鉴定去劳动局监察大队去投诉,这样就会在责任链上多一个具体处理者角色,监察大队会继续追查下去,找到链上该负责的处理对象,环环相扣,最终给老李一个交代。
总结