zoukankan      html  css  js  c++  java
  • [转]23种经典设计模式的java实现_5_职责链模式

    对于职责链模式,一般是这么定义的:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,即——将这些对象连成一条(处理)链,并沿着这条链传递该请求,直到这条链结束同时有一个对象处理它为止。

    最经典的职责链模式应用就是JavaEE规范中web.xml的filter了,filters按照配置的顺序,依次处理根据其mapping条件过滤得到的web请求(response)/应答(request),从而可以实现诸如编码转换、事务封装、页面缓存等众多“职责”。

    给出职责链模式的适用范围:

    1、有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。

    2、想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。

    3、可处理一个请求的对象集合需要被动态指定。

    下面这个范例模拟了一个软件行业中常见的项目处理流程,为一个项目(Project对象)的职责人员构造了一条处理链,然后向这个项目发出处理指令——从而在这条链上引发相关职责人员的处理。代码如下:

    职责的接口定义,所有的职责人员都要具有这些功能:加入下一职责对象、处理消息(即行使职责)、获得下一职责对象。

    package com.alex.designpattern.chainofresponsibility;

    /**
     * The interface of the chain You can use AddChain function to modify the chain
     * dynamically.
     * 
     * 
    @author <a href="mailto:huqiyes@gmail.com">huqi</a>
     * 
    @serialData 2007
     
    */

    public interface Chain {
        public void addChain(Chain c);

        public void sendToChain(String mesg);

        public Chain getChain();
    }

    以下就是我们流程中所有的职责人员实现啦,依次为经理、项目经理、程序员、测试员及其它,它们根据mesg参数分别处理自己的消息——即行使特定的职责

    package com.alex.designpattern.chainofresponsibility;

    /**
     * A beginner of the chain The resposibility of manager is to get a project
     
    */

    public class Manager implements Chain {
        private Chain nextChain = null;

        private static final String Responsibility = "Getting Project";

        public void addChain(Chain c) {
            nextChain = c;
        }


        public Chain getChain() {
            return nextChain;
        }


        public void sendToChain(String mesg) {
            if (mesg.equals(Responsibility)) {
                System.out.println("A Manager --> " + mesg);
            }
     else {
                if (nextChain != null{
                    nextChain.sendToChain(mesg);
                }

            }

        }


    }


    package com.alex.designpattern.chainofresponsibility;

    /**
     * A member of the chain The resposibility of PM is to design the project
     
    */

    public class ProjectManager implements Chain {
        private Chain nextChain = null;

        private static final String Responsibility = "Design";

        public void addChain(Chain c) {
            nextChain = c;
        }


        public Chain getChain() {
            return nextChain;
        }


        public void sendToChain(String mesg) {
            if (mesg.equals(Responsibility)) {
                System.out.println("A Project Manager --> " + mesg);
            }
     else {
                if (nextChain != null{
                    nextChain.sendToChain(mesg);
                }

            }

        }


    }


    package com.alex.designpattern.chainofresponsibility;

    /**
     * A member of the chain The resposibility of Programmer is coding
     
    */

    public class Programmer implements Chain {
        private Chain nextChain = null;

        private static final String Responsibility = "Coding";

        public void addChain(Chain c) {
            nextChain = c;
        }


        public Chain getChain() {
            return nextChain;
        }


        public void sendToChain(String mesg) {
            if (mesg.equals(Responsibility)) {
                System.out.println("A Programmer --> " + mesg);
            }
     else {
                if (nextChain != null{
                    nextChain.sendToChain(mesg);
                }

            }

        }


    }


    package com.alex.designpattern.chainofresponsibility;

    /**
     * A member of the chain<br>
     * The resposibility of QA is test
     
    */

    public class QA implements Chain {
        private Chain nextChain = null;

        private static final String Responsibility = "Testing";

        public void addChain(Chain c) {
            nextChain = c;
        }


        public Chain getChain() {
            return nextChain;
        }


        public void sendToChain(String mesg) {
            if (mesg.equals(Responsibility)) {
                System.out.println("A QA --> " + mesg);
            }
     else {
                if (nextChain != null{
                    nextChain.sendToChain(mesg);
                }

            }

        }

    }


    package com.alex.designpattern.chainofresponsibility;

    /**
     * The end of the chain<br>
     * The resposibility of Others is handle exeception
     
    */

    public class Others implements Chain {
        private Chain nextChain = null;

        public void addChain(Chain c) {
            nextChain = c;
        }


        public Chain getChain() {
            return nextChain;
        }


        public void sendToChain(String mesg) {
            System.out.println("None can handle --> " + mesg);
        }


    }

    下面当然是测试的主程序了,首先我们按照顺序初始化这条“链”——项目来了,先是部门经理过目,然后交给相关项目经理做设计,有了设计程序员就可以编码了,完工后测试人员测试,最后来到职责链末尾结束(当然,真实的情况下,不会有这么顺利的项目:-)。注意我们在这里定义了一个Project类,将由它的对象引发这条“链”的连锁反应。

    package com.alex.designpattern.chainofresponsibility;

    /**
     * 职责链模式
     * <p>
     * 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。<br>
     * 将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
     *
     * 
    @author <a href="mailto:huqiyes@gmail.com">huqi</a>
     * 
    @serialData 2007
     
    */

    public class Test {
        public static void main(String[] args) {

            Project project = new Project();

            Manager aManager = new Manager();
            ProjectManager aPM = new ProjectManager();
            Programmer aProgrammer = new Programmer();
            QA aQA = new QA();
            Others others = new Others();
            // init chain
            aManager.addChain(aPM);
            aPM.addChain(aProgrammer);
            aProgrammer.addChain(aQA);
            aQA.addChain(others);

            project.setChain(aManager);

            project.getChain().sendToChain("Getting Project");
            project.getChain().sendToChain("Design");
            project.getChain().sendToChain("Coding");
            project.getChain().sendToChain("Testing");
            project.getChain().sendToChain("Over");
        }

    }


    class Project {
        private Chain chain;

        public Chain getChain() {
            return chain;
        }


        public void setChain(Chain chain) {
            this.chain = chain;
        }

    }

    结合这个例子,那就很容易理解web.xml中常用的filter了吧,每个filter都是职责链中的一环,它们按照自己的职责过滤web的请求、应答……这就是职责链模式。

  • 相关阅读:
    Atitit ..Net Framework sdk 3.0 3.5 4.04.5 4.6 4.7  .net core版本新特性 v2 s22 1. 新特性来源于 down ms 官方网站 1
    Atitit 工业体系的分类 目录 1. 有多少个灰色 黑色行业?? 1 2. 30个制造业行业细分 制造业下面还有更精细的划分,见下表1.2: 2 3. 艾提拉需要掌握的体系 3 4. 艾提拉需要
    Atitit 提升开发效率 通道化驱动化数据操作与查询 目录 1. xiaoguo 1 1.1. Atitit mybatis 简化开发与提升开发效率法 1 2. Code 1 3. ref 4
    Atitit 常见聚合运算与算法 目录 1.1. 单行函数,vs 那就是聚合函数 1 1.2. 聚合分类 哈希聚合 标量聚合 流聚合 1 1.3. 常见聚合函数 mysql oracle等 1 r
    Atitit git 使用法v2 目录 1. Git客户端工具 1 1.1. Tortoisegit git gui 图形化工具。。 1 1.2. Ide中的git插件(eclipse idea
    Atitit 提升开发效率的方法 提升语言级别 目录 1. 提升语言级别到4gl 1 1.1. 语言的代际关系 sql 》script 》java 1 1.2. 使用4gl dsl语言与api 1
    Atitit mybatis返回多个数据集总结 目录 1.1. 配置handleResult接受,但是只有第一个select语句的结果 1 2. 配置resultMap ok 1 2.1. 调
    Atitit pdf转文本 pdf2txt v4 t83.docx Atitit pdf转文本 pdfutil 目录 1.1. Pdfbox cmd 模式 TextToPDF 1 1.2. Pdf
    Atitit mybatis 简化开发与提升开发效率法 目录 1.1. 使用注解模式代替xml模式 1 1.2. 使用通用mapper代替专用mapper 1 1.3. 使用js等脚本语言来输出sq
    Atitit mq的AMQP 协议 STOMP2 、MQTT3 等协议  MQTT,XMPP,STOMP,AMQP,WAMP 目录 1. AMQP in a Nutshell 1 2. MQTT概述
  • 原文地址:https://www.cnblogs.com/rosesmall/p/2418683.html
Copyright © 2011-2022 走看看