dynamic number of functioons - need for a pattern

Hey all!

I'm currently developing web based game, with a quite original core system. You can skip the details to the point who brings me troubles.

In this game, players sends units in the "wolrd", to fetch gold packs. Players have no real-time view of what's happening to their units; instead, they receive a written report of what has happened during the units' journey, when they come back to home.

These reports are built out of 3 to 7events, depending on the length of the trip.

In fact, the game is a big pool of events - i.e. short stories, that are non-linear. They are tree-shaped, thus they have several possible executions. The events tell the players stories in a roman-fashion, but they also specify the bonuses the units gain (like gold, items etc...).

I invented a simple language, designed to simplify the writing of events. Non-java programmer are supposed to write these events. I also developped a compiler that does the job of translating these events into JAVA. The compiler works, however I have no idea how to present the output.

Here is the trouble :

As events should be represented by objects, I'd prefer an OO solution. I need to retain information about events, such as "which player as already encountered such event".

But if I need to write a class for each event, and the game gets really big later on, there will be hundreds of classes loaded in the server (TOMCAT) and I don't like that.

Note that I can't use a template class, and instanciate it with the code of the event, simply because itis code, with statements, flow control etc....

If only I could instanciate that template, providing a method to its constructor, I'd be happy. Any one knows how to do that?

[1786 byte] By [bestama] at [2007-10-2 14:22:13]
# 1

> Here is the trouble :

> As events should be represented by objects,

Why?

> I'd

> prefer an OO solution. I need to retain information

> about events, such as "which player as already

> encountered such event".

> But if I need to write a class for each event, and

> the game gets really big later on, there will be

> hundreds of classes loaded in the server (TOMCAT) and

> I don't like that.

> Note that I can't use a template class, and

> instanciate it with the code of the event, simply

> because it is code, with statements, flow

> control etc....

> If only I could instanciate that template, providing

> a method to its constructor, I'd be happy. Any one

> knows how to do that?

What about a single class called event with many instances? If memory is a problem you can cache them to the file system - like in a database.

You create an interface. The interface is implemented by all of the code components. If you can do it via an interface then the most likely reason is that you are not abstracting the actual functionality enough.

Other than that I suspect you are looking for dynamic code invocation. You can start by looking at java.lang.Class and at java.lang.reflect.

After you understand the above then (and only then) you might want to look at custom class loaders.

jschella at 2007-7-13 12:40:41 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I don't know that loading hundreds of classes is necessarily a problem. I agree with jschell, though, that you don't necessarily need a class for each one.

In any case, one or more of the following may be useful to you:

http://java.sun.com/docs/books/tutorial/reflect/

http://www.beanshell.org/

http://cglib.sourceforge.net/

jverda at 2007-7-13 12:40:41 > top of Java-index,Other Topics,Patterns & OO Design...