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.