Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Using the Correct Data Structure


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 epicpunnum   Members   -  Reputation: 458

Like
0Likes
Like

Posted 18 December 2012 - 04:19 PM

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?

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 9289

Like
0Likes
Like

Posted 18 December 2012 - 04:29 PM

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?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 epicpunnum   Members   -  Reputation: 458

Like
0Likes
Like

Posted 18 December 2012 - 05:39 PM

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.

#4 Bacterius   Crossbones+   -  Reputation: 9289

Like
0Likes
Like

Posted 18 December 2012 - 05:58 PM

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.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 Suspense   Members   -  Reputation: 449

Like
0Likes
Like

Posted 18 December 2012 - 05:59 PM

Have you considered removing all optional parameters from constructors and setting their values after object creation?

#6 TheChubu   Crossbones+   -  Reputation: 4787

Like
0Likes
Like

Posted 18 December 2012 - 07:38 PM

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.

Edited by TheChubu, 18 December 2012 - 07:43 PM.

"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


#7 epicpunnum   Members   -  Reputation: 458

Like
0Likes
Like

Posted 19 December 2012 - 08:42 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS