Archived

This topic is now archived and is closed to further replies.

Preacher

[java] Implementing Custom Event Model

Recommended Posts

Preacher    154
Hi im looking to develop my a set of custom events (For a Chess Game so events such as Move etc) and an event broadcaster. This event broadcaster will be the central part of a plugin architecture i want to implement, where each plugin will implemnt a plugin interface (To flag its a plugin) and an optional set of listener interfaces such as MoveListener and PropertyChangeListener etc. Each pulgin will register with a core module which will hold the event broadcaster and then receive appropiate events. I was wondering does anyone have any experience in implementing a system anyway like this, i have a good idea of how the plugin side will work im just a bit shaky about the event broadcaster and creating my own events. Im grateful for all contribution

Share this post


Link to post
Share on other sites
fractionMan    122
Sound simple to me,
you need your broadcaster to have three methods for each event. One to register listeners, one to remoeve a listener and one to fire events. You need a set of listeners for each event and an interface for each event.

--> for the broadcaster:

public void regAnEventListener(AnEventListener el){
anEventSet.add(el);
}

public void removeAnEventListener(AnEventListener el){
anEventSet.remove(el);
}

private void fireAnEvent(){
//Psudeo code, loop through the set and call the method defined in the appropriate interface for each registrant in turn.
}

Each class that wants to listen to events has to implement the appropriate interface and register with the broadcaster passing itself as a parameter. You can use the eventInterfaces to define exacly what information will be passed for each event.

I might be over simplifying things (this aint multithreaded etc), but it'll work.

[edited by - fractionMan on January 15, 2004 9:25:50 AM]

Share this post


Link to post
Share on other sites
garazdawi    145
I have no idea of how you could create your own event, what I would do is that I would catch events caused my the system (MouseMotion, Mouse, Keyboard etc) and then in their implementing methods I would trigger maybe a Move event or whatever depending on certain contditions. So bacially

class Chess implements MouseListener {
public EventDispatcher listener;
// contructor and other methods

public Chess(EventDispatcher plugin){
listener=plugin;
}

public void mousePressed(MouseEvent evt){
if (moving) {
listener.moveEvent();
}
}
}

I have no idea how this is going to work out but u might wanna give it a try....

Share this post


Link to post
Share on other sites