Using the Correct Data Structure

Started by
5 comments, last by epicpunnum 11 years, 4 months ago
Hello! I have been designing a level design system in Java, that allows the user to associate tiles on a tileset with a class. To do this I'm designing a dictionary file, that creates this association. However, most classes have more than one constructor. This is where I come across problems.
Originally I used a java Hashtable. The key is a String for the parameter name, and the type of the elements are Objects (for use with wrapper classes and Strings largely). However, to account for the multiple constructors, I also need to record the priority of each parameter.

Let's say that I had the following constructors:

public Foobar(int x, int y, String name, double gravity){
this.x=x;
this.y=y;
this.name=name;
this.gravity=gravity;
}
public Foobar(int x, int y, String name){
this.x=x;
this.y=y;
this.name=name;
this.gravity=9.8;
}

I'd need to mark the x and y variables for use in the level editor, mark name as a required variable, and mark gravity as an optional variable.

I could go with 2 Hashtables each having the same exact keys, but I feel as if there must be a better way to approach this - another data structure perhaps? Any advice?
Advertisement
I actually don't understand what you want. Can you give a use-case? Why should the two constructors produce objects which are to be differentiated somehow?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Why should the two constructors produce objects which are to be differentiated somehow?

If I understand what you're asking, I simply mean that constructors are often overloaded. However, in the process of designing the level, I wanted the user to define the objects they create.
I have a Player object for instance that can turn 360 degrees. Based on the level being designed, the user may want the player to be turned 270 degrees at the start, but if they don't specify I want it to default to 90 degrees. This is an example of an option parameter, because in my code I've accounted for it, by having a constructor that doesn't include an angle parameter.
But other things may be required. Wall objects are a good example of that. For any wall object (as a rectangle), you need to tell the class what its width and height are.
Both the necessity of the parameters, as well as their name and type (int, double, boolean, string, etc.) need to be recorded. Rather than break it up into 3 different, hard-to-manage arrays, or two Hashtables, I wanted to know if there was an better option in terms of data structures.

If I understand what you're asking, I simply mean that constructors are often overloaded. However, in the process of designing the level, I wanted the user to define the objects they create.
I have a Player object for instance that can turn 360 degrees. Based on the level being designed, the user may want the player to be turned 270 degrees at the start, but if they don't specify I want it to default to 90 degrees. This is an example of an option parameter, because in my code I've accounted for it, by having a constructor that doesn't include an angle parameter.

Yes, I understand that (though in many languages you can achieve the same thing by specifying a default value for parameters, I'm not sure Java can though). I think I understand what you want - you need objects to behave differently depending on the arguments passed to the constructor. One way to do it is to use inheritance, where all objects in your world derive from a base Entity class, and you can make different classes such as Player, Wall, Enemy, etc.. inherit from this, all with their own individual behavior, arguments, constructors, etc... Then you can just store an array of Entity instances (which may be players, walls, etc.. you don't care) and call the appropriate methods, e.g. does this Entity intersect with another (regardless of what the entities actually represent). This may not work well if your entities are too different from one another and don't share common features, and inheritance is often not the preferred solution.

So here I don't think the problem is about data structures, but about design patterns.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Have you considered removing all optional parameters from constructors and setting their values after object creation?
There was an Item dealing with this in Effective Java (constructors with many non-essential parameters). The book proposed the builder pattern for dealing with that.

http://en.wikipedia....Builder_pattern

Though the book's example was more clear.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Thanks! With a little effort, I could rework this structure into my level designer. Might make it a bit more flexible.

This topic is closed to new replies.

Advertisement