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 8870369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:42:36+00:00 2026-06-14T17:42:36+00:00

Having a bit of trouble with a project at the moment. I am implementing

  • 0

Having a bit of trouble with a project at the moment. I am implementing a game and I’d like to have it event-driven.

So far I have an EventHandler class which has an overloaded method depending on what type of event is generated(PlayerMove, Contact, Attack, etc)

I will have either the game driver or the class generate the events. My question is how can I efficiently handle the events without tightly coupling the event generating classes to the event handler and making the use of EDA redundant?

I want to design my own simple handler and not use the built-in Java one for this

  • 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-06-14T17:42:37+00:00Added an answer on June 14, 2026 at 5:42 pm

    If you want to have a single EventHandler class with overloaded methods and your event types do not have any subclasses, then this simple code, which uses reflection, should work:

    public class EventHandler {
        public void handle (final PlayerMove move) {
           //... handle
        }
    
        public void handle (final Contact contact) {
           //... handle
        }
    
        public void handle (final Attack attack) {
           //... handle
        }
    }
    
    public void sendEvent (final EventHandler handler, final Object event) {
        final Method method = EventHandler.class.getDeclaredMethod ("handle", new Class[] {event.getClass ()});
        method.invoke (handler, event);
    }
    

    However, if you want to have seperate EventHandlers for different events, the following would be better.

    public interface EventHandler<T extends Event> {
        void handle (T event);
    }
    
    public class PlayerMoveEventHandler implements EventHandler<PlayerMove> {
        @Override
        public void handle (final PlayerMove event) {
            //... handle
        }
    }
    
    public class EventRouter {
        private final Map<Class, EventHandler> eventHandlerMap = new HashMap<Class, EventHandler> ();
    
        public void sendEvent (final Event event) {
            eventHandlerMap.get (event.getClass ()).handle (event);
        }
    
        public void registerHandler (final Class eventClass, final EventHandler handler) {
            eventHandlerMap.put (eventClass, handler);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of trouble using the List.Find with a custom predicate i have
First django app and am having a bit of trouble my project is laid
I'm having quite a bit of trouble linking a test project of FLTK I'm
So I'm having trouble converting my game over to XML/Linq, which I need to
I'm having a bit of trouble with including javax.script.*; in an Android project. I
I am having a bit of trouble building my project with Ant. I wanted
i have a bit of stupid question, but i'm really having trouble with it.
In the project i'm currently working on, I am having a bit of trouble
Having a bit of trouble wrapping my head around this one. I'm sure the
Im having a bit of trouble getting my JSON to be recognised by my

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.