0

I have a BaseImageView (platform is irrelevant here), which has a property Effect, which is a command object.

I would like all my commands to implement IImageEffect, which has an static method ApplyEffect, taking an image and returning the image with the effect.

But the compiler complains that I cannot mark an interface method as static, but this seems like the logical option (as there is no state information needed for the effect).

I ask this because I would need 100 effect objects to apply 1 effect to 100 different images, while I could get away with passing the class and calling the static method, this would do away with the 100 instances.

Is there a way I can have a static interface, or a way that I can pass a class and have it call the static ApplyEffect of that type?

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • No, interface members cannot be static. How would the CLR know which implementation you wanted to call, if you called `IImageEffect.ApplyEffect`? (There's an argument for static interface members with generic type arguments, but that's more subtle.) – Jon Skeet Sep 21 '16 at 10:34
  • @JonSkeet I would pass a class to the `Effect` property, and then call `ApplyEffect` on that class, so it would become `EffectClass.ApplyEffect`. Then it would know what specific class I want to call it on. – vrwim Sep 21 '16 at 10:35
  • 2
    Sorry, this is very hard to understand without a concrete example *in the question*. If you just want a static method that always does the same thing, just put it in a class instead of an interface... – Jon Skeet Sep 21 '16 at 10:36
  • Are there any reason you are not using an abstract class? – Morten Toudahl Sep 21 '16 at 10:40
  • @MortenToudahl I don't want to instantiate a new instance of the same class each time I use that effect, so I thought I'd make it static, but I just realised that this is why the singleton pattern was invented. I'll go ahead and use that. – vrwim Sep 21 '16 at 11:41
  • @vrwim I think that is misuse of the singleton pattern. Singletons are for when more than one instance would break the app. ie, two drivers for one piece of hardware running. Like 2 x video card drivers. Or for managing configurations - accessing/updating the same configuration in two different objects could make something go wrong. Take a look at this, and read the articles linked to too: http://stackoverflow.com/questions/135347/most-common-examples-of-misuse-of-singleton-class – Morten Toudahl Sep 22 '16 at 10:12
  • @MortenToudahl I have a method that only takes an image and only outputs an image, it wouldn't break the app if I had 2 instances. I just deem it useless to have multiple at the same time. What do you suggest I use? Let it be an interface and just instantiate a new instance each time I use it? Or cache an instance and reuse that one? Or simply use a singleton? – vrwim Sep 22 '16 at 10:15
  • @vrwim Create a static class just to handle effects? Adhering with the single responsibility principle – Morten Toudahl Sep 22 '16 at 10:18

1 Answers1

0

I would suggest that you either make an abstract class where you implement the static method, like you wanted to do with the interface.

It could look like this

class Program
{

    static void Main(string[] args)
    {
        BaseImageView.ApplyEffect();
        // Or
        EffectClass.ApplyEffect();
    }

}

public abstract class EffectClass
{
    public static void ApplyEffect()
    {
        // Effect of awesomeness
    }
}

public class BaseImageView : EffectClass
{

}

Or create a static class to apply the effect with, if you don't want to inherrit from an abstract class.

public static class EffectClass
{
    public static void ApplyEffect()
    {
        // Effect of awesomeness
    }
}
Morten Toudahl
  • 452
  • 1
  • 4
  • 13