Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 221279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:01:06+00:00 2026-05-11T19:01:06+00:00

I’m working on a little Java game in which all sorts of events can

  • 0

I’m working on a little Java game in which all sorts of events can happen. There are at least a couple of dozen basic events that various event handlers may be interested in. There are also several places in the code where these events might be triggered. Rather than forcing the event listeners to know which class they need to register with, I’d like to create some sort of a centralized message-dispatching system, which some classes would submit events into and interested classes could hook into to listen for certain kinds of events.

But I have some questions. Firstly, this seems like an obvious and common problem. Are there favorite implementations of simple, in-VM messaging systems? Seems like there would be.

Secondly, and more importantly, I’m trying to work out a reasonably elegant way for the dispatching class to know as little as possible about types of messages. I’d love to be able to create new kinds of events without modifying the message dispatcher at all. However, I have an opposite concern. I’d really like for the method signatures of the handling methods to be clear. In other words, I would prefer the following:

public class CollisionConsoleHandler implements CollisionListener {
  @Override
  public void spaceshipCollidedWithMeteor( Spaceship spaceship, Meteor meteor ) {
      //...
  }
}

over something more generic and harder to read:

public class CollisionConsoleHandler implements GameMessageListener {
   @Override
   public void handleMessage( GameMessage message ) {
     if( message instanceof SpaceshipCollisionMessage ) {
        Spaceship spaceship = ((SpaeshipCollisionMessage)message).getSpaceship();
        Meteor meteor = ((SpaeshipCollisionMessage)message).getMeteor();
        //...
     }
   }
}

But I don’t see any good ways to keep type-specific knowledge out of the dispatcher while at the same time keeping the method signatures clean and readable.

Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:01:07+00:00Added an answer on May 11, 2026 at 7:01 pm

    If there is a specific listener interface for each event. Each event is able to issue listeners calls itself. Then, the role of the dispatcher is to identify target listeners and to trigger the event notification on them.

    For example, a generic event definition can be:

    public interface GameEvent<L> {
    
       public void notify( final L listener);
    }
    

    If your CollisionListener is:

    public interface CollisionListener {
    
        public void spaceshipCollidedWithMeteor( Spaceship spaceship, Meteor meteor );
    
    }
    

    Then, the corresponding event can be:

    public final class Collision implements GameEvent<CollisionListener> {
    
       private final Spaceship ship;
       private final Meteor meteor;
    
       public Collision( final Spaceship aShip, final Meteor aMeteor ) {
          this.ship = aShip;
          this.meteor = aMeteor;
       }
    
       public void notify( final CollisionListener listener) {
          listener.spaceshipCollidedWithMeteor( ship, meteor );
       }
    
    }
    

    You can imagine a dispatcher which is able to propagate this event on the target listeners like in the following scenario (Events is the dispatcher class):

    // A unique dispatcher
    final static Events events = new Events();
    
    // Somewhere, an observer is interested by collision events 
    CollisionListener observer = ...
    events.listen( Collision.class, observer );
    
    // there is some moving parts        
    Spaceship aShip = ...
    Meteor aMeteor = ...
    
    // Later they collide => a collision event is notified trough the dispatcher
    events.notify( new Collision( aShip, aMeteor  ) );
    

    In this scenario, the dispatcher did not require any knowledge on events and listeners. It triggers an individual event notification to each listener, using only the GameEvent interface. Each event/listener pair chooses it’s own dialog modalities (they can exchange many messages if they want).

    A typical implementation of a such dispatcher should be something like:

    public final class Events {
    
       /** mapping of class events to active listeners **/
       private final HashMap<Class,ArrayList> map = new HashMap<Class,ArrayList >( 10 );
    
       /** Add a listener to an event class **/
       public <L> void listen( Class<? extends GameEvent<L>> evtClass, L listener) {
          final ArrayList<L> listeners = listenersOf( evtClass );
          synchronized( listeners ) {
             if ( !listeners.contains( listener ) ) {
                listeners.add( listener );
             }
          }
       }
    
        /** Stop sending an event class to a given listener **/
        public <L> void mute( Class<? extends GameEvent<L>> evtClass, L listener) {
          final ArrayList<L> listeners = listenersOf( evtClass );
          synchronized( listeners ) {
             listeners.remove( listener );
          }
       }
    
       /** Gets listeners for a given event class **/
       private <L> ArrayList<L> listenersOf(Class<? extends GameEvent<L>> evtClass) {
          synchronized ( map ) {
             @SuppressWarnings("unchecked")
             final ArrayList<L> existing = map.get( evtClass );
             if (existing != null) {
                return existing;
             }
    
             final ArrayList<L> emptyList = new ArrayList<L>(5);
             map.put(evtClass, emptyList);
             return emptyList;
          }
       }
    
    
       /** Notify a new event to registered listeners of this event class **/
       public <L> void notify( final GameEvent<L> evt) {
          @SuppressWarnings("unchecked")
          Class<GameEvent<L>> evtClass = (Class<GameEvent<L>>) evt.getClass();
    
          for ( L listener : listenersOf(  evtClass ) ) {
             evt.notify(listener);
          }
       }
    
    }   
    

    I suppose its fulfills your requirements:

    • very light,
    • fast,
    • no casts (at usage),
    • Every thing is checked at compile
      time (no possible mistake),
    • No API constraints on listeners (each event
      choose it’s own messages),
    • Evolutive (no dependencies between
      different events and/or listeners),
    • The dispatcher is a generic black
      box,
    • Consumers and producers don’t need to
      know each other.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.