Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need design/pattern/structure help on coding up a java 'world'

Tags:

java

I've always wanted to write a simple world in Java, but which I could then run the 'world' and then add new objects (that didn't exist at the time the world started running) at a later date (to simulate/observe different behaviours between future objects).

The problem is that I don't want to ever stop or restart the world once it's started, I want it to run for a week without having to recompile it, but have the ability to drop in objects and redo/rewrite/delete/create/mutate them over time.

The world could be as simple as a 10 x 10 array of x/y 'locations' (think chessboard), but I guess would need some kind of ticktimer process to monitor objects and give each one (if any) a chance to 'act' (if they want to).

Example: I code up World.java on Monday and leave it running. Then on Tuesday I write a new class called Rock.java (that doesn't move). I then drop it (somehow) into this already running world (which just drops it someplace random in the 10x10 array and never moves).

Then on Wednesday I create a new class called Cat.java and drop that into the world, again placed randomly, but this new object can move around the world (over some unit of time), then on Thursday i write a class called Dog.java which also moves around but can 'act' on another object if it's in the neighbour location and vice versa.

Here's the thing. I don't know what kinda of structure/design I would need to code the actual world class to know how to detect/load/track future objects.

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

like image 864
d33j Avatar asked Feb 02 '11 07:02

d33j


People also ask

Why do we need design patterns in java?

By using the design patterns you can make your code more flexible, reusable and maintainable. It is the most important part because java internally follows design patterns. To become a professional software developer, you must know at least some popular solutions (i.e. design patterns) to the coding problems.

Which design pattern is mostly used in java?

Factory Pattern We can apply a Singleton pattern on the Factory class or make the factory method static. Check out Factory Design Pattern for example program and factory pattern benefits. This is one of the most widely used java design patterns.

What is structural design pattern in java?

Structural design patterns are those that simplify the design of large object structures by identifying relationships between them. They describe common ways of composing classes and objects so that they become repeatable as solutions.

Why do we need design patterns?

A design pattern provides a general reusable solution for the common problems that occur in software design. The pattern typically shows relationships and interactions between classes or objects. The idea is to speed up the development process by providing well-tested, proven development/design paradigms.


3 Answers

I don't know if there is a pattern/strategy for a problem like this, but this is how I would approach it:

I would have all of these different classes that you are planning to make would have to be objectsof some common class(maybe a WorldObject class) and then put their differentiating features in a separate configuration files.

Creation
When your program is running, it would routinely check that configuration folder for new items. If it sees that a new config file exists (say Cat.config), then it would create a new WorldObject object and give it features that it reads from the Cat.config file and drops that new object into the world.

Mutation
If your program detects that one of these item's configuration file has changed, then it find that object in the World, edit its features and then redisplay it.

Deletion
When the program looks in the folder and sees that the config file does not exist anymore, then it deletes the object from the World and checks how that affects all the other objects.

like image 78
chustar Avatar answered Nov 15 '22 01:11

chustar


I wouldn't bet too much on the JVM itself running forever. There are too many ways this could fail (computer trouble, unexepected out-of-memory, permgen problems due to repeated classloading).

Instead I'd design a system that can reliably persist the state of each object involved (simplest approach: make each object serializable, but that would not really solve versioning problems).

So as the first step, I'd simply implement some nice classloader-magic to allow jars to be "dropped" into the world simulation which will be loaded dynamically. But once you reach a point where that no longer works (because you need to modify the World itself, or need to do incompatible changes to some object), then you could persist the state, switch out the libraries for new versions and reload the state.

Being able to persist the state also allows you to easily produce test scenarios or replay scenarios with different parameters.

like image 29
Joachim Sauer Avatar answered Nov 15 '22 01:11

Joachim Sauer


Have a look at OSGi - this framework allows installing and removing packages at runtime.

The framework is a container for so called bundles, java libraries with some extra configuration data in the jars manifest file.

You could install a "world" bundle and keep it running. Then, after a while, install a bundle that contributes rocks or sand to the world. If you don't like it anymore, disable it. If you need other rocks, install an updated version of the very same bundle and activate it.

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

The reference implementation is equinox


BTW: "I don't know what kinda of structure/design" - at least you need to define an interface for a "geolocatable object", otherwise you won't be able to place and display it. But for the "world", it really maybe enough to know, that "there is something at coordinates x/y/z" and for the world viewer, that this "something" has a method to "display itself".

like image 29
Andreas Dolk Avatar answered Nov 14 '22 23:11

Andreas Dolk