In this post I suggested a solution that uses interface and anonymous class. However, there is one thing to be implemented: the
However I found it is hard to implement
for anonymous class that implements an interface. In that example the interface is
, and a factory method
will return an anonymous implementation for it. Suppose I added an
implementation. The user may implement their own
classes with a different
code, therefore the call
will enter their code, and
will enter my code. Because I have no control of their code, it is hard to make sure
to be symmetric, which is required for a good implementation.
I believe this problem is common for other cases, so I would like to know how this issue being address generally?
In typical class, the implementation of
will check the parameter type to make sure it is the same as its own. This guarantee only the implementing code will be called to compare the objects. However, the anonymous class do not have a name and cannot check the type with
. What I can do is make sure it is an instance of the implementing interface/class. Which is not enough to prevent the above scenario.