How can I give “generic” interface functions a concrete type on override?

How can I give “generic” interface functions a concrete type on override?

I believe this shouldn’t be a hard question, but I somehow can’t come up with the clean solution I’m hoping for.

Basically, I’d like to have several interfaces with one function each, and several classes implementing any number of those interfaces. The thing is, that each implementing class is dealing with one specific type, but a different type each.

Here is a solution that would be working, but that I’d like to improve on:


interface add <E> { fun add(entity: E) }

interface remove <E> { fun remove(entity: E) }


class TypeA_Handler: add&lt;TypeA&gt; { //implementation of add()

}


class TypeB_Handler: add&lt;TypeB&gt;, remove&lt;TypeB&gt; { //implementation of add() &amp; remove

}

What bothers me is that I will have quite a few such interfaces, and each handler will always use the same type. Being forced to explicitly state the same type on the implements statement seems cumbersume (and slightly error prone).

The interfaces’ purpose here is to force the implementation of a certain set of functions (different combination for each handler) and those functions to have some uniformity across all implementing handlers (e.g. so that the function will always be “remove”, and cannot be called “delete” instead).

In case you haven’t guessed it already, this approach is inspired by the DAO Design Pattern. What I dislike about that pattern is that every DAO_Impl has it’s own interface with all the operations you want to allow for said DAO. Considering (at least in my case) DAOs have no need for non-private functions I want to mask via a interface and there’s no reusability for a specific interface, that’s seems to me like a whole bunch of nonsensical overhead.

So what I’m instead trying to to is define a interface for each operation, and have my DAOs implement all the interfaces I want to allow on it. Thus I need my interfaces to be generalized and my implementations to specify these generalizations.

I’m really open to any kind of suggestion here.

I’ve been looking into generics (see example), and into using

Any

instead of generics in my interfaces (which I couldn’t override to a certain type in my handlers).
While I’m also open to arguments in favor of the classic DAO Design pattern, this idea of overriding generalized types somehow peaked my interest independently of the thing I’m working on right now… ^^

submitted by /u/batt84
[link] [comments]