代码已经上传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();
}
网友评论