|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Interface for messages in the Wyvern publish/subscribe system. This system allows for any N objects in the game to send messages without being tightly coupled (i.e. without having references to each other.) This reduces inter-class dependencies, and allows you to change the name of one class without needing to change the name of the other.
This shouldn't be used for performance-critical applications within the game. It's useful for having objects notify each other that something interesting has happened. For example, there's a pubsub message to indicate that a Live Quest has started, and another message sent when it's over.
Each Message has a Subject and a Body - you can think of it almost like an email message being sent between objects in the game. The subjects are heirarchical namespaces, so they have names like "wiz.janica.auto-lq.started", and "wiz.janica.auto-lq.ended". If you want to subscribe to all of the messages related to janica's auto-LQs (Automatic Live Quests), you subscribe to "wiz.janica.auto-lq." Simple! Be careful not to subscribe to more events than you actually need, however, or you'll overwhelm the system.
Message bodies are loosely typed. The Body can be pretty much anything, and it's up to the class that publishes the message to document what the body is. For convenience, messages can also have named attachments, which are just optional objects that didn't go in the Body, for whatever reason.
For example, if you're sending a message that a certain player has entered map M at location P, with coordinates X, Y, you could construct the Message in any number of ways:
Hashtables are convenient, but they lack strong typing, and have slower performance than doing method calls to access instance variables. If you're publishing a message that could have lots (e.g. dozens or hundreds) of Subscribers, you should consider not using attachments. You could create your own class for the message body, with getter methods to get the parameters.
Another possibility is to use Message "slots". If you have five or fewer attachments in your message, you can use five numbered "slots" in the message, which are just instance variables that hold Object references. They each have their own accessor, rather than passing in an index, to avoid object allocation for an internal array, and to help avoid programming errors where someone tries to use too many slots. They still don't have strong typing, and it's possible to get the slot numbers mixed up, so double-check your published messages and subscribers to make sure you're publishing and reading the correct slot attachments.
In general, the best way to use these Messages is:
Field Summary | |
static int |
NUM_SLOTS
|
Method Summary | |
java.lang.Object |
getAttachment(java.lang.String name)
Messages can have attachments, which are just additional data that might or might not be interesting to subscribers. |
java.lang.Object |
getBody()
The body of the message. |
java.lang.Object |
getSlot1()
Returns the object stored in slot 1. |
java.lang.Object |
getSlot2()
Returns the object stored in slot 2. |
java.lang.Object |
getSlot3()
Returns the object stored in slot 3. |
java.lang.Object |
getSlot4()
Returns the object stored in slot 4. |
java.lang.Object |
getSlot5()
Returns the object stored in slot 5. |
java.lang.String |
getSubject()
Returns the Subject for this message. |
Field Detail |
public static final int NUM_SLOTS
Method Detail |
public java.lang.String getSubject()
public java.lang.Object getBody()
public java.lang.Object getAttachment(java.lang.String name)
public java.lang.Object getSlot1()
public java.lang.Object getSlot2()
public java.lang.Object getSlot3()
public java.lang.Object getSlot4()
public java.lang.Object getSlot5()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |