1、策略枚举
2、责任链模式
1、策略枚举
- 使用策略枚举消除大量使用的if else
- 使用大量 if-else
public class IfService {public String getToDo(String day) {if ("Monday".equals(day)) {return "今天上英语课";} else if ("Tuesday".equals(day)) {return "今天上语文课";} else if ("Wednesday".equals(day)) {return "今天上数学课";} else if ("Thursday".equals(day)) {return "今天上音乐课";} else if ("sunday".equals(day)) {return "今天上编程课";} else {return null;}}public static void main(String[] args) {IfService ifService = new IfService();System.out.println(ifService.getToDo("Monday"));}
}
结果:
- 使用枚举类进行优化大量if -else
public class CheckDay {public String day(DayEnum dayEnum){return dayEnum.toDo();}}
public enum DayEnum {Monday{@Overridepublic String toDo(){return "周一";}},Tuesday{@Overridepublic String toDo(){return "周二";}},Wendsday{@Overridepublic String toDo(){return "周三";}},Thursday{@Overridepublic String toDo(){return "周四";}},Friday{@Overridepublic String toDo(){return "周五";}};public abstract String toDo();
}
public static void main(String[] args) {CheckDay checkDay = new CheckDay();//可以传递参数选择哪个 枚举对象String monday = checkDay.day(DayEnum.valueOf("Monday"));System.out.println(monday);}
}
- 另一种情况
public String getToDo(String day) {//第一种情况
// if ("Monday".equals(day)) {
// return "今天上英语课";
// } else if ("Tuesday".equals(day)) {
// return "今天上语文课";
// } else if ("Wednesday".equals(day)) {
// return "今天上数学课";
// } else if ("Thursday".equals(day)) {
// return "今天上音乐课";
// } else if ("sunday".equals(day)) {
// return "今天上编程课";
// } else {
// return null;
// }//第二种情况if("Monday".equals(day)||"Tuesday".equals(day)||"Wednesday".equals(day)){return "今天上英语课";}else if("Thursday".equals(day)){return null;}return null;}
public class CheckDay {public String dayNew(DayEnumNew dayEnumNew) {return dayEnumNew.toDo();}
}
public enum DayEnumNew {Monday(Type.ENGLISH, "星期一"),Tuesday(Type.ENGLISH, "星期二"),Wednesday(Type.ENGLISH, "星期三"),Thursday(Type.CHINESE, "星期四");private final Type type;private final String day;DayEnumNew(Type type, String day) {this.type = type;this.day = day;}String toDo(){return type.toDo();}/*** 内部策略枚举*/private enum Type {ENGLISH {@Overridepublic String toDo() {return "今天上英语课";}},CHINESE {@Overridepublic String toDo() {return "今天上语文课";}};public abstract String toDo();}
}
public static void main(String[] args) {CheckDay checkDay = new CheckDay();//可以传递参数选择哪个 枚举对象
// String monday = checkDay.day(DayEnum.valueOf("Monday"));
// System.out.println(monday);String monday = checkDay.dayNew(DayEnumNew.valueOf("Monday"));String thursday = checkDay.dayNew(DayEnumNew.valueOf("Thursday"));String thursday1 = checkDay.dayNew(DayEnumNew.valueOf("Wednesday"));System.out.println(monday);System.out.println(thursday);System.out.println(thursday1);}
结果:
2、责任链模式
用『责任链模式』来优化 参数多重校验,非常优雅!
以前在做需求,写一个方法,先在前面做验证,
if不满足A条件则return,
if不满足B条件则return…
一共写了5个验证,等验证通过以后才执行下面的逻辑,
后来提了需求,跟这个方法类似,
只不过验证条件稍微有点不一样,变成6个验证了
处理器:
public abstract class Handler<T> {protected Handler next;private void next(Handler next) {this.next = next;}public abstract void doHandler(User user);public static class Builder<T> {private Handler<T> head;private Handler<T> tail;public Builder<T> addHandler(Handler handler) {if (this.head == null) {this.head = this.tail = handler;return this;}this.tail.next(handler);this.tail = handler;return this;}public Handler<T> build() {return this.head;}}
}
@Data
public class User {private String userName;private String passWord;private String roleName;
}
进行校验:
public class LoginHandler extends Handler{@Overridepublic void doHandler(User user) {if (!"GJ".equals(user.getUserName()) && !"123456".equals(user.getPassWord())){System.out.println("用户名或密码错误!");
// return;}//判断是否还有下一个处理器,如果有则继续处理if (null!=next){next.doHandler(user);}}
}
public class AuthHandler extends Handler{@Overridepublic void doHandler(User user) {if (!"管理员".equals(user.getRoleName())){System.out.println("不是管理员,不允许访问");
// return;}if (next != null){next.doHandler(user);}}
}
public class LogicHandler extends Handler {@Overridepublic void doHandler(User user) {System.out.println("逻辑处理");if (next != null){next.doHandler(user);}}}
测试方法
public class Test {public static void main(String[] args) {Handler.Builder builder = new Handler.Builder<>();builder.addHandler(new LoginHandler()).addHandler(new AuthHandler()).addHandler(new LogicHandler());User user = new User();user.setUserName("GJ");user.setPassWord("123456");user.setRoleName("普通用户");builder.build().doHandler(user);}
}
结果: