Note that there are some explanatory texts on larger screens.

plurals
  1. POInterface and class inheritance. Is this an avoidable pattern?
    text
    copied!<p>I'd appreciate any advice on which way to go on in the following scenario. Let's see if I can explain it clearly (english is not my native language so things might get confusing, sorry).</p> <p>Suppose I have the following interfaces:</p> <pre><code>internal interface IBlah { int Frob(); } internal interface IBlahOnSteroids: IBlah { double Duh(); } </code></pre> <p>Now we have a Foo class with a 'has a' relationship with an IBlah object:</p> <pre><code>public class Foo { IBlah blah; internal Foo(IBlah blah) { this.blah = blah; } public int Frob() { .... return this.blah.Frob(); } } </code></pre> <p>Now we also need a FooOnSteroids class that has a 'has a' relationship with a IBlahOnSteroids object. The question is, knowing that part of IBlahOnSteroids is already implemented in Foo, what happens if we create FooOnSteroids inheriting from Foo?</p> <p>We would get something like this:</p> <pre><code>public class FooOnSteroids: Foo { IBlahOnSteroids blah; internal FooOnSteroids(IBlahOnSteroids blah) :base(blah) { this.blah = blah; } public double Duh() { return this.blah.Duh(); } } </code></pre> <p>Is this a recommended pattern? We are passing down the inheritance chain the same 'blah' object and at each "level" we are storing it in a private field with a 'useful' type. There is no way, that I can see, that I could store in BlahBase a protected property that exposed one common IBlah reference to all descending classes as it would have to be of type IBlah wich would be of no use to BlahOnSteroids. Is this scenario even recommended? Or should we just implement Foo and FooOnSteroids as independent classes with no inheritance (this would create code duplication)? Maybe its absolutely fine to do this, but it somehow feels like a hack. Is it?</p> <p>The option of using generics, which would solve the problem in no time, is not possible as, yes I know it sucks, this library must target .Net 1.x platforms.</p> <p>The option of just implementing BlahOnSteroids is also a possibility but it would mean that depending on the caller, we would have to throw an exception if any of IBlahOnSteroids members was called. I dont like that.</p> <p>Thanks a lot for any advice!</p>
 

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