设计模式——职责链模式

职责链模式

  • 职责链模式
  • 职责链模式解决什么问题?
  • 职责链模式实现

职责链模式

使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象练成一条链,并沿着这条链传递该请求,知道有一个对象处理它为止

职责链模式解决什么问题?

如请假请求需要主管、经理、总经理批准

public class Request {
    private String type;
    private String content;
    private int number;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getNumber() {
        return number;
    }

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

创建类处理请求

public class Manager {
    protected String name;

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

    public void getResult(Request request) {
        if (name.equals("主管")) {
            if (request.getNumber() <= 2) {
                System.out.println(name + "批准" + request.getContent() + "," + request.getType() + request.getNumber() + "天");
            } else {
                System.out.println(name + "无权处理");
            }
        } else if (name.equals("经理")) {
            if (request.getNumber() <= 5) {
                System.out.println(name + "批准" + request.getContent() + "," + request.getType() + request.getNumber() + "天");
            } else {
                System.out.println(name + "无权处理");
            }
        } else if (name.equals("总经理")) {
            System.out.println(name + "批准" + request.getContent() + "," + request.getType() + request.getNumber() + "天");
        }
    }
}

当提出请求时

Request request = new Request();
request.setContent("家里有事");
request.setType("请假");
request.setNumber(10);

Manager manager = new Manager("主管");
manager.getResult(request);
Manager director = new Manager("经理");
director.getResult(request);
Manager generalDirector = new Manager("总经理");
generalDirector.getResult(request);
  • problem1:分支判断过长
  • problem2:当新增新的管理类别时,都要更改类

职责链模式实现

创建管理者类

abstract class Manager {
    protected String name;
    protected Manager superior;

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

    public void setSuperior(Manager superior) {
        this.superior = superior;
    }

    public abstract void requestApplications(Request request);
}

创建职责链处理请求

class CommonManager extends Manager {
    public CommonManager(String name) {
        super(name);
    }

    @Override
    public void requestApplications(Request request) {
        if (request.getNumber() <= 2) {
            System.out.println(name + "批准" + request.getContent() + "," + request.getType() + request.getNumber() + "天");
        } else {
            System.out.println(name + "无权处理");
            if (superior != null) {
                superior.requestApplications(request);
            }
        }
    }
}

class Direct extends Manager {
    public Direct(String name) {
        super(name);
    }

    @Override
    public void requestApplications(Request request) {
        if (request.getNumber() <= 5) {
            System.out.println(name + "批准" + request.getContent() + "," + request.getType() + request.getNumber() + "天");
        } else {
            System.out.println(name + "无权处理");
            if (superior != null) {
                superior.requestApplications(request);
            }
        }
    }
}

class GeneralDirector extends Manager {
    public GeneralDirector(String name) {
        super(name);
    }

    @Override
    public void requestApplications(Request request) {
        System.out.println(name + "批准" + request.getContent() + "," + request.getType() + request.getNumber() + "天");
    }
}

传递过程

CommonManager manager = new CommonManager("主管");
Direct director = new Direct("经理");
GeneralDirector generalDirector = new GeneralDirector("总经理");

manager.setSuperior(director);
director.setSuperior(generalDirector);

Request request = new Request();
request.setContent("家里有事");
request.setType("请假");
request.setNumber(10);

manager.requestApplications(request);

你可能感兴趣的:(#,设计模式,设计模式,java,开发语言)