Radio

一个小小程序员

0%

责任链模式

责任链模式

责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。

在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。

示例演示:
  1. 我们举个例子,王者荣耀中,如果你是黄金,那么你肯定要经历白银和青铜,如果你是白银,那么你肯定要经历青铜,就是等级是一级一级来的,我们创建一个people的抽象类,来说明这个级别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    package test.chain;

    public abstract class AbstractPeople {
    public static int gold = 1;
    public static int silver = 2;
    public static int bronze = 3;

    protected int level;

    protected AbstractPeople nextPeople;

    public void setNextPeople(AbstractPeople nextPeople) {
    this.nextPeople = nextPeople;
    }

    public void loadMessage(int level, String message) {
    if (this.level <= level) {
    write(message);
    }
    if (nextPeople != null) {
    nextPeople.loadMessage(level, message);
    }
    }

    abstract protected void write(String message);
    }
  2. 分别创建各个等级的实现类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package test.chain;

    public class GoldPeople extends AbstractPeople {

    public GoldPeople(int level) {
    this.level = level;
    }

    @Override
    protected void write(String message) {
    System.out.println("write goldpeople:" + message);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package test.chain;

    public class SilverPeople extends AbstractPeople {

    public SilverPeople(int level) {
    this.level = level;
    }

    @Override
    protected void write(String message) {
    System.out.println("write silverpeople:" + message);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package test.chain;

    public class BronzePeople extends AbstractPeople {

    public BronzePeople(int level) {
    this.level = level;
    }

    @Override
    protected void write(String message) {
    System.out.println("write bronzepeople:" + message);
    }
    }
  3. 完成上述步骤,我们就可以开始演示了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package test.chain;

    public class ChainDemo {
    public static AbstractPeople getPeople() {
    AbstractPeople goldPeople = new GoldPeople(AbstractPeople.gold);
    AbstractPeople silverPeople = new SilverPeople(AbstractPeople.silver);
    AbstractPeople bronzePeople = new BronzePeople(AbstractPeople.bronze);

    goldPeople.setNextPeople(silverPeople);
    silverPeople.setNextPeople(bronzePeople);
    return goldPeople;
    }

    public static void main(String[] args) {
    AbstractPeople abstractPeople = getPeople();
    abstractPeople.loadMessage(AbstractPeople.gold,"this is gold");
    System.out.println("---------------------------------");
    abstractPeople.loadMessage(AbstractPeople.silver,"this is silver");
    System.out.println("---------------------------------");
    abstractPeople.loadMessage(AbstractPeople.bronze,"this is bronze");
    }
    }

    运行结果:

    1
    2
    3
    4
    5
    6
    7
    8
    write goldpeople:this is gold
    ---------------------------------
    write goldpeople:this is silver
    write silverpeople:this is silver
    ---------------------------------
    write goldpeople:this is bronze
    write silverpeople:this is bronze
    write bronzepeople:this is bronze
说明:

优点: 1、降低耦合度。它将请求的发送者和接收者解耦。 2、简化了对象。使得对象不需要知道链的结构。 3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。 4、增加新的请求处理类很方便。

缺点: 1、不能保证请求一定被接收。 2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。 3、可能不容易观察运行时的特征,有碍于除错。

欢迎关注我的其它发布渠道