职责链设计模式最简单的实例

Posted wuxun1997

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了职责链设计模式最简单的实例相关的知识,希望对你有一定的参考价值。

  啥是职责链设计模式、类图长啥样、有哪些应用场景等等这些网上一捞一大把,就不再费口水了。一图抵千言,一个例子抵一百个类图。不废话,上例子:

  1、职责接口:

package com.wlf.chain;

import com.wlf.entity.CommonRequest;
import com.wlf.entity.CommonResponse;

/**
 * 职责类
 */
public interface Responsibility {
    void execute(CommonRequest request, CommonResponse response, ResponsibilityChain chain);
}

 

  2、两个实现类:

package com.wlf.chain.concrete;

import com.wlf.chain.Responsibility;
import com.wlf.chain.ResponsibilityChain;
import com.wlf.entity.CommonRequest;
import com.wlf.entity.CommonResponse;

/**
 * 转写职责类
 */
public class TransferResponsibility implements Responsibility {
    @Override
    public void execute(CommonRequest request, CommonResponse response, ResponsibilityChain chain) {
        System.out.println("开始转写前处理一下...");
        chain.doExecute(request,response);
        System.out.println("结束转写前再搞一下...");
    }
}
package com.wlf.chain.concrete;

import com.wlf.chain.Responsibility;
import com.wlf.chain.ResponsibilityChain;
import com.wlf.entity.CommonRequest;
import com.wlf.entity.CommonResponse;

/**
 * 翻译职责类
 */
public class TranslateResponsibility implements Responsibility {
    @Override
    public void execute(CommonRequest request, CommonResponse response, ResponsibilityChain chain) {
        System.out.println("开始翻译前处理一下...");
        chain.doExecute(request,response);
        System.out.println("结束翻译前再搞一下...");
    }
}

 

  3、职责链类:

package com.wlf.chain;

import com.wlf.chain.concrete.TransferResponsibility;
import com.wlf.chain.concrete.TranslateResponsibility;
import com.wlf.entity.CommonRequest;
import com.wlf.entity.CommonResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 职责链
 */
public class ResponsibilityChain {

    // 职责链当前位置,从0开始
    private int position = 0;

    private List<Responsibility> responsibilities = new ArrayList<>();

    ResponsibilityChain addResponsibility(Responsibility responsibility) {
        responsibilities.add(responsibility);
        return this;
    }

    public void doExecute(CommonRequest request, CommonResponse response) {

        // 递归执行职责链,把所有职责都执行完递归就结束了
        if (position < responsibilities.size()) {
            Responsibility responsibility = responsibilities.get(position++);
            responsibility.execute(request, response, this);
            return;
        }

        // 职责链结束后执行业务逻辑
        System.out.println("do business...");
    }

    public static void main(String[] args) {
        ResponsibilityChain chain = new ResponsibilityChain();
        chain.addResponsibility(new TransferResponsibility());
        chain.addResponsibility(new TranslateResponsibility());
        chain.doExecute(new CommonRequest(), new CommonResponse());
    }
}

 

  运行日志:

com.wlf.chain.ResponsibilityChain
开始转写前处理一下...
开始翻译前处理一下...
do business...
结束翻译前再搞一下...
结束转写前再搞一下...

Process finished with exit code 0

 

以上是关于职责链设计模式最简单的实例的主要内容,如果未能解决你的问题,请参考以下文章

职责链模式

用于数据加载的 Android 活动/片段职责

JAVA设计模式大总结(二十三)--- b站尚硅谷设计模式之 职责链模式 图解 + 代码 整理(超详)

行为型模式之职责链模式

设计模式入门之职责链模式Chain Of Responsibility

(十五)职责链模式-代码实现