mvc design doubts
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){
...
}
...
}