# 通常写法

##### class SalaryCalculator
``````public static final int ROLE_TEACHER = 1;
public static final int ROLE_DEAN = 2;
public static final int ROLE_PRESIDENT = 3;

private int role;
private int leaveCount;//请假天数
private int workYear;
private int salary;
private int baseSalary;
//...
if(role== ROLE_TEACHER) {
baseSalary = workYear*100+10000; //1年工龄月薪+100
} else if(role== ROLE_DEAN) {
if(workYear >= 10) {
baseSalary = workYear*300+20000;
} else {
baseSalary = workYear*200+20000;
}
} else {
baseSalary = workYear*500+30000;
}
//...为什么不把上下2个if else里面的代码合并起来，因为这只是DEMO，简单化了真实场景，真实场景比如这2段代码都需要各自在不同的回调里处理呢
if(role== ROLE_TEACHER) {
salary = baseSalary/23*(23 - leaveCount);
} else if(role== ROLE_DEAN) {
leaveCount = Math.max(leaveCount - 2, 0); //院长每个月有额外的2天假期
salary = baseSalary/23*(23 - leaveCount);
} else {
// 校长请假有半薪，且每个月有额外的3天假期
leaveCount = Math.max(leaveCount - 3, 0);
salary = baseSalary/23*(23-leaveCount)+ baseSalary/23/2*leaveCount;
}
//...
``````

# 策略模式

##### class SalaryCalculator
``````public static final int ROLE_TEACHER = 1;
public static final int ROLE_DEAN = 2;
public static final int ROLE_PRESIDENT = 3;

private int role;
private int leaveCount;//请假天数
private int salary;
private SalaryStrategy strategy;
//...
if(role== ROLE_TEACHER) {
strategy = new TeacherSalaryStrategy ();
} else if(role== ROLE_DEAN) {
strategy = new DeanSalaryStrategy ();
} else {
strategy = new PresidentSalaryStrategy ();
}
//..
strategy.setWorkYear(workYear);
//...
strategy.setLeaveCount(leaveCount);
salary = strategy.getSalary();
//...
``````
##### interface SalaryStrategy
``````private void setWorkYear();
private void setLeaveCount();
private int getSalary();
``````

##### class TeacherSalaryStrategy implements SalaryStrategy
``````private int baseSalary=10000;
private int salary;

private void setWorkYear(int workYear){
baseSalary = workYear*100+salary;
}

private void setLeaveCount(int leaveCount){
salary = baseSalary/23*(23 - leaveCount);
}

private int getSalary(){
if(salary==0){
salary = baseSalary;
}
return salary;
}
``````

if(role== ROLE_TEACHER) {
} else if(role==ROLE_DEAN) {
} else {
}
（虽然DEMO只有2次，但是可以继续假设场景，写出N次if else）