aspose file tools*
The moose likes Java in General and the fly likes Just how dumb should a UI be. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Just how dumb should a UI be." Watch "Just how dumb should a UI be." New topic
Author

Just how dumb should a UI be.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I know that the UI should be decoupled from all business logic, and that seems to me to indicate that all exception hadling should be outside of the UI, but is this a hard and fast rule? I have a simple TextUI that forwards all calls to a controller. One of my menu options is to load a model from a file.
So I guess question is, it seems like in a situation like this, a FileNotFound exception seems to be something the UI can recover from. But is that assuming too muxh intelligence on the part of the UI. Should my controller even be throwing an Exception here? I'm no MVC expert by any means. Any guidance would be appreciated.
[ July 27, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
I'm no MVC expert by any means


No hard and fast. But your design will not be flexible. Anytime you change business logic, you have to update UI as well.

Even if you are not going for MVC, still I think exception should not be caught in UI.

If you directly calling model from UI component, then your model is highly coupled with UI component. You can break this coupling by using a business delegate. Define a simple java class whose sole purpose is to delegate client's call to business component.

By that, you components will then be loosely coupled. Second you can easily catch exception in business delegate and can forward it some some Error class and finally catch it in that error class. By this way, your design will be more flexible.

Naseem
[ July 27, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The controller can know about the UI as well as the UI knowing about the controller. You can do some tricky bits with interfaces to void circular dependencies between packages, but let's leave that for later. The UI might so something like:

Now the controller can do all the hard work, catch the exception, format a nice message and send it back to the UI.

Does that sound reasonable?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
BTW: To the question of "just how dumb" it can be pretty amazing. Robert Martin's book "Agile Software Development" has a big case study where he builds a fully functional payroll system with no UI. Up in the servlet forum they talk about making testable POJOs instead of putting logic into the UI. In those styles you can write a whole system and the UI is almost a no-brainer on top. I see you're doing a text UI now - which is a pretty neat way to start - but you might wander up to Servlets and ask how they keep the UI dumb.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Often the best answer is redundant validation.

First make sure that your core app or model won't allow values that could cause a problem. This insures that, regardless of the UI (or UIs) being used you have assurances that your data has been validated.

Then, you may want to improve your user's experience (or reduce bandwidth on your model) by duplicating the validation in the UI. Often the UI can take more active steps to limit data entry to correct values with things such as select lists containing only valid values or input fields that don't accept non-numeric characters, etc...

In short, write your back end code with the assumption that the UIs are VERY stupid. Then see if you can make your UI smart enough that the validation on the back end never catches anything.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Thanks for the responses all.


SJ: Now the controller can do all the hard work, catch the exception, format a nice message and send it back to the UI.

Does that sound reasonable?


That sounds very reasonable. When you mention using an interface to avoid circular dependancy, I'm assuming you mean refactor the UI to implement an interface that would include (amid others) the

void displayMessage(String message);

method so that my controller is not completely coupled to this particular UI but to any that implements the required interface. Or were you talking about something even slicker than that.

Stan: I remember some time in the recent past you posted a code snippet for a UI that initialized a number of Listeners in a strategy map that coupled the Listeners with the event source they were mapped to. However I can't find the post. Think you could repost that (or dig up the link to the previous post) ?
[ July 28, 2006: Message edited by: Garrett Rowe ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
When you mention using an interface to avoid circular dependancy, I'm assuming you mean refactor the UI to implement an interface that would include (amid others) the

void displayMessage(String message);

method so that my controller is not completely coupled to this particular UI but to any that implements the required interface. Or were you talking about something even slicker than that.


Yes, that's just what I was after. But I don't really know how much a controller should know about the UI. I think in many designs a screen-controller pair are tightly coupled.

I like Ben's discussion of duplicate validation, too. I often say the model must defend against a sloppy or malicious client, so it must validate and enforce the rules. A UI can duplicate validation and rules to improve the user experience right up to their threshold for pain. Duplication can be painful in many ways, but sometimes it's still the right answer.

Here's a snippet of that other thing. I don't do much Swing and I haven't seen anyone else do this, so I might be crazy. Be sure to survey other styles, too.
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
I am currently doing the business side of a system. Ultimately, it is responsible if an invalid transaction passes through.

So the business side is very important.

As mentioned above, redundant validation can help for speed. For example, javascript can do minor validations.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you have an hour to really concentrate, Martin Fowler has very recently updated an article on GUI Architectures. I bookmarked it for later today.
[ July 30, 2006: Message edited by: Stan James ]
 
Consider Paul's rocket mass heater.
 
subject: Just how dumb should a UI be.