Andy wrote:
Ok, interfaces are contract - yeah. But why limit them to instance methods?
Doesn't it make sense that you might sometime want to create a group of
classes that implement specific behavior (contract) that is static?
*Is there a pattern that will let me work around this?*
"Andy" <ea***@expknow.com <mailto:ea***@expknow.com>> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl... > Why can't I create an interface and make its implementation
static/shared? >
> Is there some pattern that lets me work around this?
>
> Any explanation of why this is would be appreciated.
>
>
>
> This is the message I get for the following code:
>
> 'staticInterface.Class1' does not implement interface member
> 'staticInterface.foo.doSomething(int)'.
> 'staticInterface.Class1.doSomething(int)' is either static, not
public, or > has the wrong return type.
>
>
> interface foo
>
> {
>
> bool doSomething(int y);
>
> }
>
>
> public class Class1 : foo
>
> {
>
> public bool x() { }
>
> static public bool doSomething(int y)
>
> {
>
> // TODO: Add Class1.doSomething implementation
>
> return false;
>
> }
>
>
> }
>
>
Here is what I think (1 1/2 c)..
*static* methods are a contract from the "Type" but not from an
"instance". i.e. The mere existence of a particular Type would guarantee
the existence of that *static*. Now, Interface are instance specific.
This has to be the case since interfaces are meant to discover and use
abilities of similar instances of types.
With that in mind, it would not really make sense to declare a interface
method as static since that would mean that the contract being agreed
upon is actually from the *type* of the derived class and not by the
*instance*.
And of course, the real thing to note is that if you consider COM
interfaces (or any implementation of interfaces) they are just runtime
vtbl pointers to concrete instance method, that would just not work with
a static.
Anyway, if you really want a static method to be part of a contract, you
can do that using a simple facade pattern to wrap the underlying
interfaces and use Template method + Factory to generate and use the
real interface implementations.
--
Girish Bharadwaj