How would I handle a set of differing event classes with differing handler interfaces in a single event processor?

I’m working on an event processor framework for a simple game I’m writing, in which multiple types of events are handled in a loop. Since these events carry distinct pieces of data (i.e. one carries a player and position, another carries a message and timestamp), I ended up creating different classes for them (though they still all implement a common interface, which is currently a marker interface).

Within my event processor, I have, for each type of event, a set of event handlers implementing a corresponding interface (e.g. anonymous class implementing PlayerInteractHandler that handles PlayerInteractEvents). Since these interfaces are being implemented through a Javascript engine (Rhino) I am unable to use a single generic interface.

In trying to implement the actual engine, I have currently code as follows (methods inlined to show idea behind code in a more compact representations):

if (recvdEvent instanceof FooEvent){
    // getHandlerList() returns a List<EventHandler> due to limitations of generics
    for(EventHandler eh : getHandlerList(FooHandler.class) {
        FooEvent eventAfterCast = (FooEvent) recvdEvent
    }
}

Obviously there are a bunch more else if (recvdEvent instanceof BarEvent) blocks, and I catch and handle ClassCastExceptions.

The problem with this, is that it seems like a mis-use of an object-oriented language, and I have to stringently verify other code by hand to retain type safety.

The other alternatives I know of are for my event to return the Class<? extends EventHandler> in a certain method, to use polymorphism / dynamic dispatch, but that would require a reflective cast, or a polymorphic method in the handler class that would still break complete type safety (and another layer on top of the interface itself).

Multiple event processors would lead to code duplication, and would imply having separate threads for each as the current event processor is designed. If I were to retain a single thread and processor I would need a single queue, whose declared type is AbstractQueue<GameEvent>, and I’d be back at square one.

Am I approaching this in a completely incorrect manner?

2

Visitor Pattern

It won’t help here. The best scenario to adopt Visitor Pattern is you have a fixed number of subclasses. However, in your example, you could have a lot of Event in the future. So, if you choose to use Visitor Pattern, every time you add a new kind of Event, you have to modify the Visitor. It’s a nightmare.

Reflection

It’s the right way to go. You have to sacrifice type safety for more dynamic dispatching. But, we can do something to improve the safety.

Guava EventBus use reflection and annotation to provide both compiling and runtime safety. You can check the implementation if you are interested. In summary, it sacrifice a little safety(you may have an event which doesn’t have corresponding handler, which I think is reasonable, but may not for your case), but what you gain is more clean and maintainable code.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật