Static Interfaces

What about allowing static interfaces

Regards


Answer this question

Static Interfaces

  • Cola

    Dear Diego ,

    what do you mean by static interfaces
    if you mean to make all methods static in an interface , then this cannot be declared in C# on both the interface level and the method level .
    so would you please be more clear about what do you mean by static interfaces



    regards ,
    Hussein Ahmad
    http://HusseindotNET.BlogSpot.com

  • Billy Hollis

    Hi Diego,

    what would you use them for

    I use interfaces when I want to abstract communication of callers and callees. So they can have their implementation details burried in their concrete classes but a caller can treat a range of callees the same based on the exposed interface.

    I would not know how to use a static interface where all functions would be static and would have no "connection" to an associated object (missing this pointer). This could only be used on something like a singleton where also the state could be static.

    Maybe you can be more convincing by giving a pattern where static interfaces would be of help

    Bye,
    SvenC

  • Undying

    Hi,
    Just an example. Not sure about static interfaces but maybe they can be useful
    in some cases.

    // Static interface
    public static interface IFigureOptions
    {
       Color BackColor {get;}
       Color ForeColor {get;}
       Pen BorderPen {get;}
    }

    // Circle class
    public class Circle : IFigureOptions
    {
       public static Color BackColor
       {
          return Color.Black;
       }

       ...
    }

    // Ellipse class
    public class Ellipse : IFigureOptions
    {
       public static Color BackColor
       {
          return Color.Red;
       }

       ...
    }

    // Renderer
    public class Renderer
    {
       void DrawCircle(IFigureStyle style)
       {
          ...
       }
    }

    Regards


  • Espen Gatzschmann

    You'll never be able to use the interface that way. You have either non-static interfaces, or static functions like so:


     //non-static interface IFigureOptions figureOptions = new Circle(); figureOptions.BackColor = Color.Red;  //static methodcalls Circle.BackColor = Color.Red;  
     


    How will you be able to call the instance method when it's static

  • JimF

    SvenC,

    Here's an example of where a static interface ( or possibly an interface which includes both static and non-static methods) might be useful.

    Suppose that we want to create a bunch of validator objects in our domain, but we don't want to incur the costs of object creation.  Furthermore, aside from performing validation, there is really no need to keep the validators around - e.g. after calling Validate(), we are done with the objects.

    Consider an interface IBaseValidator, which is defined as such:

    public interface IBaseValidator
    {
        static void Validate(ref object obj);
    }

    It would be nice to be able to define such an interface, since we could then say that every validator class is guaranteed to implement a static Validate() method. 

    The client class might look something like this:

    public class NameValidator : IBaseValidator
    {
        static void Validate( ref object obj )
        {
           // cast obj to proper type, validate, throw an ApplicationException if  not valid.
        }  
    }

    The calling code may or may not ever call Validate thru the interface. If not, the interface still has some merit in that it formalizes the interface (classes must implement static Validate method).  For example, maybe the NameValidator is used as such:

    NameValidator.Validate( ref someObject );

    It may be that the only time the Validate methods are called via the interface is by using reflection.

    Without being able to use the keyword static in an interface definition, I know of no way in C# to require a set of classes to each implement the static Validate() method.

    Regards,

    Zac


  • ArunSun

    I think there is a technical problem where to store the function pointer table of the interface functions. With static interfaces the type (Ellipse, Circle) itself would need such a function pointer table (for BackColor, ForeColor, BorderPen).
    For current interfaces (which are not static) this information is stored with each object instance.

    I read somewhere that interfaces are not implemented as vtables in .Net but I do expect that somewhere somehow there must be some information which override of a function in a class hierarchie is going to be called...


  • Static Interfaces