修改私有字段的多个if语句

时间:2015-05-07 10:42:05

标签: java if-statement

我在Java中定义了一个类A,如下所示:

public class A
{
    private String fieldName;
    private String detailFieldName;
    private String externalFieldName;
    private int formatLength;
    /* additional private members */

    void setProperty( String aPropertyName, String aPropertyValue )
    {
        try
        {
            if( aPropertyName.equals( AField.FieldName.getName() ) )
            {
                fieldName = aPropertyValue;
                return;
            }
            if( aPropertyName.equals( AField.DetailFieldName.getName() ) )
            {
                detailFieldName = aPropertyValue;
                return;
            }
            if( aPropertyName.equals( AField.ExternalFieldName.getName() ) )
            {
                externalFieldName = aPropertyValue;
                return;
            }
            if( aPropertyName.equals( AField.FormatLength.getName() ) )
            {
                formatLength = Integer.parseInt( aPropertyValue );
                return;
            }
            /* more if's */
        }
        catch( Exception e )
        {
            /* logging exceptions */
        }
    }
}

进一步的if是类比的,即它们都修改了类A的一个特定私有成员变量。

AField的定义如下:

public enum AField
{
    FieldName( "FieldName" ),
    DetailFieldName( "DetailViewFieldName" ),
    ExternalFieldName( "ExternalFieldName" ),
    FormatLength( "FormatLength" )
    /* more fields */

    private final String name;

    AField(String aName)
    {
        name = aName;
    }

    public String getName()
    {
        return name;
    }
}

我想用更合适,更易于维护的解决方案替换多个if .. return ..语句。我读到了使用地图将每个AField值作为关键字并使用适当的命令(命令设计模式)作为值。但是,我的问题是,在每个if语句中,我修改了A类私有成员中的一个,为了做到这一点,我必须有权访问它们。

你能建议什么样的解决方案?提前谢谢。

2 个答案:

答案 0 :(得分:1)

你在考虑这个问题太难了。你的set属性一次只能修改一个字段,所以if / return结构只是矫枉过正IMO。 if / else if结构就好了。

    void setProperty(String aPropertyName, String aPropertyValue) {
        try {
            if (aPropertyName.equals(AField.FieldName.getName())) {
                fieldName = aPropertyValue;
            }
            else if (aPropertyName.equals(AField.DetailFieldName.getName())) {
                detailFieldName = aPropertyValue;
            }
            else if (aPropertyName.equals(AField.ExternalFieldName.getName())) {
                externalFieldName = aPropertyValue;
            }
            else if (aPropertyName.equals(AField.FormatLength.getName())) {
                formatLength = Integer.parseInt(aPropertyValue);
            }
            /* more if's */
        } catch (Exception e) {
            /* logging exceptions */
        }
    }

您还可以使用枚举确定要分配的字段

    void setProperty(String aPropertyName, String aPropertyValue) {
        try {
            switch(AField.valueOf(aPropertyName)) {
                case FieldName:
                    fieldName = aPropertyValue;
                    break;
                case DetailFieldName:
                    detailFieldName = aPropertyValue;
                    break;
                case ExternalFieldName:
                    externalFieldName = aPropertyValue;
                    break;
                case FormatLength:
                    formatLength = Integer.parseInt(aPropertyValue);
                    break;
                // more cases
                default:
                    throw new AssertionError(AField.valueOf(aPropertyName).name());

            }
        } catch (Exception e) {
            /* logging exceptions */
        }
    }
}

如果你想去地图路线,你必须设置一个接口到你班级的字段。

public interface APropertySetter {
    public void setProperty(String propertyValue);
}

public static class A {

    private String fieldName;
    private String detailFieldName;
    private String externalFieldName;
    private int formatLength;

    private Map<String, APropertySetter> propertySetter;
    /* additional private members */

    public A() {
        propertySetter = new HashMap<>();
        propertySetter.put(AField.FieldName.getName(), (APropertySetter) (String propertyValue) -> {
            fieldName = propertyValue;
        });
        propertySetter.put(AField.DetailFieldName.getName(), (APropertySetter) (String propertyValue) -> {
            detailFieldName = propertyValue;
        });
        propertySetter.put(AField.ExternalFieldName.getName(), (APropertySetter) (String propertyValue) -> {
            externalFieldName = propertyValue;
        });
        propertySetter.put(AField.FormatLength.getName(), (APropertySetter) (String propertyValue) -> {
            formatLength = Integer.parseInt(propertyValue);
        });
    }

    void setProperty(String aPropertyName, String aPropertyValue) {
        try {
            propertySetter.get(aPropertyName).setProperty(aPropertyValue);
        } catch (Exception e) {
            /* logging exceptions */
        }
    }
}

选择对你更有意义的东西,并帮助你成长为开发者......干杯!

答案 1 :(得分:0)

如何使用反射,它会使你的代码变得如此简短,你也不需要使用枚举。:

void setProperty(String aPropertyName, String aPropertyValue) {
        try {
            Field field = this.getClass().getDeclaredField(aPropertyName);
            field.set(this, aPropertyValue);

        } catch (Exception e) {
            /* logging exceptions */
        }
    }