Simple java message dispatching system

前端 未结 3 1885
清歌不尽
清歌不尽 2021-01-29 20:35

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.

3条回答
  •  难免孤独
    2021-01-29 20:55

    If you want to avoid instanceof, then your only bet is to use inheritance to route a method call to the right method. You cannot use method overloading, since that is decided at compile time by the declared type of the variable you are passing to a method. You have to use inheritance.

    If you cannot use inheritance, then your only other choice (that I'm aware of) involves a lot of instanceof.

    Regarding a messaging system, you can use ActiveMQ as an in-the-JVM message transport. You do not have to use it via socket or other means. I can't imagine that ActiveMQ would not be efficient enough for your means.

提交回复
热议问题