I am looking for the cleanest way to validate complex hierarchies of objects.
I have a master class names Container. Constainer consists of some string fields and ContainerProp. ContainerProp is abstract class with many subclasses like PropA, PropB etc. PropA has a Constraint which is albo an abstract class with many subclasses like ConstraintA, ConstraintB... Additionally, ConstraintA has a Rating object. The structure may go even deeper, but lets stay with that.
Bit complex, I know, but I can't modify this structure.
When it comes to code, it looks like this:
public class Container {
//some fields, constructors and methods
private ContainerProp containerProp;
}
public abstract class ContainerProp {
//some fields, constructors and abstract methods
}
public class PropA extends ContainerProp {
//some fields, constructors and methods
private Constraint constraint;
}
public abstract class Constraint {
//some fields, constructors and abstract methods
}
public class ConstraintA extends Constraint {
//some fields, constructors and methods
private Rating rating;
}
public class Rating {
private String x;
private String y;
}
I am thinking of some clean and elegant way to validate the whole structure just by using container.validate() or something like this. The first idea that I had was using visitor pattern. It would work perfectly for single superclass-subclass level, but I am not really sure if using visitor for the whole structure is the right approach, as it seems a little bit too wide. Do you know some clean way to solve this problem? Thanks in advance
Aucun commentaire:
Enregistrer un commentaire