Need a construction / structure / structure of assistance in coding the Java world,

I always wanted to write a simple world in Java, but then I could start the “world” and then add new objects (which were not there when the world started working) at a later date (simulate / observe different behaviors between future objects) .

The problem is that I do not want to ever stop or restart the world after it starts, I want it to work for a week without recompiling it, but be able to drop objects and remodel / rewrite / delete / create / mutate them with time.

The world may be as simple as a 10 x 10 x / y 'location' array (think of a chessboard), but I probably need some kind of ticktimer process to monitor objects and give everyone (if any) a chance to "act "(if they want).

Example: I code up World.java on Monday and leave it turned on. Then on Tuesday I write a new class called Rock.java (which does not move). Then I throw it (somehow) into this already running world (which just discards it randomly in a 10x10 array and never moves).

Then on Wednesday I create a new class called Cat.java and delete it in a world randomly placed again, but this new object can move around the world (for some unit of time), then on Thursday I write a class called Dog.java , which also moves, but can "act" on another object if it is in a neighboring location and vice versa.

Here is the thing. I don’t know what type of structure / design I will need to encode a real world class, to know how to define / load / track future objects.

So, any ideas on how you will do something like this?

+7
source share
7 answers

I do not know if there is a template / strategy for such a problem, but here is how I approach this:

I would like all these different classes that you plan to make to be an object of some general class (possibly a WorldObject class) and then put your distinguishing functions in separate configuration files.

Creature
When your program is running, it will regularly check this configuration folder for new items. If he sees that a new configuration file exists (for example, Cat.config), he will create a new WorldObject and provide him with the functions that he reads from the Cat.config file, and transfer this new object to the world.

Mutation
If your program detects that one of these file configuration files has been modified, it finds this object in the world, edits its functions and then re-displays it.

Delete
When the program looks in the folder and sees that the configuration file no longer exists, it removes the object from the world and checks how it affects all other objects.

+1
source

I would not push too much on the JVM, which works forever. There are too many ways that a crash can occur (computer problems, unexpected memory shortages, problems with the pergene due to class reloading).

Instead, I would develop a system that can reliably maintain the state of each object (the simplest approach is to make each object serializable, but this will not solve the version problem).

So, as a first step, I would just implement some nice manual class loading to allow it to “fall” into a global simulation that will load dynamically. But as soon as you reach the point where it no longer works (because you need to change the world itself or you need to make incompatible changes for any object), you can save the state, disable libraries for new versions and reload the state.

The ability to save state also makes it easy to create test scripts or playback scripts with various parameters.

+1
source

Take a look at OSGi - this environment allows you to install and remove packages at run time.

The structure is a container for so-called packages, java libraries with some additional configuration data in the jars manifest file.

You can install the "world" kit and maintain it. Then, after some time, install a kit that brings stones or sand to the world. If you don't like it anymore, turn it off. If you need other stones, install an updated version of the same package and activate it.

And with OSGi, you can keep the world cool and moving around the sun.

Reference implementation of equinox


BTW: “I don’t know what kind of structure / design it is” - at least you need to define an interface for a “geolocation-bound object”, otherwise you cannot place and display it. But for the “world”, indeed, it may be enough to know that “there is something in the x / y / z coordinates” and for the world viewer that this “something” has a way of “showing itself”.

+1
source

If you only care about adding classes (and don't change), here is what I would do:

  • There is an Entity interface with all the necessary business methods ( insertIntoWorld() , isMovable() , getName() , getIcon() , etc.)
  • there is a specific package in which the objects are located
  • a task is scheduled in your application that lists package class files every 30 seconds
  • track classes and for any new class try to load the class and impose on Entity
  • for any newly loaded Entity create a new instance and name it insertIntoWorld() .

You can also skip the scheduler and auto-discovery task and have a user interface control in World where you can specify the name of the class to load from.

Some problems:

  • You cannot easily update Entity . Most likely you will need the magic of the loadload class
  • you cannot extend the Entity interface to add a new business business, so you are tied to the contract with which you started your application with
0
source

Too long explanation of too simple problem. In other words, you just want to dynamically load the classes.

First, if you somehow know the name of the class, you can load it using Class.forName() . This is the way to get the class. Then you can create an instance using Class.newInstance() . If the class has a public default constructor, this is enough. Read more about the reflection API.

But how do you pass the name of a new class to a program that is already running? I would suggest two ways.

  • A program can poll a predefined file. If you want to deploy a new class, you need to register it, that is, write its name to this file. In addition, this class must be available in the classpath of your application. application
  • can poll (for example) a special directory containing jar files. When it discovers a new jar file, it can read its contents (see JarInputStream ), then call a new instance of the class using ClaasLoader.defineClass() , then call newInstane (), etc.
0
source

What you basically create here is called the application container. Fortunately, there is no need to reinvent the wheel, there are already excellent software tools that are designed for long-term functioning, executing code that can change over time. I would suggest choosing your IDE first, and this will lead you to which application container you should use (some of them are better integrated than others).

You will need a persistence layer, the JVM will be reliable, but eventually someone will send a power cable and wipe your world. Again with JPA et al. There is no need to reinvent the wheel. Hibernate is probably "standard", but with your requirements I would try something more interesting with one of the NoSQL graph-based solutions.

0
source

what you probably want to see is the dynamic object model template / approach. I implemented it a while ago. With it, you can create / modify objecttypes at run time, which are some kind of templates for objects. Here is a document that describes the idea:

http://hillside.net/plop/plop2k/proceedings/Riehle/Riehle.pdf

There are more papers, but I could not publish them, because this is my first answer, and I do not have a sufficient reputation. But Google is your friend :-)

0
source

All Articles