'Effective Java: Understanding Workaround When Implementing Comparable
In the 2nd edition of Effective Java, Item 12 discusses implementing the Comparable interface. There is a statement that I can't quite wrap my head around, and was curious about a concrete example exemplifying the statement.
If you want to add a value component to a class that implements
Comparable, don't extend it; write an unrelated class containing an instance of the first class. Then provide a "view" method that returns this instance. This frees you to implement whatevercompareTomethod you like on the second class, while allowing its client to view an instance of the second class as an instance of the first class when needed
So, is the "second class" just a wrapper class containing the "value component" + an instance of the first class? What is the "view" method, a getter? If so, how could the client of this wrapper class "view an instance...as an instance of the first class when needed"?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
