• Post Reply Bookmark Topic Watch Topic
  • New Topic

Design pattern for adding differnt kind of events to one class  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am developing a framework, where I have class that hold visible objects like tables. When an event occurs, I want other classes to be notified. I imagine it being used somehow like this:



But there are differnts questions that I ask myself:
  • Should the tableClass have a different method for each event type (double click, selection changed) that could happen?
  • Should the tableClass save every event type in one list and iterate through that list when the correspinding event occurs?
  • Some classes want to know which row has been selected, other only want to know the fact that something has happened. How do I cope with that? How should the object look like that the classes receive?


  • Thank you
     
    Bartender
    Posts: 563
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you considered using an EventBus or something like that? What you have outlined is similar to an Observable/Observer pattern, except your pattern outline requires the class originating the event to be responsible for knowing which classes have subscribed to which events and then responsible for notifying each subscriber. A much more flexible design that is easier to modify and maintain would be to simply send the event to the event bus and let those classes interested in that event become aware of it and take the desired action.

    I've just learned EventBus so am kind of high on it, and what you describe seems to be a great application for it. The EventBus examples and tutorials available on the web aren't great, but this is the simplest one I could find that was easily understandable and scalable to my own examples. I have written my own study from that tutorial that I can share with you on request.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!