Design patterns learning summary (21) - Chain of Responsibility pattern

definition

Duty chain is to avoid a request sender and the receiver are coupled together, so that a plurality of objects are likely to receive a request to connect these objects form a chain, and the request is transmitted along the chain until an object handles it so far.

We can at any time increase or change a handler in the chain of responsibility pattern, even those who can change the order processing, increasing the flexibility of the system. Flexibility is increased by treatment, but sometimes may lead to a request in any case can not be processed, it will be placed at the end of the chain, the chain of responsibility is both an advantage and a disadvantage.

Character

Character:

  • Handler: abstract handler. It defines a method of processing the request. All handlers must implement the abstract class.
  • ConcreteHandler: specific handler. It is responsible for processing requests, as well as access to its successor. If it is able to handle the request, otherwise the request is passed to its successor.
  • Client: Client class.

Advantages and disadvantages

advantage:

  • Reduce the coupling. It requests the sender and receiver decoupling.

  • Simplified object. Such that the object does not need to know the structure of the chain.

  • Enhance the flexibility of assigning responsibilities to objects. By changing the members of the chain or the mobilization of their order, allows to dynamically add or delete liability.

  • Adding a new class of easily processing a request.

Shortcoming

  • Must be received request can not be guaranteed.

  • System performance will be affected, but not very convenient when performing debugging code; may cause the cycle call.

  • It may not be readily observable characteristic runtime hinder debugging.

Examples

Students leave

Leave alone objects:

/**
 * 请假单
 */
public class LeaveForm {

    /** 请假天数 **/
    private  int number;

    /** 请假人 **/
    private String person;

    public LeaveForm(String person,int number){
        this.person = person;
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getPerson() {
        return person;
    }

    public void setPerson(String person) {
        this.person = person;
    }
}

Abstract handler:

/**
 * 抽象处理者
 */
public abstract class Leader {

    /** 姓名 **/
    public String name;

    /** 后续处理者 **/
    protected Leader successor;

    public Leader(String name){
        this.name = name;
    }

    public void setSuccessor(Leader successor) {
        this.successor = successor;
    }

    public abstract void handleRequest(LeaveForm LeaveNode);

}

Specific treatment by:

/**
 * 辅导员
 */
public class Instructor extends Leader {
    public Instructor(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveForm LeaveNode) {
        //小于3天辅导员审批,否则传递给系主任
        if (LeaveNode.getNumber() <= 3) {
            System.out.println("辅导员" + name + "审批" + LeaveNode.getPerson()
                    + "同学的请假条,请假天数为"
                    + LeaveNode.getNumber() + "天。");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(LeaveNode);
            }
        }
    }
}

/**
 * 系主任
 */
public class DepartmentHead extends Leader {
    public DepartmentHead(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveForm LeaveNode) {
        //小于7天系主任审批,否则传递给院长
        if (LeaveNode.getNumber() <= 7) {
            System.out.println("系主任" + name + "审批" + LeaveNode.getPerson()
                    + "同学的请假条,请假天数为" + LeaveNode.getNumber() + "天。");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(LeaveNode);
            }
        }
    }
}

/**
 * 院长
 */
public class Dean extends Leader {

    public Dean(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveForm LeaveNode) {
        //小于10天院长审批,否则传递给校长
        if (LeaveNode.getNumber() <= 10) {
            System.out.println("院长" + name + "审批"
                    + LeaveNode.getPerson() + "同学的请假条,请假天数为"
                    + LeaveNode.getNumber() + "天。");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(LeaveNode);
            }
        }
    }
}

/**
 * 校长
 */
public class President extends Leader {
    public President(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveForm LeaveNode) {
        //小于15天校长长审批,否则不允批准
        if (LeaveNode.getNumber() <= 15) {
            System.out.println("校长" + name + "审批"
                    + LeaveNode.getPerson() + "同学的请假条,请假天数为"
                    + LeaveNode.getNumber() + "天。");
        } else {
            System.out.println("请假天天超过15天,不批准...");
        }
    }
}

test:

public static void main(String[] args) {
    Leader instructor = new Instructor("冯强");       //辅导员
    Leader departmentHead = new DepartmentHead("冯晓强");    //系主任
    Leader dean = new Dean("张妲强");      //院长
    Leader president = new President("王望望");     //校长

    instructor.setSuccessor(departmentHead);       //辅导员的后续者是系主任
    departmentHead.setSuccessor(dean);             //系主任的后续者是院长
    dean.setSuccessor(president);                  //院长的后续者是校长

    //请假3天的请假条
    LeaveForm leaveNode1 = new LeaveForm("张三", 3);
    instructor.handleRequest(leaveNode1);

    //请假9天的请假条
    LeaveForm leaveNode2 = new LeaveForm("李四", 9);
    instructor.handleRequest(leaveNode2);

    //请假15天的请假条
    LeaveForm leaveNode3 = new LeaveForm("王五", 15);
    instructor.handleRequest(leaveNode3);

    //请假20天的请假条
    LeaveForm leaveNode4 = new LeaveForm("赵六", 20);
    instructor.handleRequest(leaveNode4);
}

Console output:

辅导员冯强审批张三同学的请假条,请假天数为3天。
院长张妲强审批李四同学的请假条,请假天数为9天。
校长王望望审批王五同学的请假条,请假天数为15天。
请假天天超过15天,不批准...

Guess you like

Origin www.cnblogs.com/markLogZhu/p/11582709.html