New classes created by users?

Consider this situation: I have an aquarium simulator, where I have 5 different types of fish. Different types mean different attributes (speed, color, hunger, etc.). What if I want a user of my simulator to be able to create a new type of fish and assign it its values ​​for its attributes?

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

(In case of need, the language is Java. And to avoid any misunderstandings and to prevent comments such as “is this uni work?”, Yes, but it is not. But I'm not looking for an answer, I'm curious about the concept.)

EDIT: Yes, I feel bad that I did not mention the interaction method: graphical interface.

So, imagine a tab called "Add New Species", in which there is a field for each attribute of the fish (type, speed, color, etc.). Thus, the user fills in the fields with the appropriate values, and when he clicks "add", the constructor is called. At least as I imagine it. :)

+6
java types dynamic user-controls user-input
source share
4 answers

Look at the type of template type . Also google for this i just gave one of the first links i found ...

You can also see the reflection pattern ...

0
source share

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"); } } 
+1
source share

Java is an OO language, and it deals with classes and objects. A tempting, naive solution would be for your program to deal with "classes" of fish, for example, with classes of something, i.e. To create some Java code and let the compiler and loader present it at runtime.

This approach can be used to work with some awkwardness. Essentially, your “dynamic Java class classes” would probably be much bigger and more complex than you actually thought.

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

To ask, I think you really only need one Fish class. When a user defines a new one, what he really defines is the attribute values.

If you really need new and dynamic attributes, you can go a long way using, for example, a HashMap to store name / value pairs. You can allow the user to add "legs" / "4" and then print this new attribute as is; but you couldn’t get the fish to walk on these legs because you lacked the code to work with the new attribute.

+1
source share

Let the user define the attribute values ​​of an instance of, say, the FishSpecies class, and give FishSpecies the createFish method that creates the fish of this species (i.e., has these attribute values). Keeping track of all the FishSpecies objects in the list gives you the ability to manage FishSpecies and create Fish objects of this kind.

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

0
source share

All Articles