This is about a model which spans all the other models, and assists them in communicating with each other.
There are really two kinds of events in Clash - those which have an immediate effect, and those which have an effect at the "round-up" at the end of each turn.
The first kind of event is normally dealt with within a model and will not be discussed here.
The second kind of event covers a great range of actual situations. Presently there is code which, in various ways, produces event for display at the end of the turn, and another kind for passing to the economic social models for effects of riots. A third kind saves up data for combat reports. The social model code also has some embryonic effects which at present are actually just messages (strings).
These events represent communication between models. It is easy to slip into the situation where every model talks to every other model, often in a variety of ways.
If that happens, every model becomes dependent on every other model. A change to any model's code means changing the code in every model. This means disaster in the coding.
What is needed is some sort of mechanism which means that each model talks only to a single entity, both to report events and to react to them. Also needed is a uniform event interface, implemented by all the varied kinds of event.
It must be possible to determine from the event itself who will be interested, so the event can be sent to the right destination or destinations.
There is in Java a publish/subscribe system (the Observer/Observable system). This is a system which I do not like at all, for a variety of reasons. It is not my purpose here to argue the pros and cons of that system. It is sufficient to point out that everything that is observable has its own list of observers, so that each of the observers needs to know about the thing observed. This leads to the kind of dependencies that I mentioned above.
The kind of model I suggest is based on the broker pattern.
There is a single entity (class instance) which is the event broker. Any events which happen are sent to the event broker, which stores them.
Anything interested in events of a particular kind registers itself with the event broker as being interested in events of that kind.
At the end of the turn, the event broker goes through all the events in the queue and "posts" them to the entities that have expressed an interest in that kind of event.
Thus, all models need to know about the event interface and the event broker, only.
If I have a coding battle cry, it is likely to be "Down with dependencies". And this model goes a long way toward achieving that.
It remains to design the event interface...
Cheers
There are really two kinds of events in Clash - those which have an immediate effect, and those which have an effect at the "round-up" at the end of each turn.
The first kind of event is normally dealt with within a model and will not be discussed here.
The second kind of event covers a great range of actual situations. Presently there is code which, in various ways, produces event for display at the end of the turn, and another kind for passing to the economic social models for effects of riots. A third kind saves up data for combat reports. The social model code also has some embryonic effects which at present are actually just messages (strings).
These events represent communication between models. It is easy to slip into the situation where every model talks to every other model, often in a variety of ways.
If that happens, every model becomes dependent on every other model. A change to any model's code means changing the code in every model. This means disaster in the coding.
What is needed is some sort of mechanism which means that each model talks only to a single entity, both to report events and to react to them. Also needed is a uniform event interface, implemented by all the varied kinds of event.
It must be possible to determine from the event itself who will be interested, so the event can be sent to the right destination or destinations.
There is in Java a publish/subscribe system (the Observer/Observable system). This is a system which I do not like at all, for a variety of reasons. It is not my purpose here to argue the pros and cons of that system. It is sufficient to point out that everything that is observable has its own list of observers, so that each of the observers needs to know about the thing observed. This leads to the kind of dependencies that I mentioned above.
The kind of model I suggest is based on the broker pattern.
There is a single entity (class instance) which is the event broker. Any events which happen are sent to the event broker, which stores them.
Anything interested in events of a particular kind registers itself with the event broker as being interested in events of that kind.
At the end of the turn, the event broker goes through all the events in the queue and "posts" them to the entities that have expressed an interest in that kind of event.
Thus, all models need to know about the event interface and the event broker, only.
If I have a coding battle cry, it is likely to be "Down with dependencies". And this model goes a long way toward achieving that.
It remains to design the event interface...
Cheers
Comment