责任链模式

今儿写了一下责任链设计模式,代码来自于《Android源码设计模式解析与实战》。

程序猿狗屎运被派出去异国出差一周,这时候就要去申请一定的差旅费了,你心里小算一笔加上各种车马费估计大概要个两三万,于是先向小组长汇报申请,可是大于一千块小组长没权利批复,于是只好去找项目主管,项目主管一看妈蛋这么狠要这么多我只能批小于五千块的,于是你只能再跑去找部门经理,部门经理看了下一阵淫笑后说没法批我只能批小于一万的,于是你只能狗血地去跪求老总,老总一看哟!小伙子心忒黑啊!老总话虽如此但还是把钱批给你了毕竟是给公司办事,到此申请处理完毕,你也可以屁颠屁颠地滚了。

Programmer接口

public interface Programmer {

    public int getExpenses();
    
    public String getApply();
    
}

具体的AndroidProgrammer类

public class AndroidProgrammer implements Programmer {

    private int expenses;
    private String apply = "µù¸øµãÇ®³ö²î°¡";
    
    public AndroidProgrammer(int expenses){
        this.expenses = expenses;
    }
    
    public int getExpenses() {
        // TODO Auto-generated method stub
        return expenses;
    }

    public String getApply() {
        // TODO Auto-generated method stub
        return apply;
    }

}

Leader的抽象类

public abstract class Leader {

    private int expenses;
    private Leader mSuperiorLeader;
    
    public Leader(int expenses){
        this.expenses = expenses;
    }
    
    public void setLeader(Leader mSuperiorLeader){
        this.mSuperiorLeader = mSuperiorLeader;
    }
    
    public abstract void reply(Programmer program);
    
    public void handleRequest(Programmer program){
        if (expenses >= program.getExpenses()) {
            reply(program);
        }else {
            if (mSuperiorLeader != null) {
                mSuperiorLeader.handleRequest(program);
            }else {
                System.out.println("GoogBye my money");
            }
        }
    }   
}

小组长类

public class GroupLeader extends Leader {

    public GroupLeader(int expenses) {
        super(expenses);
        // TODO Auto-generated constructor stub
    }

    public void reply(Programmer program) {
        System.out.println(program.getApply());
        System.out.println(program.getExpenses());
        System.out.println("GroupLeader:Of course yes!");
    }
}

主管类

public class Director extends Leader {

    public Director(int expenses) {
        super(expenses);
        // TODO Auto-generated constructor stub
    }

    public void reply(Programmer program) {
        System.out.println(program.getApply());
        System.out.println(program.getExpenses());
        System.out.println("Director:Of course yes!");
    }
}

项目经理类

public class Manager extends Leader {

    public Manager(int expenses) {
        super(expenses);
        // TODO Auto-generated constructor stub
    }

    public void reply(Programmer program) {
        System.out.println(program.getApply());
        System.out.println(program.getExpenses());
        System.out.println("Manager:Of course yes!");
    }
}

老板类

public class Boss extends Leader {

    public Boss(int expenses) {
        super(expenses);
        // TODO Auto-generated constructor stub
    }

    public void reply(Programmer program) {
        System.out.println(program.getApply());
        System.out.println(program.getExpenses());
        System.out.println("Boss:Of course yes!");
    }
}

客户端类

public class Client {

    public static void main(String[] args) {
        Programmer programmer = new AndroidProgrammer((int)(Math.random()*40000));
        
        Leader groupLeader = new GroupLeader(1000);
        Leader directorLeader = new Director(5000);
        Leader managerLeader = new Manager(10000);
        Leader bossLeader = new Boss(40000);
        
        groupLeader.setLeader(directorLeader);
        directorLeader.setLeader(managerLeader);
        managerLeader.setLeader(bossLeader);
        
        groupLeader.handleRequest(programmer);
        
    }   
}

你可能感兴趣的:(责任链模式)