mardi 3 mai 2016

Object validation using builder pattern

Let's say I have an object Foo. I also have an FooBuilder which is to be used to construct the Foo objects. When and where should I validate the Foo object's data?

Foo foo = new FooBuilder()
    .withX("specific data for X")
    .withY("specific data for Y")
    .build();

Let's add to the equation that the validation may contain lookups in for example a database. Should the builder perform a validation inside the build method? Or should there be a specific validate method in the foo object? Or maybe the validation is best off completely abstracted from both the Foo object and the FooBuilder?

Aucun commentaire:

Enregistrer un commentaire