Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > mvc design doubts

Reply
Thread Tools

mvc design doubts

 
 
harryos
Guest
Posts: n/a
 
      11-04-2010
hi,
I made a gui app based on mvc pattern.I came out as below.Here ,I am
using a SimpleExceptionHandler to handle any exceptions occurring in
the model.This should show the user some appropriate error messages if
any error occurs during processing.In order to do this ,I had to make
the SimpleExceptionHandler become aware of the view and then set the
exceptionhandler in the model.
Here ,I am beginning to doubt if this is the correct way.Am I mixing
up the mvc pattern by letting exception handler beome aware of the
view .Setting the exception handler inside the model too looks
suspicious..
I don't see how else I can let the user know about errors occurred
during processing (inside the model).I considered letting exceptions
bubble to the top level method and returning message from exception to
the user.But that too looks ugly.
If someone can suggest an alternative, it would be great.I am posting
code snippets below.Please let me know what you think.

class Controller{
private BasicView view;
private BasicModel model;
private SimpleUIValidator sValidator;
private StringBuilder errorMsg;
public Controller(BasicView v,BasicModel m){
view=v;
view.addOKButtonListener(new SimpleButtonListener());
model=m;
exhandler=new SimpleExceptionHandler(view);/*a handler that
can show error messages on GUI back to the user*/
model.setExceptionHandler(exhandler);/*set this handler in the model
so any errors in processing inside the model will be handled.*/
sValidator=new SimpleUIValidator();
}
....
private void handleUserInputs(String userInput1,File userInput2){
if(sValidator.validate(userInput1,userInput2)){
Result result=model.processInputs(userInput1,userInput2);

}else{
//tell the user about wrong inputs
view.displayMessage(errorMsg.toString());
}
if (result!=null){
displayMessage(result.getMessage());
//also show other info from Result to user
...
}

}


//inner class ButtonListener
class SimpleButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
...
String userInput1=view.getUserInput1();
File userInput2=view.getUserInput2();
handleUserInputs(userInput1,userInput2);
}
}//end inner class ButtonListener

//inner class SimpleUIValidator
class SimpleUIValidator{
public boolean validate(String input1,File input2){
errorMsg=new StringBuilder();
boolean input1Valid=validateTextFieldInput(input1);
boolean input2Valid=validateFileSelectionInput(input2);
return input1Valid && input2Valid;

}
private boolean validateTextFieldInput(String textFieldInput){
boolean isValid=false;
try{
Double.parseDouble(textFieldInput);
isValid=true;
}catch(NumberFormatException e){
errorMsg.append("enter a decimal number");
}return isValid;
}
private boolean validateFileSelection(File file){
//validate if this is an image file
//if not ,append error message to errorMsg
return isValid
}

}//end inner class SimpleUIValidator
}


class BasicView extends JFrame {
...

}

class BasicModel{
private ExceptionHandler exhandler;
public void setExceptionHandler(ExceptionHandler h){
exhandler=h;
}
public Result processInputs(String decimalInput,File selectedFile)
{
Result result;
doSomeWork(selectedFile,decimalInput);
result=makeResult();
return result;
}
public void doSomeWork(selectedFile,decimalInput){
try{
//process the data
...
}catch(SomeException e){
exhandler.handle(e,"some specific message");
}
}

}

public interface ExceptionHandler {
public void handle(Exception e, String errorMessage);
}

class SimpleExceptionHandler implements ExceptionHandler{
private BasicView view;
private Logger somelogger;
public SimpleExceptionHandler(BasicView v){
view=v;
somelogger=getSomeLogger();
...
}
public void handle(Exception e,String msg){
view.displayMessage(msg+e.getMessage());
somelogger.writetoLog(msg+e.getMessage());
}
}

class Result{
private boolean processingSuccess;
private double someValue;
private String message;
public Result(boolean processingSuccess,double someValue,String
someMessage){
...
}
...
}
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      11-04-2010
harryos <(E-Mail Removed)> writes:
>I don't see how else I can let the user know about errors occurred
>during processing (inside the model).


The model broadcasts a message to all registered observers.

http://en.wikipedia.org/wiki/Observer_pattern

»If your writing is semi-literate, ungrammatical, and
riddled with misspellings, many hackers (including
myself) will tend to ignore you. While sloppy writing
does not invariably mean sloppy thinking, we've
generally found the correlation to be strong -- and we
have no use for sloppy thinkers. If you can't yet write
competently, learn to.«

Eric Raymond

http://www.catb.org/~esr/faqs/hacker-howto.html#skills4

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      11-04-2010
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
>harryos <(E-Mail Removed)> writes:
>>I don't see how else I can let the user know about errors occurred
>>during processing (inside the model).

>The model broadcasts a message to all registered observers.


Another possibility:

The messages that the model processes all were sent by the
controller. So, when an exception occurs during such a
process, the model can just let it rise to the controller
(that is, the model does not catch any exception and just
declares methods as »throws«, or, it only catches to convert
exceptions and then re-throws. It also might report errors
by return values).

The controller then knows what went wrong and then can react
in any way it wants, because the controller knows both the
model and the view. So it could, for example, display an
error message using the view, or terminate the whole process
in the case of a »fatal« error.

 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      11-04-2010
In article
<(E-Mail Removed)>,
harryos <(E-Mail Removed)> wrote:

> I am posting code snippets below. Please let me know what you think.


I'd reiterate Stefan Ram's suggestion to use the Observer pattern:

<http://en.wikipedia.org/wiki/Observer_pattern>

Here's a compete example using java.util.Observable and
java.util.Observer:

<http://stackoverflow.com/questions/3072979>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
harryos
Guest
Posts: n/a
 
      11-04-2010
thanks a lot for the suggestions..
I modified the classes accordingly.

class BasicModel implements Observable{
public Result processInputs(String decimalInput,File selectedFile)
{
Result result;
doSomeWork(selectedFile,decimalInput);
result=makeResult();
return result;
}
public void doSomeWork(selectedFile,decimalInput){
try{
//process the data
...
}catch(SomeException e){
String response="specific message"+e.getMessage();
setChanged();
notifyObservers();

}
}
...
}

class ErrorHandler implements Observable{
private BasicView view;
public ErrorHandler(BasicView v){
view=v;
}
public void update (Observable obj, Object arg) {
if (arg instanceof String) {
resp = (String) arg;
view.displayMessage(resp);
}
}
...
}

class Controller{
private BasicView view;
private BasicModel model;
private SimpleUIValidator sValidator;
private StringBuilder errorMsg;
private ErrorHandler errorhandler;
public Controller(BasicView v,BasicModel m){
view=v;
view.addOKButtonListener(new SimpleButtonListener());
model=m;
errorhandler=ErrorHandler(view);
model.addObserver(errorhandler);

}
...
}
 
Reply With Quote
 
Ian Shef
Guest
Posts: n/a
 
      11-04-2010
harryos <(E-Mail Removed)> wrote in news:fc40ea84-a348-4489-b013-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> thanks a lot for the suggestions..
> I modified the classes accordingly.
>
> class BasicModel implements Observable{


<snip>

Are you talking about some different Observable ?

The Observable that I know is java.util.Observable and is a concrete class,
not an interface.

You can extend java.util.Observable, but you can't implement it.
Typo, perhaps?


 
Reply With Quote
 
harryos
Guest
Posts: n/a
 
      11-04-2010
On Nov 4, 10:31*pm, Ian Shef <(E-Mail Removed)> wrote:

> Typo, perhaps?


sorry..typo

thanks for pointing it out
regards
harry
 
Reply With Quote
 
harryos
Guest
Posts: n/a
 
      11-04-2010
also

class ErrorHandler implements Observer

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-04-2010

Ian Shef wrote:
>> Are you talking about some different Observable ?
>>
>> The Observable that I know is java.util.Observable and is a concrete class,
>> not an interface.
>>
>> You can extend java.util.Observable, but you can't implement it.
>> Typo, perhaps?


harryos wrote:
> sorry..typo
>
> thanks for pointing it out
>


Had you tried compiling that code?

--
Lew

 
Reply With Quote
 
harryos
Guest
Posts: n/a
 
      11-05-2010
On Nov 5, 2:31*am, Lew <(E-Mail Removed)> wrote:

> Had you tried compiling that code?


sorry..there were some more copy paste mistakes..


class BasicModel extends Observable{
public Result processInputs(String decimalInput,File selectedFile)
{
Result result;
doSomeWork(selectedFile,decimalInput);
result=makeResult();
return result;
}
public void doSomeWork(selectedFile,decimalInput){
try{
//process the data
...
}catch(SomeException e){
String response="specific message"+e.getMessage();
setChanged();
notifyObservers(response);

}
}
...

}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No MVC Project Template after installing ASP.NET MVC 1.0 Crazy Cat ASP .Net 1 09-03-2009 08:02 PM
WebForms X MVC? Why MVC? Give me reasons to migrate my web apps to it please. Pros x Cons! Thanks! Paulo ASP .Net 3 12-04-2008 03:00 AM
differences between Spring WebFlow,Spring MVC,and String Portlet MVC? rmn190 Java 2 01-10-2008 02:27 AM
Doubts in Design Level. Desktop -> Web-Based conveting using .NET =?Utf-8?B?Sm9obg==?= ASP .Net 1 03-30-2007 03:43 PM
Any gd example for MVC Design in ASP.NET ? winglite ASP .Net 1 11-03-2003 05:03 AM



Advertisments