Sign in to follow this  

Using the Correct Data Structure

This topic is 1854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
[CODE]
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;
}
[/CODE]
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1355869769' post='5012225']
Why should the two constructors produce objects which are to be differentiated somehow?
[/quote]
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 [i]doesn't[/i] 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.

Share this post


Link to post
Share on other sites
[quote name='epicpunnum' timestamp='1355873979' post='5012246']
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.
[/quote]
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 [b]design patterns[/b].

Share this post


Link to post
Share on other sites
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.

[url="http://en.wikipedia.org/wiki/Builder_pattern"]http://en.wikipedia....Builder_pattern[/url]

Though the book's example was more clear. Edited by TheChubu

Share this post


Link to post
Share on other sites
Thanks! With a little effort, I could rework this structure into my level designer. Might make it a bit more flexible.

Share this post


Link to post
Share on other sites

This topic is 1854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this