zoukankan      html  css  js  c++  java
  • 职责连模式的使用场景

    static interface Response {
            void process();
        }
    
        static abstract class CallServiceHandler {
            CallServiceHandler nextHandle;
            
            abstract CallServiceHandler process();
            
            protected final void callBackFn(Response responase ) {
                responase.process();
            }
            
            protected final CallServiceHandler execute() {
                process();
                if(nextHandle != null ){
                    nextHandle.process();
                }
                return this;
            }
            
            protected final CallServiceHandler setNextHandle(CallServiceHandler nextHandle) {
                this.nextHandle = nextHandle;
                return this;
            }
            
        }
    
        static class RequestDnHandler extends CallServiceHandler {
    
            @Override
            CallServiceHandler process() {
                System.out.println("fetch data by url ....");
                return this;
            }
    
        }
    
        static class ProcessDnHandler extends CallServiceHandler {
    
            @Override
            CallServiceHandler process() {
                System.out.println("json to map ...");
                return this;
            }
        }
    public static void main(String[] args) {
            CallServiceHandler requesthandler = new RequestDnHandler();
            CallServiceHandler processHandler = new ProcessDnHandler();
            requesthandler.setNextHandle(processHandler).execute().callBackFn(new Response() {
                public void process() {
                    System.out.println("callback response..");
                }
            });
        }

    以上是职责链模式模拟的一个客户端请求服务器数据的处理过程,首先通过URL请求后台service,之后对后台service处理的结果进行处理,最后设置callback完成页面渲染。

    也可以给予对象的依赖设置 关系,设置数据流程处理关系。

  • 相关阅读:
    C#session配置
    在本地局域网 windows server 2008 下安装 Nginx 1.12.1
    博客园地址
    oracle 导入关键字说明
    oracle 导出关键字说明
    重塑和轴向旋转
    合并重叠数据
    Pandas 的轴向连接 concat
    c语言数据类型、运算符和表达式
    数据规整化:pandas 求合并数据集(交集并集等)
  • 原文地址:https://www.cnblogs.com/gstsyyb/p/5059449.html
Copyright © 2011-2022 走看看