patternjavaMinor
Copy object properties without using many if statements
Viewed 0 times
withoutpropertiesstatementsusingmanyobjectcopy
Problem
I have two class:
InputForm.java
CopyForm.java
I want to copy values from an InputForm to another if its corresponding property in CopyForm is true.
This is what I do:
The problem is I have many many properties. Writing many if statements seems ugly and bad programming practice.
How to solve it? (I know reflection is not a good choice so I don't think about it)
InputForm.java
public class InputForm {
private String brandCode;
private String caution;
public String getBrandCode() {
return brandCode;
}
public void setBrandCode(String brandCode) {
this.brandCode = brandCode;
}
public String getCaution() {
return caution;
}
public void setCaution(String caution) {
this.caution = caution;
}
}CopyForm.java
public class CopyForm {
private boolean brandCodeChecked;
private boolean cautionChecked;
public boolean isBrandCodeChecked() {
return brandCodeChecked;
}
public void setBrandCodeChecked(boolean brandCodeChecked) {
this.brandCodeChecked = brandCodeChecked;
}
public boolean isCautionChecked() {
return cautionChecked;
}
public void setCautionChecked(boolean cautionChecked) {
this.cautionChecked = cautionChecked;
}
}I want to copy values from an InputForm to another if its corresponding property in CopyForm is true.
This is what I do:
if(copyForm.isBrandCodeChecked()) {
inputForm.setBrandCode(otherInputForm.getBrandCode());
}
if(copyForm.isCautionChecked()) {
inputForm.setCaution(otherInputForm.getCaution());
}The problem is I have many many properties. Writing many if statements seems ugly and bad programming practice.
How to solve it? (I know reflection is not a good choice so I don't think about it)
Solution
I think Apache commons BeanUtils might help you there. It uses reflection. Take this into consideration if performance is an issue...
We need 2 Steps:
1st create a method that copies a property if a condition is met
2nd create a copy method that passes all fields to the first method
Here we go...
if you're still not pleased with that you could use reflection to invoke
We need 2 Steps:
1st create a method that copies a property if a condition is met
2nd create a copy method that passes all fields to the first method
Here we go...
public class InputForm {
[...]
private void copyProperty(InputForm srcForm, String field, boolean condition) {
if(condition) {
PropertyUtils.setProperty(this, field, PropertyUtils.getProperty(srcForm, field));
}
}
public void copyFromOtherForm(InputForm srcForm, CopyForm conditions) {
copyProperty(srcForm, "brandCode", conditions.isBrandCodeChecked());
copyProperty(srcForm, "caution", conditions.isCautionChecked());
[...]
}
}if you're still not pleased with that you could use reflection to invoke
"is"+field+"Checked" on your CopyForm instance and change the calls to:copyProperty(srcForm, conditions, "field");Code Snippets
public class InputForm {
[...]
private void copyProperty(InputForm srcForm, String field, boolean condition) {
if(condition) {
PropertyUtils.setProperty(this, field, PropertyUtils.getProperty(srcForm, field));
}
}
public void copyFromOtherForm(InputForm srcForm, CopyForm conditions) {
copyProperty(srcForm, "brandCode", conditions.isBrandCodeChecked());
copyProperty(srcForm, "caution", conditions.isCautionChecked());
[...]
}
}copyProperty(srcForm, conditions, "field");Context
StackExchange Code Review Q#27254, answer score: 3
Revisions (0)
No revisions yet.