i need help with designing my code. assume i got an interface that represents a metadata of fields
public interface FieldDefinition {
boolean validate(Object value)
FieldTypeDefinition getFieldTypeDefinition()
}
and classes that implement this interface(i'm not writing here all the context such as properties i.e),
public class SomeFieldDefinition implements FieldDefinition {
@Override
public boolean validate(Object value) {
// some general validation logic
}
@Override
public FieldTypeDefinition getFieldTypeDefinition() {
// return NumericFieldTypeDefinition or StringFieldTypeDefinition
}
}
public class SomeFieldDefinition2 implements FieldDefinition {
@Override
public boolean validate(Object value) {
// some general validation logic
}
@Override
public FieldTypeDefinition getFieldTypeDefinition() {
// return NumericFieldTypeDefinition or StringFieldTypeDefinition
}
}
now, FieldTypeDefinition is also an interface
public interface FieldTypeDefinition {
public String getTypeName();
boolean validate(FieldDefinition fieldDefinition,Object value);
}
and i have concrete classes that implements it
public class StringFieldTypeDefinition implements FieldTypeDefinition {
@Override
public String getTypeName() {
return "String";
}
@Override
public boolean validate(FieldDefinition fieldDefinition, Object value) {
// some specific field of type string validations by fieldDefinition type
}
}
public class NumericFieldTypeDefinition implements FieldTypeDefinition {
@Override
public String getTypeName() {
return "Number";
}
@Override
public boolean validate(FieldDefinition fieldDefinition, Object value) {
// some specific field of type number validations by fieldDefinition type
}
}
now, when i want to do some validation on fields, i want to have general validations, and specific validation by the field type. so assume i'm invoking the method validate on SomeFieldDefinition2 with a String parameter the method will look like this:
public boolean validate(Object value) {
// some general validation logic
getFieldTypeDefinition().validate(this, value); // getFieldTypeDefinition() returns StringFieldTypeDefinition
}
and this causes me a problem because in validate method of StringFieldTypeDefinition class i need to check the instance of fieldDefinition parameter, and by its value, make the suitable validation logic, because in each FieldDefinition type class i have a different logic and metadata.
is there a better way of designing my code? i hope my question is understandable.
Aucun commentaire:
Enregistrer un commentaire