Without giving it much though, I would like to see an interface<T> extension that effectively creates an interface of a class on the fly. Why would you do that? Just extract the interface of a class or inherit from it? Well, works fine, unless you add something to the original class.
Consider the following code:
public class Duck
{
public int Age { get; set;}
public void Quack()
{
...
}
}
public class MockDuck : interface<Duck>
{
public int Age { get; set;}
public void Quack()
{
...
}
}
The Duck class is the original class. To create a mock duck I would have to inherit from it or extract an interface. The problem with inheritance is that if I add a method or a property to the base Duck class, the implementation would leak into my MockDuck. As intended in inheritance, but I'm not after inheritance. A simular problem occurs when you extract an interface. The interface would not be in sync with the original implementation. Also my duck source/binary might be outside of my control so I couldn't add the interface to it.
- Anywhere any code needs a Duck, passing a class implementing interface<Duck> would be OK.
- All methods and properties of the source class must be implemented
So what are the problems with this approach? Probably a lot of things... This is just an idea! :)
No comments:
Post a Comment