Hey, Hope all of you are having nice time out there.. Today I am interested in sharing one of my own framework how to create totally decoupled GUI apps. But, what does that mean? Well, that ‘s a million dollar question that needs to be understood first before we start talking about the solution . One best example I can quote today for you is Portlets.
What are portlets? Wikipedia says, Portlets are pluggable user interface software components that are managed and displayed in a web portal. Portlets produce fragments of markup code that are aggregated into a portal. Typically, following the desktop metaphor, a portal page is displayed as a collection of non-overlapping portlet windows, where each portlet window displays a portlet. Hence a portlet (or collection of portlets) resembles a web-based application that is hosted in a portal. Some examples of portlet applications are email, weather reports, discussion forums, and news.
Consider dashboard environment. The portlets that are loaded in dashboard may be based on ACL data. Lets go by a scenario to understand this problem better.
Scenario: We need to create a dashboard where we have a button in tool bar of dashboard. On click of that we need to reload data of portlets. In that case, what we shall do is this,
Problematic Solution: We may get instance of all the portlets (registered in some kind of Map or Session manager) and then invoke corresponding portlet’s refresh method. For this we may intelligently use the help of Interfaces. But think if prototyping languages like javascript are so strict about that. Answer is NO. Even otherwise, if we deeply think about the programming intelligence, we would end up in restricting the flexibility of Portlets or any module. We need to communicate properly to all the other teams that we would be using so and so methods to refresh. And we need to maintain stack of documents to know which object has which method to refresh.
But, as devolpers should we think like this? again No. We will have to adopt a methodology that totally decouples the portlets or modules in this whole game. But how is that possible?
Optimal Solution: I hope we all are aware about Producer and Consumer problem. Where consumer will be subscribing to an interested topic and producer will be producing messages to that topic. Yes, its a JMS kind of implementation we have to do to decouple the portlets / any system. This strategy can be used widely for any of your systems based on its demands. I used this methodology for alteast 2 of the products I designed so far.
Example of Implementation: For our example I used ExtJS 4.1 based portlets. I picked the portlets example of ExtJS library and then implemented a Message Driven Framework for UI (MDF-UI) and solved the question I put in above block. This implementation totally decoupled the portlets and even my other modules where all my events are published as topics and interested subscriber portlets will respond based on the topic and message.
Note: You need to know few things about ExtJS to understand this example easily
MDF-UI Blocks:
Above is the representation of components used to create MDF-UI in ExtJS.
Logger: is used to printing any messages in to browser console.
Constants: is a directory used to maintain all TOPICS
Manager: is a Lookup Index where we can use it for storing any components object to it. It is not used in this framework directly, but still thought of sharing it. You can use this to register all UI components loaded in the system.
NotifierMap: is extension of Ext.util.MixedCollection where I have implementation for how to use it to easily manage all registered Producers and Subscriber informations.
NotificationManager: is the brain of this framework where we use this to register / de-register producers and subscribers along with the topic.
IPublishedMessage: can be used as Mixin where it is used to fetch messages for specified topic
INotifier: is a Mixin where it is abstract implementation of Notifier.
INotificationLsnr: is a Mixin where it is abstract implementation of Notification Listener.
AbstractMessenger: is a sample implementation of MDA-UI
How it works:
Once the UI / Portlet compoent is loaded the initComponent method will be called Ext. There, I am subscribing the same object by calling NotificationManger via INotificationLsnr mixin with the relevant topic. Like wise, any notifier buttons or components will have INotifier mixin with the topics. So, whenever there is an event, the notifiers will send a message to the NotificationManger which in turn will iteratively call the notified methods of all Subscribers with the message.
Download Source: You can download the sample code here or you can visit here to look at the working demo. Codes are very simple and self explanatory. You can look at them and then post me if you need any clarity on that. To set up your environment, simply download the source code and extract to your htdocs / webapp folder of your server and call http://localhost/works/extjs/mda/index.html. You should see the app in action.
Learning Framework: You should open /works/extjs/mda/ux/messaging folder to see Ext.ux.messaging.* components.
(Warning: If your browser is not loading the app, please try refreshing it once. I am still working to solve the Ext.Loader issues. 😦 )
Feel free to download and use this framework. I would be more than glad to explain this more if you someone is looking for 🙂