职责链模式(Chain of Responsibility)(对象行为型)

参考文档:

1.设计模式-可复用面向对象软件的基础

2.http://blog.csdn.net/hguisu/article/details/7547231(设计模式 ( 十二 ) 职责链模式(Chain of Responsibility)(对象行为型))


职责链模式(Chain of Responsibility)(对象行为型)

在这里讲的是书中实现的案例。

先看一下这个责任链模式的结构:


一个典型的对象结构可能如下图所示:


这一模式的想法是,给多个对象处理一个请求的机会,从而解耦发送者和接受者。该请求沿对象链传递直至其中一个对象处理它,如下图所示。


下面的交互框图(diagram) 说明了帮助请求怎样沿链传递:


代码实现:

下面的例子举例说明了在一个像前面描述的在线帮助系统中,职责链是如何处理请求的。

HelpHandler.java:
package com.rick.designpattern.chain_of_responsibility;

/**
 * Created by MyPC on 2017/6/16.
 */
public class HelpHandler {
    public static final int NO_HELP_TOPIC = -1;

    private HelpHandler successor;
    private int topic;


    public HelpHandler(HelpHandler helpHandler, int topic) {
        this.successor = helpHandler;
        this.topic = topic;
    }

    public void setHandler(HelpHandler h, int topic) {
        this.successor = h;
        this.topic = topic;
    }

    public boolean hasHelp() {
        return topic != NO_HELP_TOPIC;
    }

    public void handleHelp() {
        if (null != successor) {
            successor.handleHelp();
        }
    }
}
Widget.java:
package com.rick.designpattern.chain_of_responsibility;

/**
 * Created by MyPC on 2017/6/16.
 */
public class Widget extends HelpHandler {

    public Widget(Widget parent, int topic) {
        super(parent, topic);
    }
}
Dialog.java:
package com.rick.designpattern.chain_of_responsibility;

/**
 * Created by MyPC on 2017/6/16.
 */
public class Dialog extends Widget {

    public Dialog(HelpHandler helpHandler, int topic) {
        super(null, topic);
        setHandler(helpHandler, topic);
    }


    @Override
    public void handleHelp() {
        if (hasHelp()) {
            System.out.println("Dialog is handleHelp");
        } else {
            super.handleHelp();
        }
    }
}
Button.java:
package com.rick.designpattern.chain_of_responsibility;

/**
 * Created by MyPC on 2017/6/16.
 */
public class Button extends Dialog {

    public Button(Dialog parent, int topic) {
        super(parent, topic);
    }

    @Override
    public void handleHelp() {
        if (hasHelp()) {
            System.out.println("Button is handleHelp");
        } else {
            super.handleHelp();
        }
    }
}
Application.java:
package com.rick.designpattern.chain_of_responsibility;

/**
 * Created by MyPC on 2017/6/16.
 */
public class Application extends HelpHandler {
    public Application(int topic) {
        super(null, topic);
    }

    @Override
    public void handleHelp() {
        System.out.println("Application is handleHelp");
    }
}
Client.java:
package com.rick.designpattern.chain_of_responsibility;

import com.rick.designpattern.visitor.PricingVisitor;

/**
 * Created by MyPC on 2017/6/16.
 */
public class Client {
    public static final int PRINT_TOPIC = 1, PAPER_ORIENTATION_TOPIC = 2, APPLICATION_TOPIC = 3;


    public static void main(String[] args) {
        Application application = new Application(APPLICATION_TOPIC);
        Dialog dialog = new Dialog(application, PRINT_TOPIC);
        Button button = new Button(dialog, HelpHandler.NO_HELP_TOPIC);

        button.handleHelp();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值