Saturday, December 17, 2011

Passing messages in method names? Why not!

Have been working with Mockito awesome mocking framework (i LOVE it). It also does use Hamcrest matcher as an option for matching its arguments.

So, say you don't want users of your library to implement interface directly but rather'd like force them to use the Base implementation of this interface and extend it instead? Putting away the reasons which led to this situation, here's how it may be accomplished:

package org.hamcrest;
public interface Matcher extends org.hamcrest.SelfDescribing {
boolean matches(java.lang.Object o);
void _dont_implement_Matcher___instead_extend_BaseMatcher_();

Every time you'll try to create a direct implementation of this interface, you'll get this weird method which would tell you what to do.

Good or bad? To me rather bad; something is wrong in design if you offer interface but don't allow users to use it. On the other side it may be good, as BaseMatcher gives some bells and whistles and you don't always know about good base subclass. Also, this may have been caused by some external reasons.

No comments: