jeudi 31 mars 2016

Should we design interfaces to include object manipulators

I have a class named ValidationResult which implements IValidationReport. These are generic entities which are meant to encapsulates output of the various validators we have. All validators do return IValidationReport.

As a standard design practice we include only getters in IValidationReport, and no setters or methods that can manipulate the contents of the returned object. Thus it has getResult(String filePath), getMessages(String filePath) etc. which returns validation result, validation messages and other information of validation of the specified file.

ValidationReport have different overloads of the form addResult(filePath, message, ...) which adds the corresponding information against specified filePath. However we also have one method in ValidationReport called addResult(IValidationReport). This method essentially work as merging two ValidationReports. So if two validation reports (the on which this method is called and the one which is passed as an argument) have validation information of same file, then it will be merged.

Now in one of our modules, it happens that we are calling two validators. Both have validate() methods which returns IValidationReport. However I cannot merge the two as IValidationReport contains only getters, but addResult(IValidationReport). That is following:

IValidationReport valrep1 = validator1.validate();
IValidationReport valrep2 = validator2.validate();
valrep1.addResult(valrep2); //cannot do this as valrep1 is IValidationReport and it does not contain addResult(IValidationReport)

  • So what should I do? Should I be adding addResult(IValidationReport) to IValidationReport. But then it will break design convention of not adding object manipulators to interfaces.
  • Where did I go bad with the design? Or am I missing something?

Aucun commentaire:

Enregistrer un commentaire