## springboot 下计谋模式的简朴使用
javascript中的事件
1.魂魄三问
- 接办前人(已跑路)项目快活否?
- 前人项目不写解释懵逼否?
- 一个要领中一堆if/else,且营业推断前提用简朴数字(或英文字母),不带解释,想打人否?
所以,关于上述三个问题,我写了此漫笔,然则————然并卵
这篇文章并不能让你不接办前人项目,并不能让你看懂没有解释的营业代码,也并不能让你今后不遇到if/else轰击波,然则——系尬系
鲁迅教师曾提倡过,假如你以为政府糜烂,那末你就应当勤奋考取公务员从政,去内部处理糜烂;假如你以为你的故乡竖立不够优美,那末你应当去竖立他;假如你以为群众素养不够高,那末你应当进步本身素养,身先士卒;假如你以为这功用完全是营业无理取闹,产物瞎接,那末你应当——好吧,营业爸爸统统都好。
瞎扯了半天,下面进入正题:
许多情况下,都没有一次到位得产物,也一般没有一次到位的营业需求,所以在后期的功用扩大中,很可能会形成不断的对原代码到场if/else分支推断,来满足新的营业需求,然则这关于后接办的程序员来讲,在一堆if/else,几百行代码中去疾速明白逻辑,并到场扩大,无疑是件很蛋疼的事(好吧,实在我的实在原因是:看起来有点丑),所以,怎样革新本身的if/else代码,让其看的不显痴肥,直观简约,我百度了一下,简朴写了份在springboot
环境下,应用战略形式革新代码的漫笔,作为新知识的get体味!(好吧,战略形式实在很罕见)
2.什么是战略形式
战略形式(Strategy Pattern):一个类的行动或其算法能够在运行时变动。这类范例的设想形式属于行动型形式。
运行时随时变动,有无想到spring的自动注入?天真,易扩大这也是其特性之一。有无详细的完成步骤呢?有的!
3.简朴完成步骤
简朴的一个需求
- 起首,先设想一个简朴的营业——新需求上,这个功用临时有三个分支推断,三个分支对应差别的实行逻辑:
- 1——孙悟空逻辑
- 2——二师兄逻辑
- 3——唐僧逻辑
那末,起首,我们固然能够用if/else,switch,来做分支推断处理,好的处理,项目上线,后期扩大——
- 营业A有发话了,我以为应当有4——沙师弟逻辑,对吧,是如许吧!好,我再加一个if,没问题的!
- 营业B倏忽有话发话了,我以为应当有5——三只眼逻辑,对吧,不能没有二郎神啊!好,我再加一个if,
ojbk
! - 营业C倏忽有主意了,咋不能忘了6——小白马吧,对吧,这么辛劳!嗯~~~,能够,应当能够疾速完成!
- 营业D又来了,我以为那白骨精挺美丽的,是吧?你(美丽你大爷)!!!!!!!好,另有无美丽的?
所以,这个分支的类,看起来有点难熬痛苦!
战略形式完成步骤
好的话不多说,战略形式下,我们能够怎样处理这个问题呢?
- 起首,能够竖立一个罗列类,用于存储分支推断的前提(也能够用静态变量)
public enum WestCommand {
W_SuWuKong(1,"孙爷爷","monkeyCommand"),
W_ZhuBaJie(2,"二师兄","pigCommand"),
W_TangSeng(3,"僧人","monkCommand");
private int value;
private String descreption;
private String beanName;
private WestCommand(int value,String descreption,String beanName){
this.value=value;
this.descreption=descreption;
this.beanName=beanName;
}
public int getValue(){
return value;
}
public String getDescreption(){
return descreption;
}
public String getBeanName(){
return beanName;
}
public static WestCommand getInstance(int value) {
for(WestCommand type : WestCommand.values()) {
if(type.getValue() == value) {
return type;
}
}
return null;
}
}
这个类用于存储分支推断前提,,,,,
- 竖立战略角色接口
/**
* @申明:
* @范例称号:StrategyCommand
* @建立者: Raiden
* @建立时候: 2020/2/11 12:01
* @修正者: Raiden
* @修正时候: 2020/2/11 12:01
*/
public interface StrategyCommand {
String process();
}
- 建立详细战略角色
/**
* @申明:唐僧
* @范例称号:MonkeyCommand
* @建立者: Raiden
* @建立时候: 2020/2/11 12:02
* @修正者: Raiden
* @修正时候: 2020/2/11 12:02
*/
@Service
public class MonkCommand implements StrategyCommand {
@Override
public String process() {
return "僧人未曾调戏妖精了,檀越照样别问了!!";
}
}
/**
* @申明:孙悟空
* @范例称号:MonkeyCommand
* @建立者: Raiden
* @建立时候: 2020/2/11 12:02
* @修正者: Raiden
* @修正时候: 2020/2/11 12:02
*/
@Service
public class MonkeyCommand implements StrategyCommand {
@Override
public String process() {
return "你孙爷爷叫你回家用饭了!!";
}
}
/**
* @申明:二师兄
* @范例称号:MonkeyCommand
* @建立者: Raiden
* @建立时候: 2020/2/11 12:02
* @修正者: Raiden
* @修正时候: 2020/2/11 12:02
*/
@Service
public class PigCommand implements StrategyCommand {
@Override
public String process() {
return "你二师兄走位相称风流!!";
}
}
- 建立上下文,猎取详细角色
这里起首须要建立一个spring的东西类,用于依据id猎取bean
/**
* @申明:
* @范例称号:StrategeContext
* @建立者: Raiden
* @建立时候: 2020/2/11 14:41
* @修正者: Raiden
* @修正时候: 2020/2/11 14:41
*/
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtils.context==null){
SpringUtils.context=applicationContext;
System.out.println("初始化容器胜利------------");
}
}
//猎取context
public static ApplicationContext getContext(){
return SpringUtils.context;
}
//经由过程称号猎取bean
public static Object getBean(String beanName){
return getContext().getBean(beanName);
}
/**
* 经由过程范例猎取bean
* @param tClass
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> tClass){
return getContext().getBean(tClass);
}
public static <T> T getBean(String beanName,Class<T> classType){
return getContext().getBean(beanName,classType);
}
}
然后,建立上下文
/**
* @申明:
* @范例称号:StrategyContext
* @建立者: Raiden
* @建立时候: 2020/2/11 15:05
* @修正者: Raiden
* @修正时候: 2020/2/11 15:05
*/
public class StrategyContext {
/**
* 猎取对应的command完成类
* @param value
* @param classType
* @param <T>
* @return
*/
public static <T> T getStrategyBean(Integer value,Class<T> classType){
return SpringUtils.getBean(
WestCommand.getInstance(value).getBeanName(),classType);
}
}
在这里,统统准备工作基础算是完成了,下面,我们来看看结果
这里我是应用的springboot+springcloud
建立了一个接口:
响应的掌握层:
/**
* @申明:
* @范例称号:StrategyCrtl
* @建立者: Raiden
* @建立时候: 2020/2/11 11:58
* @修正者: Raiden
* @修正时候: 2020/2/11 11:58
*/
@RestController
@RequestMapping("/strategy")
public class StrategyCrtl {
@GetMapping("/{number}")
public ResponseEntity<String> getMessage(@PathVariable Integer number){
StrategyCommand bean = StrategyContext.getStrategyBean(number, StrategyCommand.class);
return ResponseEntity.ok(bean.process());
}
}
营业逻辑干掉了if/else,启动效劳:
我们看一看看结果:1——孙悟空逻辑
2——二师兄逻辑
3——僧人逻辑
至于厥后万一须要美丽的白骨精,那末怎样做置信不必我多说,这看起来确切比if/else雅观了不少,有人说差不多,那是由于营业爸爸层就只跟你说了一句话,万一有几百句呢?
【5min+】 对象映射只有AutoMapper?试试Mapster