设计模式-责任链模式

2020-09-14  本文已影响0人  dashingqi

定义

UML图

模版代码

抽象处理类

public abstract class ChainHandler {

    ChainHandler nextChainHandler;
    
    public final HandleResponse handleMessage(HandleRequest handleRequest) {

        HandleResponse handleResponse = null;

        /**
         * 当前请求的等级与自己的等级是相符合的,就进行处理
         * 如果不是自己等级范围内的,就传递给下一个对象来处理
         */
        if (this.getHandleLevel() == handleRequest.getHandleLevel()) {
            handleResponse = handleMethod();
        } else {
            /**
             * 当没有下一个对象来处理这个请求,就返回一个自定义的Response
             */
            if (nextChainHandler != null) {
                handleResponse = this.nextChainHandler.handleMessage(handleRequest);

            } else {
                handleResponse = new HandleResponse();
                handleResponse.setResponseMessage("没有对象来处理,拒绝你的请求");
            }
        }

        return handleResponse;

    }

    /**
     * 获取到每个对象能处理的级别
     *
     * @return
     */
    public abstract int getHandleLevel();

    /**
     * 每个处理对象的处理方法
     */
    public abstract HandleResponse handleMethod();

    /**
     * 设置处理下一个处理请求的对象
     *
     * @param handler
     */
    public void setNextChainHandler(ChainHandler handler) {
        nextChainHandler = handler;
    }
}

处理的实现类

public class ChainHandlerOne extends ChainHandler {
    @Override
    public int getHandleLevel() {
        return ConstantLevel.LEVEL_ONE;
    }

    @Override
    public HandleResponse handleMethod() {
        HandleResponse handleResponse = new HandleResponse();
        handleResponse.setResponseMessage("身为大总管,我的考虑一下......,没问题,同意吧!");
        return handleResponse;
    }
}


public class ChainHandlerTwo extends ChainHandler {
    @Override
    public int getHandleLevel() {
        return ConstantLevel.LEVEL_TWO;
    }

    @Override
    public HandleResponse handleMethod() {
        HandleResponse handleResponse = new HandleResponse();
        handleResponse.setResponseMessage("同意,同意,必须得同意");
        return handleResponse;
    }
}


public class ChainHandlerThree extends ChainHandler {
    @Override
    public int getHandleLevel() {
        return ConstantLevel.LEVEL_THREE;
    }

    @Override
    public HandleResponse handleMethod() {
        HandleResponse handleResponse  = new HandleResponse();
        handleResponse.setResponseMessage("既然大家都不处理请求,到我这肯定得同意啊!");
        return handleResponse;
    }
}

客户端类

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private List<HandleRequest> requestList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 创建三个处理请求的对象
         */
        final ChainHandler chainHandlerOne = new ChainHandlerOne();
        ChainHandler chainHandlerTwo = new ChainHandlerTwo();
        ChainHandler chainHandlerThree = new ChainHandlerThree();

        /**
         * 形成请求处理链
         */
        chainHandlerOne.setNextChainHandler(chainHandlerTwo);
        chainHandlerTwo.setNextChainHandler(chainHandlerThree);

        /**
         * 创建三个请求
         */
        requestList = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 4; i++) {
            HandleRequest handleRequest = new HandleRequest();
            handleRequest.setHandleLevel(random.nextInt(4));
            requestList.add(handleRequest);
        }


        findViewById(R.id.btnStartRequest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < requestList.size(); i++) {
                   HandleResponse handleResponse =  chainHandlerOne.handleMessage(requestList.get(i));
                    Log.d(TAG,handleResponse.getResponseMessage());
                }
            }
        });

    }
}

优缺点

优点

缺点

注意事项

上一篇下一篇

猜你喜欢

热点阅读