I have data object's let say PersonDto (fields name, surname) and OrganizationDto (field name, type).

Then I have some common screen, showing such data, but screen title is something related to type of - Person/Organization.

The easiest way how to implement that functionality is to have field/constant in that class. Another approach would be to use some instanceof checks (related to my previous question), the most complex (from my point of view) is to use Visitor pattern, so at some point, there is one of methods called:

Visitor {
    accept(PersonDto dto) {
        return "Person";
    accept(OrganizationDto dto) {
        return "Organization";

Just now I realized, some magic with class name can be done (for example if DTO name is not one word I'd need to add space or something), but I do not like that approach at all.

Approach with additional field seems the most straighforward to me especially if I have common interface for DTOs, but it is breaking SRP in a sense, that class not only holds data, but knows something about screen/UI. I just prefer KISS more.

  • 3
  • Why is there a common screen in the first place? They look like different enough things to have their own UI, don't they? – guillaume31 Jan 9 '19 at 13:10
  • ...simply because we can, sad I know. For example by serializing data to JSON all you have (as a result) is field name and it's value and you can have a generic screen for that. No need to write specific screen, no "additional" effort, quicker delivery, no space for errors... – Betlista Jan 9 '19 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.