I have a POJO class(MyPojo) with 50+ fields that I need to create.
Each field is created using multiple other objects and has some complex business logic/aggregation logic. So, to process these fields I created a interface
FieldProcessor
with a method
public interface FieldProcessor{
// set field in myPojo after processing data from obj1, obj2, obj3, obj4
void processField(Class1 obj1, Class2 obj2, Class3 obj3, Class4 obj4, MyPojo myPojo)
}
Note: Not always all for objects(obj1, obj2 etc..) are used to create a field.
Implementation of above interface will look something like below:
@Service("productOfferProcessor")
public class ProductOfferFieldProcessor implements FieldProcessor{
public void processField(Class1 obj1, Class2 obj2, Class3 obj3, Class4 obj4, MyPojo myPojo){
//get data from multiple objs and do some processing here
String offerValue = "some processed offer";
Double offerValue2 = "some processed offer value";
myPojo.setOfferField(offerValue);
myPojo.setOfferAmountField(offerValue2);
}
}
Then I am maintaining these FieldProcessors bean name in enum. This enum keeps map of field to field processor bean name:
public enum FieldProcessors {
offerName("productOfferProcessor"),
offerValue("productOfferProcessor"),
field3("someprocessor3"),
field4("someprocessor4")
.....
....
field50("someprocessor4");
private final String value;
FieldProcessors(String fieldProcessors) {
this.value = fieldProcessors;
}
public String getValue() {
return value;
}
}
Finally, I am creating some sort of builder class:
public class MyPojoBuilder {
EnumSet<FieldProcessors> fieldProcessors = EnumSet.allOf(FieldProcessors.class);
public MyPojo buildMyPojo(Class1 obj1, Class2 obj2, Class3 obj3, Class4 obj4) {
MyPojo myPojo = new MyPojo();
for (FieldProcessors fieldProcessor : fieldProcessors) {
processorInstance(fieldProcessor.getValue()).process(obj1, obj2, obj3, obj4, myPojo);
}
return myPojo;
}
public FieldProcessor processorInstance(String beanName) {
return (FieldProcessor) ApplicationContextProvider.getApplicationContext().getBean(beanName);
}
}
I would like to know whether this is good pattern or not? I am not using builder pattern here as there complex logic involved and I did not wanted to write too many lines of code to set those fields? is there any better way to solve this problem?
Any design suggestions on this problem will help :)
Aucun commentaire:
Enregistrer un commentaire