3 ms·
> this interface hierarchy would be annoying to use in practice No doubt, but it happens, and sometimes because the refactoring you might prefer to do can't be
by cryptonector 7y ago
> this interface hierarchy would be annoying to use in practice
No doubt, but it happens, and sometimes because the refactoring you might prefer to do can't be done or can't be done right away.
For example, in Java you might have a subclass of Principal where you store additional metadata and have additional methods, then you might have code like:
public static Something princOk(Principal p, Stuff stuff)
throws MyPrincipalException
{
if (p instanceof MyPrincipal)
return princOk((MyPrincipal)p, stuff);
return princOk(MyPrincipal(p), stuff);
}
Why would you do this? Well... because many JDK and third-party interfaces take Principal object arguments that you could reuse... if only you could decorate those objects with the additional metadata that you wanted, and... with subclassing you get to.
In Java you might say "use Subject", but it's not that easy, and anyways, JAAS is a terrible terrible thing that should have been ripped out when applets were ripped out.
- CoolGuySteve 7y agoIn C++, you can use templates to pattern match/duck type interfaces at compile time. So "template<typename PrincType> void princOk(PrincType& p, Stuff& stuff) {p.doThing(stuff); }" would map properly as long as doThing was implemented in all the different classes. You don't actually need an interface or base class for it to work. The rest of the dynamic mapping is faster by calling switch() on an enum class member and than static_cast rather than relying on dynamic_cast.