0

I have a list of numbers, and each number that is the same should act exactly the same. So I have static classes for each number so that if I change the class, so do all of the numbers it references to.

The way the numbers are accessed is via a wrapper function, so that I'm not referencing the array directly, e.g.:

Map.GetBlock(x,y).AccessToStaticClassMembers;

So, how would I go about this?

4
  • And the name of this design pattern is? Commented Dec 21, 2010 at 10:25
  • No idea. This needs to be as least-memory intensive as possible. Commented Dec 21, 2010 at 10:39
  • Your question is really, really, not clear. Please explain in more details what you are trying to do, and post the code you have that doesn't work. Commented Dec 21, 2010 at 11:22
  • Okay then. I have an array of numbers. These numbers should refer to static classes. I want to access these classes by calling Map.GetBlock(X, Y).CLASSMEMBERS, to avoid having to instantiate the 32768 numbers that will be in the array. Static classes make this easier to sort out and configure. How do I return a static class from a function? Commented Dec 21, 2010 at 11:28

2 Answers 2

1

I'm not really sure what you want. But it sounds like you're trying to ensure that there is only one instance in memory for each number. If that's the case, what's wrong with something like this:

static public class ObjectMapping
{
    static Dictionary<int, object> dictionary = new Dictionary<int, object>();

    static public object GetObjectForNumber(int x)
    {
        object o;
        if (!dictionary.ContainsKey(x))
        {
            o = CreateObjectForNumberTheFirstTime(x);
            dictionary.Add(x, o);
            return o;
        }
        return dictionary[x];
    }
}

Of course, I left out things such as thread safety and creation of the objects in the first access, for you to do on your own.

Sign up to request clarification or add additional context in comments.

1 Comment

This looks like what I need. Thanks. So, the returned result from this could be accessed via GetObjectForNumber(X).MEMBERS? It seems so. Also, this'd be a great way of handling something else I have in mind too!
1

Why make it static? This looks more like overrides of some abstract method or implementations of some interface method, if I got you correctly.

2 Comments

Static to reduce memory usage. It's critical. An array of bytes doesn't take much space, and neither do a group of static classes. If they were all just classes, I'd have a huge amount of memory usage for what it is.
Ran approach will not help with statics. Can you afford for reflection?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.