views:

83

answers:

2

Like a lot of C# programmers, I eventually found myself in need of what would essentially be static abstract method functionality. I'm fully aware of why it can't be done, and it makes sense, but I'm in need of a workaround.

I'm working on an XNA game, but the problem doesn't involve too much XNA code, fortunately. I have an abstract base class Note, and various subclasses derived from it. At a certain point, I'll be drawing these to the screen, so I'll have to load textures (each subclass would have different textures and different amounts of textures), have the user input a size parameter, scale the textures to size, and then iterate over my List and Draw() each one. These textures are pretty large, and each instance of a given subclass would be using the same textures at the same scaled size until the user specifies a different size, hence my motivation for taking a static approach.

As I've found out, abstract static methods aren't available. I would have liked to been able to do a single texture resize at a time (whenever the user changed the size parameter) for each subclass, save the scaled texture as a static Texture2D object, and just reference each time I would draw an instance of the Note subtype. That way I could have avoided having a Texture2D object in every instance of a given Note-derived class, as well as having to scale the Texture2D in each instance as well. In addition, I would have liked being able to effectively enforce "all concrete subclasses of Note must implement this static method".

I'm not exactly sure what would be the best approach. I'd prefer a way to do that didn't involve needing to have a static method in each subclass, e.g.:

NoteA.LoadScaledTex(scale);
NoteB.LoadScaledTex(scale);
NoteC.LoadScaledTex(scale);
...
foreach(Note n in notes) {..}

but I'm not sure of any other elegant solution.

+3  A: 

Static actually isn't really appropriate simply because a given piece of data should be shared across all instances of a particular type. Rather (and in this case especially) you should use a singleton, where all instances internally refer to the same, single instance to store shared data. This way you can easily override the implementation at various inheritance levels, and even gives you the flexibility to have some derived types not even use the singleton.

public abstract class Note
{
    public abstract void LoadScaledTex(scale);
}

public class NoteA : Note
{
    private static ScaledTexData instance;

    public override void LoadScaledTex(scale)
    {
        lock(this.GetType())
        {
            if(instance == null)
            {
                instance = new ScaledTexData(scale);
            }
        }
    }
}
Rex M
I still think I'd have to declare one of these per subclass of Note (since each subtype has a different amount of textures).
Mark LeMoine
@Mark why is that a problem?
Rex M
+1  A: 

What you need is to use the AbstractFactory design pattern: http://www.dofactory.com/Patterns/PatternAbstract.aspx

That way you have a NoteAFactory, NoteBFactory, NoteCFactory, that all derive from NoteFactory, and each one can do a different LoadScaledText.

Mohamed Nuur