您的位置:首页 > 编程语言 > Java开发

java设计模式---职责链模式

2013-07-19 21:04 288 查看
对于职责链模式,一般是这么定义的:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,即——将这些对象连成一条(处理)链,并沿着这条链传递该请求,直到这条链结束同时有一个对象处理它为止。最经典的职责链模式应用就是JavaEE规范中web.xml的filter了,filters按照配置的顺序,依次处理根据其mapping条件过滤得到的web请求(response)/应答(request),从而可以实现诸如编码转换、事务封装、页面缓存等众多“职责”。给出职责链模式的适用范围:1、有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。2、想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。3、可处理一个请求的对象集合需要被动态指定。 下面这个范例模拟了一个软件行业中常见的项目处理流程,为一个项目(Project对象)的职责人员构造了一条处理链,然后向这个项目发出处理指令——从而在这条链上引发相关职责人员的处理。 代码如下:职责的接口定义,所有的职责人员都要具有这些功能:加入下一职责对象、处理消息(即行使职责)、获得下一职责对象。
**
 * 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类,将由它的对象引发这条“链”的连锁反应。
**
 * 职责链模式
 * <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的请求、应答……这就是职责链模式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: