I have a class A defined as follows in Java:
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 */
}
}
}
The further if's are analogical, i.e. they all modify one specific private member variable of the class A.
The AField is defined as follows:
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;
}
}
I would like to replace the multiple if..return.. statements with the more appropriate, and easier to maintain solution. I read about using the map with each AField value as a key and a proper command (Command design pattern) as a value. However, my problem here is that in each of the if statements I modify one of the A's class private members and in order to do that I must have access to them.
What kind of a solution can you suggest? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire