开发者

New classes created by users?

开发者 https://www.devze.com 2022-12-18 03:38 出处:网络
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 m

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 h开发者_Python百科ow I imagine it. :)


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");
  }
}


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.


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消