Note that there are some explanatory texts on larger screens.

plurals
  1. POInterface Split between Two Classes
    primarykey
    data
    text
    <p>I find myself wishing an interface could be composed of methods that are fulfilled in several classes. If I could do that, I could make an interface for a role that has one function from this class and one function from that class.</p> <p>Does any language support this? Is it possible in the typical Java/C#/VB.NET style OO?</p> <p>For example: I have a function that takes an value object, and saves all the parts into the correct tables. I just made an interface called <code>StatsPersistence</code> that I inject to a function so I can unit test its behavior, but it is missing a method. I want to be able add a method to that, but that method is on a different class from the first two, so this is obviously not possible. So, I have 2 methods in one class and 1 are in the other, but they both are needed for the "role" of "StatsPersistence". Also, those methods are all three needed for other roles too, not just this one.</p> <p>What I have:</p> <pre><code>interface StatsPersistence public saveSession(session) // fulfilled in much larger StatsActiveRecord public saveAppUse(appUse) // fulfilled in much larger StatsActiveRecord </code></pre> <p>What I want:</p> <pre><code>interface StatsPersistence public saveSession(session) // fulfilled in much larger StatsActiveRecord public saveAppUse(appUse) // fulfilled in much larger StatsActiveRecord public updateUnit(name) // fulfilled in much larger UnitActiveRecord </code></pre> <p>I <em>could</em> just make a new class that wraps all three methods, and have that class just make calls to the two implementers, but that seems just like a lot of OOverkill.</p> <p>My thoughts are leaning towards:</p> <ul> <li>My interface role is too broad</li> <li>Make a class that wraps these three, and have it implement my desired interface</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload