mvc design doubts

H

harryos

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){
...
}
...
}
 
S

Stefan Ram

harryos said:
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
 
S

Stefan Ram

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.
 
H

harryos

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);

}
...
}
 
I

Ian Shef

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?
 
H

harryos

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);

}
}
...

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top