码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-24 12:26:25      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:tran   pre   运行   inter   span   add   日志   例子   ini   

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

  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

 

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

标签:tran   pre   运行   inter   span   add   日志   例子   ini   

原文地址:https://www.cnblogs.com/wuxun1997/p/13186670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!