Need for clean publish/subscribe API (AKA Observer pattern)
This may not be the right discussion thread in regards to a publish/subscribe API that works in the GWT environment.
My comments below will be code design oriented and they are not a bug report. Please forgive me if this is not the right place to post such a comment.
My motivation for using a Java reflection API that works in the GWT environment was driven by the lack of simple and available publish/subscribe APIs. I mainly needed access to annotations and the invoke capability.
IMHO, A publish/subscribe API is crucial to writing clean loosely coupled UI code.
Through Google searches is seems the publish/subscribe API most recommended is GWT's EventBus API.
I looked at this and wrote some sample code. I felt this was not a great solution because:
Consequently I produced a very simple publish/subscribe API using GWT ENT .
Here is a subscriber code example.
Here is a publish code example:
This code works.* I'm happy to share this library with anyone interested.
(*I was able to find a work-around for the compile failure I was experiencing with GWT-ENT)
Originally posted by sjivan
View Post
My comments below will be code design oriented and they are not a bug report. Please forgive me if this is not the right place to post such a comment.
My motivation for using a Java reflection API that works in the GWT environment was driven by the lack of simple and available publish/subscribe APIs. I mainly needed access to annotations and the invoke capability.
IMHO, A publish/subscribe API is crucial to writing clean loosely coupled UI code.
Through Google searches is seems the publish/subscribe API most recommended is GWT's EventBus API.
I looked at this and wrote some sample code. I felt this was not a great solution because:
- To implement even a single event takes a about 15 lines of code.
- The 15 lines of code are very complex, hard to understand, and hard to read. It uses Java language constructs that probably are not familiar to many Java programmers (me included!).
- The use of this API doesn’t produce readable code. If I would like to create and use many events then my code’s readability will become polluted with this code. One of the great things about SmartGWT is that if it is used right it produces very readable code.
Consequently I produced a very simple publish/subscribe API using GWT ENT .
Here is a subscriber code example.
Code:
public class SomeSubscriber { private int eventCount = 0; String whatIsSomething = "No event yet"; public SomeSubscriber(){ EventHub.subscribe(this); } @Listener public void handler1(Event1 event1){ whatIsSomething = event1.getSomething(); eventCount++; } }
Code:
Event1 event1 = new Event1(); Event1.setSomething(“this is something”); EventHub.publish(event1);
(*I was able to find a work-around for the compile failure I was experiencing with GWT-ENT)
Comment