[代码优化]减少if else

代码已经上传github有兴趣的同学可以上去看看,欢迎补充
https://github.com/shubiaodian/operata_code

1.利用三元运算符,在if else分支较少的情况下

原始代码:

    public static String choice(String type){
        if("Integer".equals(type)){
            return "this is Integer";
        }
        return null;
    }

优化后的代码

    public static String choice(String type){
        return "Integer".equals(type)?"this is Integer":null;
    }

2-3的原始代码

    public static String choice(String type){
        if("Integer".equals(type)){
            return "this is Integer";
        }
        if("Long".equals(type)){
            return "this is Long";
        }
        if("String".equals(type)){
            return "this is String";
        }
        else {
            return "null";
        }
    }

2.传参为byte,char,short,int 的时候,可以使用switch case

    public static String choice(String type) {
        TypeEnum typeEnum = TypeEnum.getTypeEnumFromValue(type);
        switch (typeEnum.getCode()) {
            case 1:
                return "this is Integer";
            case 2:
                return "this is Long";
            case 3:
                return "this is String";
            default:
                return "null";
        }
    }

将各种类型通过枚举方式定义

public enum TypeEnum {
    NULL(-1,"null"),
    INTEGER(1,"Integer"),
    LONG(2,"Long"),
    STRING(3,"String");
    int code;
    String value;
    TypeEnum(int code,String value){
        this.code=code;
        this.value=value;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public static HashMap<String,TypeEnum> allValues=new HashMap<String, TypeEnum>(){
        {
            TypeEnum[] values=TypeEnum.values();
            for(int i=0;i<values.length;i++){
                this.put(values[i].value,values[i]);
            }
        }
    };

    public static TypeEnum getTypeEnumFromValue(String value){
        if(allValues.containsKey(value)) {
            return allValues.get(value);
        }
        return NULL;
    }
}

3.策略模式+工厂方法重构代码

策略接口

public interface Strategy {
    public String getType();
}

三种实现

public class IntegerTypeStrategy implements Strategy {
    public String getType() {
        return "this is Integer";
    }
}

public class LongTypeStrategy implements Strategy {
    public String getType() {
        return "this is Long";
    }
}


public class StringTypeStrategy implements Strategy {

    public String getType() {
        return "this is String";
    }
}

工厂方法

public class TypeFactory {
    public static HashMap<String,Strategy> strategys=new HashMap<String, Strategy>();
    static {
        strategys.put("Integer",new IntegerTypeStrategy());
        strategys.put("Long",new IntegerTypeStrategy());
        strategys.put("String",new IntegerTypeStrategy());
    }
    public static Strategy getStrategy(String type){
        return strategys.get(type);
    }
}

优化后的方法

    public static String choice3(String type){
        return TypeFactory.getStrategy(type).getType();
    }

4.利用枚举类重构代码

package com.optimize.code.goodcase.choice.choice4;

import java.util.HashMap;

/**
 * @author rockyluo
 * @date 2020-03-22 21:09
 **/
public enum TypeEnum {
    NULL(-1,"null"){
        @Override
        public String getType() {
            return "";
        }
    },
    INTEGER(1,"Integer"){
        @Override
        public String getType() {
            return "this is Integer";
        }
    },
    LONG(2,"Long"){
        @Override
        public String getType() {
            return "this is Long";
        }
    },
    STRING(3,"String"){
        @Override
        public String getType() {
            return "this is String";
        }
    }
    ;
    int code;
    String value;
    TypeEnum(int code, String value){
        this.code=code;
        this.value=value;
    }

    public int getCode() {
        return code;
    }

    public   abstract String getType();

    public void setCode(int code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public static HashMap<String, TypeEnum> allValues=new HashMap<String, TypeEnum>(){
        {
            TypeEnum[] values= TypeEnum.values();
            for(int i=0;i<values.length;i++){
                this.put(values[i].value,values[i]);
            }
        }
    };

    public static TypeEnum getTypeEnumFromValue(String value){
        if(allValues.containsKey(value)) {
            return allValues.get(value);
        }
        return NULL;
    }
}

优化之后的方法

    public static String choice(String type){

        return TypeEnum.getTypeEnumFromValue(type).getType();
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.面向对象的特征有哪些方面? 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面...
    浪花易逝阅读 664评论 0 5
  • 写着写着发现简书提醒我文章接近字数极限,建议我换一篇写了。 建议52:推荐使用String直接量赋值 一般对象都是...
    我没有三颗心脏阅读 1,379评论 2 4
  • __block和__weak修饰符的区别其实是挺明显的:1.__block不管是ARC还是MRC模式下都可以使用,...
    LZM轮回阅读 3,387评论 0 6
  • 一. Java基础部分.................................................
    wy_sure阅读 3,840评论 0 11
  • 今晚得空,看了口碑电影《驴得水》,电影一波三折,心情也是大起大浮。全篇的主人公,驴得水,开始只是一个虚构人物,...
    方小六阅读 667评论 1 3