多个if-else代码: @RunWith(SpringRunner.class) @SpringBootTest public class EducationalBackgroundTest { private int year = 6; @Test public void normalIfElse(){ if ( year == 6){ System.out.println("小学毕业"); }else if ( year == 9){ System.out.println(&qu
最近在工作中优化了一段冗余的if else代码块,感觉对设计模式的理解和运用很有帮助,所以分享出来.鉴于原代码会涉及到公司的隐私,因此就不贴出来了.下面以更加通俗易懂的案例来解析. 假如写一个针对员工上班不遵守制度做相应惩罚的程序,比如,上班迟到:罚100:上班睡觉:罚1000:上班早退:警告:上班玩游戏:严重警告:上班谈恋爱:开除等,通常都会这样写: public class WorkPunish { public static void main(String[] agrs){ String
最近在工作中优化了一段冗余的if else代码块,感觉对设计模式的理解和运用很有帮助,所以分享出来.鉴于原代码会涉及到公司的隐私,因此就不贴出来了.下面以更加通俗易懂的案例来解析. 假如写一个针对员工上班不遵守制度做相应惩罚的程序,比如,上班迟到:罚100:上班睡觉:罚1000:上班早退:警告:上班玩游戏:严重警告:上班谈恋爱:开除等,通常都会这样写: public class WorkPunish { public static void main(String[] agrs){ String
一.需求 根据用户vip等级来返回不同的价格,vip等级是不固定的,随时可能要增加,价格也不是固定的. 二.常规的写法 /** * 如果有新增类型,就需要频繁的修改此处的代码! * 不符合开闭原则! * @author DUCHONG * */ public class CommonGetPrice { public static final String VIP1="v1"; public static final String VIP2="v2"; public
1.什么是策略模式? Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. 策略模式(Strategy Pattern):定义一族算法类,将每个算法分别封装起来,让它们可以互相替换. 2.策略模式定义 ①.Context封装角色 它也叫做上
今天介绍下策略模式,直接先上UML图 策略模式的概念 The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.–(翻译)– 定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换.策略模式使得