Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why Use Interfaces
Message
From
10/01/2008 02:32:10
 
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01278205
Message ID:
01280801
Views:
37
>Unfortunately the debate you were so much up to was not about interfaces, but about using them to simulate MI. I am actually very interested in what you know about:
>- What part of MI bad stuff is avoided by simulation? (as opposed to true MI)
>- Why was true MI left out of the C# compiler?
>Thanks.


True MI was left out of the C# compiler because it probably didn't make much sense to Anders to put it in. And, since he invented C#, he can do anything he wants to with the language! <g>

Seriously though, the problem with true MI (the bad stuff) can be shown by an example. First, let's start off with your example:
  EveryThings          	    EatMe(theObjThatEatsMe)
    |- LivingThings         FeedMe(theObjFed), WaterMe(theObjWatered)
    |    |- Person
    |    |- Cow
    |    |- Nightshade
    |- NonLivingThings
         | - Cake
So,from this starting point, let's create a few classes:
public class LivingThings
{
    public virtual void FeedMe(theObjFed)  { }
}
public class CircusAnimals : LivingThings
{
    public virtual void PerformTricks()  { } 
    public virtual void TalkToMe() { }
}
public class FarmAnimals : LivingThings
{
    public virtual void EatMe(theObjThatEatsMe) { }
    public virtual void TalkToMe() { }
}
public class TrickGoats : CircusAnimals, FarmAnimals
{
}
OK, so it doesn't follow your example hardly at all. Oh well. <g>

Now, here's the problem ... in the TrickGoats class, which TalkToMe() method would need to be executed? CircusAnimals are probably talked to (by their trainers) a lot differently than FarmAnimals are talked to (by the farmer). I suppose we could have an overridden TalkToMe() method in the TrickGoats class, but what if you didn't do that? You see how this could easily get complicated.

Now, if we solved this by simulating MI with Interfaces, we'd *have* to implement the method in the TrickGoats class and thus there would be no ambiguity:
public interface ICircus
{
    void PerformTricks();
    void TalkToMe();
}
public interface IFarm
{
    void EatMe(theObjThatEatsMe);
    void TalkToMe();
}
public class TrickGoats : LivingThings, ICircus, IFarm
{
    public void PerformTricks() { } 
    public void EatMe() { }
    public void TalkToMe() { }
}
So, this probably isn't the greatest example, but it's too late for me to be thinking all that clearly and it was the best I could come up with. Still, I think it probably illustrates the answer to your questions.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform