File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just how dumb should a UI be.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2068
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic