mvc design doubts

Discussion in 'Java' started by harryos, Nov 4, 2010.

  1. harryos

    harryos Guest

    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){
    ...
    }
    ...
    }
     
    harryos, Nov 4, 2010
    #1
    1. Advertising

  2. harryos

    Stefan Ram Guest

    harryos <> 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
     
    Stefan Ram, Nov 4, 2010
    #2
    1. Advertising

  3. harryos

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >harryos <> 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.
     
    Stefan Ram, Nov 4, 2010
    #3
  4. In article
    <>,
    harryos <> 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>
     
    John B. Matthews, Nov 4, 2010
    #4
  5. harryos

    harryos Guest

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

    }
    ...
    }
     
    harryos, Nov 4, 2010
    #5
  6. harryos

    Ian Shef Guest

    harryos <> wrote in news:fc40ea84-a348-4489-b013-
    :

    > 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?
     
    Ian Shef, Nov 4, 2010
    #6
  7. harryos

    harryos Guest

    On Nov 4, 10:31 pm, Ian Shef <> wrote:

    > Typo, perhaps?


    sorry..typo

    thanks for pointing it out
    regards
    harry
     
    harryos, Nov 4, 2010
    #7
  8. harryos

    harryos Guest

    also

    class ErrorHandler implements Observer
     
    harryos, Nov 4, 2010
    #8
  9. harryos

    Lew Guest

    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
     
    Lew, Nov 4, 2010
    #9
  10. harryos

    harryos Guest

    On Nov 5, 2:31 am, Lew <> 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);

    }
    }
    ...

    }
     
    harryos, Nov 5, 2010
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. winglite
    Replies:
    1
    Views:
    426
    Natty Gur
    Nov 3, 2003
  2. =?Utf-8?B?Sm9obg==?=
    Replies:
    1
    Views:
    293
    bruce barker
    Mar 30, 2007
  3. rmn190
    Replies:
    2
    Views:
    2,368
    Arne Vajhøj
    Jan 10, 2008
  4. Paulo
    Replies:
    3
    Views:
    482
    Peter Bromberg [C# MVP]
    Dec 4, 2008
  5. Crazy Cat
    Replies:
    1
    Views:
    691
    schepp
    Sep 3, 2009
Loading...

Share This Page