Is there a better way than a sequence of if's to handle events?

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-03-30T02:44:21Z Indexed on 2010/03/30 3:13 UTC
Read the original article Hit count: 205

Filed under:
|
|

I recently ran across several objects implemented to handle events with a hard coded mapping using this pattern:

public void handleEvent(Event event)
{
    if(event.getCode() == SOME_STATIC_EVENT)
        doSomething(event);
    if(event.getCode() == ANOTHER_STATIC_EVENT)
        doSomethingElse(event);
}

where doSomething functions are implemented as methods of the same class.

In hopes of striving for looser coupling, how would you suggest abstracting out this pattern? Also, what's the best approach for mapping 0..N functions to a fired event?

© Stack Overflow or respective owner

Related posts about java

Related posts about abstraction