求求你们了,别再写满屏的 if/ else 了!
为什么我们写的代码都是 if-else?
1//举例一:异常逻辑处理例子
2Object obj = getObj();
3if (obj != null) {
4 //do something
5}else{
6 //do something
7}
8
9//举例二:状态处理例子
10Object obj = getObj();
11if (obj.getType == 1) {
12 //do something
13}else if (obj.getType == 2) {
14 //do something
15}else{
16 //do something
17}
异常逻辑处理型重构方法实例一
1double disablityAmount(){
2 if(_seniority < 2)
3 return ;
4
5 if(_monthsDisabled > 12)
6 return ;
7
8 if(_isPartTime)
9 return ;
10
11 //do somethig
12}
1double disablityAmount(){
2 if(_seniority < 2 || _monthsDisabled > 12 || _isPartTime)
3 return ;
4
5 //do somethig
6}
异常逻辑处理型重构方法实例二
1double getPayAmount(){
2 double result;
3 if(_isDead) {
4 result = deadAmount();
5 }else{
6 if(_isSeparated){
7 result = separatedAmount();
8 }
9 else{
10 if(_isRetired){
11 result = retiredAmount();
12 else{
13 result = normalPayAmount();
14 }
15 }
16 }
17 return result;
18}
1double getPayAmount(){
2 if(_isDead)
3 return deadAmount();
4
5 if(_isSeparated)
6 return separatedAmount();
7
8 if(_isRetired)
9 return retiredAmount();
10
11 return normalPayAmount();
12}
异常逻辑处理型重构方法实例三
1public double getAdjustedCapital(){
2 double result = 0.0;
3 if(_capital > 0.0 ){
4 if(_intRate > && _duration >){
5 resutl = (_income / _duration) *ADJ_FACTOR;
6 }
7 }
8 return result;
9}
1public double getAdjustedCapital(){
2 if(_capital <= 0.0 ){
3 return 0.0;
4 }
5 if(_intRate > && _duration >){
6 return (_income / _duration) *ADJ_FACTOR;
7 }
8 return 0.0;
9}
1public double getAdjustedCapital(){
2 if(_capital <= 0.0 ){
3 return 0.0;
4 }
5 if(_intRate <= || _duration <= ){
6 return 0.0;
7 }
8
9 return (_income / _duration) *ADJ_FACTOR;
10}
异常逻辑处理型重构方法实例四
1 /* 查找年龄大于18岁且为男性的学生列表 */
2 public ArrayList<Student> getStudents(int uid){
3 ArrayList<Student> result = new ArrayList<Student>();
4 Student stu = getStudentByUid(uid);
5 if (stu != null) {
6 Teacher teacher = stu.getTeacher();
7 if(teacher != null){
8 ArrayList<Student> students = teacher.getStudents();
9 if(students != null){
10 for(Student student : students){
11 if(student.getAge() > = 18 && student.getGender() == MALE){
12 result.add(student);
13 }
14 }
15 }else {
16 logger.error("获取学生列表失败");
17 }
18 }else {
19 logger.error("获取老师信息失败");
20 }
21 } else {
22 logger.error("获取学生信息失败");
23 }
24 return result;
25 }
1 /* 查找年龄大于18岁且为男性的学生列表 */
2 public ArrayList<Student> getStudents(int uid){
3 ArrayList<Student> result = new ArrayList<Student>();
4 Student stu = getStudentByUid(uid);
5 if (stu == null) {
6 logger.error("获取学生信息失败");
7 return result;
8 }
9
10 Teacher teacher = stu.getTeacher();
11 if(teacher == null){
12 logger.error("获取老师信息失败");
13 return result;
14 }
15
16 ArrayList<Student> students = teacher.getStudents();
17 if(students == null){
18 logger.error("获取学生列表失败");
19 return result;
20 }
21
22 for(Student student : students){
23 if(student.getAge() > 18 && student.getGender() == MALE){
24 result.add(student);
25 }
26 }
27 return result;
28 }
状态处理型重构方法实例一
1double getPayAmount(){
2 Object obj = getObj();
3 double money = ;
4 if (obj.getType == 1) {
5 ObjectA objA = obj.getObjectA();
6 money = objA.getMoney()*obj.getNormalMoneryA();
7 }
8 else if (obj.getType == 2) {
9 ObjectB objB = obj.getObjectB();
10 money = objB.getMoney()*obj.getNormalMoneryB()+1000;
11 }
12}
1double getPayAmount(){
2 Object obj = getObj();
3 if (obj.getType == 1) {
4 return getType1Money(obj);
5 }
6 else if (obj.getType == 2) {
7 return getType2Money(obj);
8 }
9}
10
11double getType1Money(Object obj){
12 ObjectA objA = obj.getObjectA();
13 return objA.getMoney()*obj.getNormalMoneryA();
14}
15
16double getType2Money(Object obj){
17 ObjectB objB = obj.getObjectB();
18 return objB.getMoney()*obj.getNormalMoneryB()+1000;
19}
状态处理型重构方法实例二
1double getSpeed(){
2 switch(_type){
3 case EUROPEAN:
4 return getBaseSpeed();
5 case AFRICAN:
6 return getBaseSpeed()-getLoadFactor()*_numberOfCoconuts;
7 case NORWEGIAN_BLUE:
8 return (_isNailed)?:getBaseSpeed(_voltage);
9 }
10}
1class Bird{
2 abstract double getSpeed();
3}
4
5class European extends Bird{
6 double getSpeed(){
7 return getBaseSpeed();
8 }
9}
10
11class African extends Bird{
12 double getSpeed(){
13 return getBaseSpeed()-getLoadFactor()*_numberOfCoconuts;
14 }
15}
16
17class NorwegianBlue extends Bird{
18 double getSpeed(){
19 return (_isNailed)?:getBaseSpeed(_voltage);
20 }
21}
总结
近有有不少老铁在后台留言说,想进大厂,但是算法不好。近我整理了一份刷题实录,这份刷题实录,也让我进了心仪的大厂。现在开放分享给大家。希望对大家有所帮助。 任何的算法题,如同写作文一样,都有一些模板可以套用的。比如面试常考的DP(动态规划),难的是一些关键点是否能想清楚。比如你能写出动态转移方程,这题基本上就可以AC了。 整个刷题实录内容,包括 双子针、动态规划、二分查找、贪心算法、深度优先搜索、字符串、递归、字典树、排序、链表等相关专题内容。图文并茂,附有刷题答案源码。 刷题任务的题目,是根据题目的类型来汇总的,总结了八个类别,每个类别下面也总结了5个左右的题型,帮助大家分门别类的突破,所以刷起来相对会更有重点和针对性。如果从头到尾的刷,每周按顺序刷42题,很容易让自己坚持不下来,也会觉得很枯燥。所以在制定计划的时候可以让这个计划变得更“有趣"和针对性,让它看起来更容易实现一点,才会更容易坚持。
相关文章