Question

I have a Quad class (for my sprites) and within this class are various variables for altering the state of said Quad. So I can simply make a sprite like so:

Quad hero = new Quad();

I have a Resource class where all of my sprites are held/created.

I have a Main class which is not a neighbouring class of Resource, therefore to keep from breaking the rule of Demeter, I have created various getters/setters in the Resource class (which is a neighbour of Main).

However, as I have so many variables that need to be accessed, is there a better way of creating a getter/setter for each variable?

I'd like to do something like being able write a getter which simply returns a Quad so I can then access it's variables within the Main class. So in Resource, I would have something like so:

public Quad getQuad(Quad quad){

    return quad;

}

And then in my Main class I want to so something like this: ('res' is an object of my Resouce class)

res.getQuad(hero).variable;

I realise the above doesn't work - it's just an example of the kind of thing I would like to achieve.

Or maybe there is code I could put in the getter itself that would allow me to access any of it's variables rather than a specific one.

This is just so I don't have to break Demeter by doing this which I have been assured on many occasions is not good practice:

res.hero.variable;

Would be grateful for any help.

Was it helpful?

Solution

You may be interested in following the Singleton Design pattern here if things aren't static.

However, if resources is used frequently in your project - but contains static members (or members that you should go back and make static) - then it's rather silly to make getters and setters for everything. Just make them public objects and access them like a constant.

Now, if you want to do what you described above (which is a perfectly good way to do it) - it's certainly possible. You'll need to keep a dictionary in your resource class that uses some kind of key to access the members. Likely you'll want to set up an Enum with each member's name assigned to an index. And use the enum as the key for your dictionary. That way you'd access it like this:

res.getQuad(Resource.MyEnum.Resource1).variable;

Resource would then need something like:

public Dictionary<MyEnum, Quad> my_quads = new Dictionary<MyEnum, Quad>();

public static enum MyEnum
{
    Resource1 = 0
    Resource2 = 1
    Resource3 = 2
}

public Quad getQuad(MyEnum resource_index)
{
    return my_quads[resource_index];
}

OTHER TIPS

Your example is correct, if "variable" is an accessible field of Quad.

res.getQuad(hero).variable;

or you can assign Quad to a local variable and access the variables later

Quad q = res.getQuad(hero);

/* do something with */ q.variable1;

You can have a HashMap<String, Quad> in Resources, so you can get a quad using some key you used when creating it. It's easier than writing getters for all your quads, if you have many of them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top