Wednesday, December 30, 2009

Generics for Event Listener Pattern

To workaround the somethingListener -> somethingEvent mapping.

I'm not the first person to care about Event Notification. (but it's a problem that I'd rather not care about, especially after I met Qt's Signals and Slots... Java beaten by C++? Come on!)
    Maybe we can use Java 5 generics to save some code...
    // Common interface for all listeners. 
    public interface Listener<E> {
      public void notify(E event);
    public class PedalEvent {
      // any custom properties wherever necessary
    public class PedalListener implements Listener<PedalEvent> {
      public void notify(PedalEvent event) {
         // ... implementation ... 
    public class Car {
      public void addPedalListener(Listener<PedalEvent> pedalListener); 
      public void removePedalListener(Listener<PedalEvent> pedalListener); 
    What do you think?


    Anonymous said...

    Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us. | | | | | | | | | |

    coach outlet store online said... Coach Outlet Store Online Michael Kors Outlet Coach Factory Outlet Toms Shoes Cheap Jerseys Michael Kors Coach Outlet Coach Factory Outlet Coach Outlet Coach Handbags Giuseppe Zanotti Coach Factory Outlet Coach Factory Outlet Online Coach Outet Store Online Coach Factory Outlet Michael Kors Outlet Online Celine Outlet Michael Kors Michael Kors Store Tomss Hoes Prada Outlet Fitflop Footwear Celine Bags Fitflops Flipflops Fitflops Giuseppe Zanotti Designer Fitflops Giuseppe Zanotti Sneakersmen Fitflops

    Nithyanantham M said...

    Thanks for sharing this post
    Dailymotion video downloader

    Try Dailymotion video
    to download videos from dailymotion.

    Post a Comment

    Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan