Re: MVP's: static methods in interfaces.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Picho (SPAM_picho_at_telhai.ac.il)
Date: 12/21/04


Date: Tue, 21 Dec 2004 15:17:17 +0200

Guys thanx for the replies, but I'm afraid you are not following me....

Component-Oriented-Design.

I have no intention of using interfaces for declaring constructors for my
own pleasure.
however, examining interface-based programming there is a certain conflict
between object oriented design and component oriented design if I cannot
control the way an object is created and only use the interface for managing
its state and behavior.

"Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.com> wrote
in message news:uN6WmG15EHA.3756@TK2MSFTNGP14.phx.gbl...
> Hi,
>
>> I would still like to state that I realy want an interface to declare
>> static methods, even not for construction...
>
> To the best of my knowledge, you cannot. Interfaces assume polymorphic
> behavior in .NET, and static methods cannot be polymorphic.
>
> Some general comments:
>
> Interfaces are not used for construction as construction in 99% cases does
> not need to be polymorphic. So if you need polymorphic construction,
> declare an interface for a class factory and implement several factories
> each encapsulating a different way of constructing instances.
>
> As for static methods such as Graphics.FromHandle(), they have nothing to
> do with interfaces, from my point of view. They are just a convenient way
> of constructing objects, but they at the same time guarantee that the
> construction behavior is not overridden if you implement a derived class.
>
> --
> Sincerely,
> Dmitriy Lapshin [C# / .NET MVP]
> Bring the power of unit testing to the VS .NET IDE today!
> http://www.x-unity.net/teststudio.aspx
>
> "Picho" <SPAM_picho@telhai.ac.il> wrote in message
> news:uvQ6gC05EHA.2876@TK2MSFTNGP12.phx.gbl...
>> Thanx for the reply Helge.
>>
>> To sum things up, what you say is that there is no way that an interface
>> can enforce or dictate the way a class instanciate itself.
>> I am not arguing with what you said and I even accept the fact that
>> interfaces has to do with behavior.
>>
>> but then again, we can talk all day long philosophicly wether
>> construction is not a behavior...
>>
>> I would still like to state that I realy want an interface to declare
>> static methods, even not for construction...
>>
>>
>> Picho
>>
>> "Helge Jensen" <helge.jensen@slog.dk> wrote in message
>> news:41C7E778.8090908@slog.dk...
>>> Picho wrote:
>>>
>>>> I want to give my classes the ability to staticly create instances,
>>>> simply because I think that calling a 'MyClass.FromSomething(Something
>>>> something)' is more intuative than a constructor 'public
>>>> MyClass(Something something)'
>>>
>>> For classes, you can just do that:
>>>
>>> public class MyClass {
>>> protected MyClass(...) {...}
>>> public FromSomething(...) { return new MyClass(...); }
>>> }
>>>
>>> So, actually your problem doesn't arise from trying to redefine
>>> contruction. It arises from trying to do (what is often called) virtual
>>> construction.
>>>
>>>> this is just me...
>>>
>>> Beware, that other people might not agree. Sometimes it's just easier to
>>> accept the common idioms, with the problems they offer, than to teach
>>> all you work together with a new one (and sometimes, the new one has
>>> it's own problems :)
>>>
>>>> anyway, as far as I know interfaces cannot define static members. this
>>>> pushed me into something I even like less - a class factory (even
>>>> though it is in some scenarios a good practice).
>>>
>>> All polymorphic behaviour is on instances in C#, and you are asking for
>>> virtual construction (which uses polymorphism). So, you need an
>>> instance, commonly called a factory.
>>>
>>>> 1. Is there a more... 'elegant' way for me to achieve this?
>>>
>>> Not really, especially don't fall into the pit of "prototyped
>>> contruction" if you don't actually *need* cloning.
>>>
>>> You can do virtual construction using delegation (delegating to
>>> FromSomething) but whether that is more elegant is debatable.
>>>
>>>> 2. is this somehing we want from C# (static member decleration in
>>>> interfaces)?
>>>
>>> Perhaps, but I don't think so in the sense that you ask for.
>>>
>>> Since C# doesn't allow free functions, it would be nice to be able to
>>> stick static functions implemented purely in terms of the interface in
>>> the interface declaration. This wouldn't solve the problem of missing
>>> free-functions for class-symmetric operations though.
>>>
>>>> 3. am I just being picky (should I just use constructors and forget my
>>>> intuition?)
>>>
>>> You couldn't get by just using constructors for virtual construction.
>>>
>>> With regard to the "FromSomething(...)" idiom, you might want to ponder
>>> whether it buys you enough to break out of the "new(...)" idiom, which
>>> is widely used.
>>>
>>> One thing which FromSomething *does* buy you is that you can do
>>> delegates to the construction, something you cannot do with new.
>>>
>>> Beware, also that you would want to write a contructor in the class even
>>> if you do FromSomthing, you just would just need to decide whether to
>>> make it public or protected.
>>>
>>> Personally I would consider having a FromSomething only as an added
>>> option, so I would not make the constructor protected. And I would
>>> probably only do FromSomething if there were a *lot* of SomeThings, or I
>>> needed to do construction by delegate.
>>>
>>>> 4. am I totaly wrong?
>>>
>>> Well,.... you're mixing up two separate issues,...
>>>
>>>> hang on... can we even define constructors in interfaces today?...
>>>> god I even confused myself...
>>>
>>> No, you can not.
>>>
>>> You cannot instantiate an interface, since an interface is exactly a
>>> *declaration* of what an instance *can*. Not a recepie for *how*, which
>>> is done in a class (which can incidentally also contain state).
>>>
>>> --
>>> Helge
>>
>>
>



Relevant Pages

  • Re: MVPs: static methods in interfaces.
    ... Interfaces are not used for construction as construction in 99% cases does ... >> virtual construction (which uses polymorphism). ... >> FromSomething) but whether that is more elegant is debatable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MVPs: static methods in interfaces.
    ... > I have no intention of using interfaces for declaring constructors for my ... >>> static methods, even not for construction... ... and static methods cannot be polymorphic. ... >> declare an interface for a class factory and implement several factories ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MVPs: static methods in interfaces.
    ... your derived classes must instantiate. ... > I have no intention of using interfaces for declaring constructors for my ... >> Interfaces are not used for construction as construction in 99% cases ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: protocols, inheritance and polymorphism
    ... have do declare interfaces everywhere to make them work this way). ... Going back to my earlier Python example: ... Note that because Java is statically typed, you have to declare all the ... I could have written getName ...
    (comp.lang.python)
  • Re: CLOS Properties Question
    ... In interfaces, you can declare methods, but you ... This mess was the result of the fact that interfaces ... > is even an OOPSLA or ECOOP paper published before the advent of Java ... > Back to the question what this all has to do with multiple inheritance: ...
    (comp.lang.lisp)