views:

105

answers:

4

Consider this situation: I've got an aquarium simulator where I have 5 different types of fishes. Different types means different attributes (speed, colour, hunger, etc). What if I want the user of my simulator to be able to create a new type of fish and give it its values for its attributes?

How is that implemented by the programmer? Do I need some kind of "event handling" that will add a specific bunch of lines of code in my "Fish" class? Is that even a valid thought?

(In case it's essential, the language is Java. And to avoid any misunderstandings and prevent comments like "is this uni work?", yes it is. But I am not looking for THE answer, I am curious about the concept.)

EDIT: Yeah, my bad that I didn't mention the interaction way: a GUI.

So, imagine a tab called "Add New Species" that has a field for every attribute of the fishes (type, speed, colour, etc). So, the user fills in the fields with the appropriate values and when he clicks on "add" the constructor is called. At least that's how I imagine it. :)

+1  A: 

I would just use a map:

class Fish
{
  Map<String,String> attributes = new HashMap<String,String>();
  setBusterFish()
  {
    attributes.put("speed", "5");
    attributes.put("colour", "red");
    attributes.put("hunger", "10");
    attributes.put("name", "buster");
  }
}
Colin
+1  A: 

Java is an OO language, and it deals in classes and objects. The tempting, naive solution would be to have your program deal with "classes" of fish like it deals with classes of anything, i.e. to create some Java code and let the compiler and loader introduce it into the runtime.

This approach can be made to work, with some awkwardness. Essentially your "dynamic Java classes" coding would probably end up much bigger and complicated than your assignment actually intends.

You only really need to do this if you are actually going to have different attributes (not just different values of those attributes) for your different fish; and even then there are simpler solutions.

For what's being asked, I think you really only need one Fish class. When the user defines a new one, what he's really defining are the attribute values.

If you really want new and dynamic attributes, then you could go a long way using e.g. a HashMap to store name/value pairs. You could let the user add "legs" / "4" and then print out that new attribute as-is; but you couldn't make the fish walk on those legs because you'd be missing coding to work with the new attribute.

Carl Smotricz
What if he defines a new one, then creates another two of an already existing type, and then wants another one of the new type he created before? The "standard" attributes of every new type have to be saved somewhere so users don't need to insert them every single time.
sebkom
Really, then, you're not getting your users to enter new Fish -- you're getting them to enter new `FishPrototypes` or `FishSpecies` or `FishMetadata`. So if you had `class Fish { private FishSpecies speciesInformation; }` and then create a new `FishSpecies` for every entry; you can easily call `new Fish(salmonSpeciesInformation)` every time they want a new salmon. Other possibilities include using `originalFish.clone()` to copy a previous fish, `Fish.copyOf(originalFish)` or `new Fish(originalFish)`... there are many possible approaches.
Cowan
+1  A: 

Let the user define attribute values of an instance of, say, a FishSpecies class, and give the FishSpecies a method createFish that creates a fish of that species (i.e. having those attribute values). Keeping track of all FishSpecies objects in a list grants you the opportunity to manage FishSpecies objects, and create Fish objects of given species.

If I understand your question correctly, then I believe that complicating things more than this is a mistake.

Buhb
+1  A: 

Hello,

Have a look at the type object pattern. Also google for it I just gave one of the first references I found...

You may also look the Reflection pattern...

pgras
The article that made it completely clear to me is this one: http://gameprogrammingpatterns.com/type-object.htmlThanks, great pattern in general and spot on in specific!
sebkom