Can anyone explain what Joshua called the “view” method?
I will continue the simple Jon Skeet example from the answer that you linked in your post :
// In Jon code, Person derives from NamedThing class NamedThing { String name; } class Person extends NamedThing { Date dateOfBirth; }
“Providing a view” rather than “extension” would mean creating a class hierarchy as follows:
class NamedThing { String name; } class Person { private NamedThing namedThing;
This frees you to implement the new compareTo inside Person , because there is no need to remain compatible with the superclass. If you need to view your person as NamedThing , call asNamedThing() to get an idea of the person reduced to an item with a name.
Note that since this is not an is-a relationship, asNamedThing somewhat misleading: you get a named thing inside a person, not a person who is named. This limits the applicability of compareTo : for example, you cannot sort people among other NamedThing s.
source share