Results 1 to 13 of 13
  1. #1
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default MVC design questions

    Hi i don't know if this ok ? but i have some general design/architecture questions?

    i am trying to separate out my architecture with what i understand to be mvc..

    view - gui - buttons, frames,combo boxes,jtables etc

    model - dataTypes, setters getters, adding things to library's lists etc

    control - when something is selected on the gui do something...

    generally i have been designing classes like

    Java Code:
    EditGuiListener implements ActionListener{
        private boolean currentAction = false;
       private DefaultComboBoxModel combo_1_Model= new DefaultComboBoxModel();
    
        public void actionPerformed(ActionEvent e) {
    	if(e.getActionCommand().equalsIgnoreCase("combo_1")&&currentAction==false)
    		{
    			
    			System.out.println(" addItem group");
    			currentAction=true;
    			processCombo_1_Event(e);
    			currentAction=false;
    			
    		}
    
    
           public void processCombo_1_Event(e)
           {
                 // do something to the   combo_1_Model
                      
    
            }
    
    
    }
    
    EditGui extends JFrame{
    
        EditGuiListener control ;
    
        public EditGui(ActionListener control)
        {
            super();
            this.control = control();
            getContentPane.addAJPanel();
            pack();
            setVisible(true);
         }
    
        public JPanel addAJPanel()
        {
            JComboBox combo_1 = new JComboBox();
            combo_1.addActionListener(control);
    
        }
    
    
    
        }
    now when i link the gui to the model/control

    i go

    1) set up model/control

    EditGuiListener editGuiListener = new EditItemEditGuiListener();

    2) set up gui and control actionListener to gui elements
    EditGui gui = new EditGui(EditGuiListener );


    Is this generally the idea or is there something that i am missing something,
    because i kind of mix the model and control elements into 1 class ?

    If i add to comboBoxes to the gui, then any time a combo box is fired it registers as an event on all comboBox's ? so i added the boolean currentAction to make sure the right processing code was being active ?

    thanks

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I thinks this follows the MVC pretty nicely. One tip, if you're executing actions that take a while to complete, consider threading. If you fire off an action, assign a thread to do it, this way, your GUI won't freeze while the acion is executing.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    thanks for the comments and tips :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default

    The problem I have with this is the controller is wired directly to the views components.

    My preference is to have the ActionListeners being anonymous inner classes, and the Controller being a listener on the view as a whole. The view then calls the relevant method on the controller (usually something along the lines of handleSomethingSomething()), but only for those actions which require data to flow somewhere, and not simply a slight change of view.

    This means the controller does not need to know the specific names of buttons, or fields, and can simply deal with the work involved.

    And the GUIController would be an interface, which is then implemented by a concrete class. So all you do is list the things the controller should work on in the interface.

  5. #5
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    thanks for the response :)

    hmmm .....processing.. do you mean

    Jbutton exitButton = new JButton("exit");
    button.addACtionListener(new ActionListener(
    {
    // processTheaction
    // saveJTableTo File ?

    });

    or
    button.addACtionListener(new ActionListener(
    {
    // processTheaction
    conrol.doSomethingSomething();

    });

    or is this completely on the wrong track ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default

    The latter.
    Unless it's something internal to the view, that is no extra data is required, no data is being saved.

    You might, for example, simply change the colour of some bit of the display (I don't know, just making stuff up at the moment), which does not need to be passed onto anything else.

    But "business logic", if you like, should be passed onto the controller. Like saveData(), or loadData(), findStuff()....or even calculateSomething().

  7. #7
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    thanks :)

    that's kinda what i'm doing like hitting savebutton launches an anonomous class actionListener.. launching a Filechooser... nothing complex

    and more complex stuff like

    - access a database
    - update rmi clients / client gui's

    is in the control .. as above

    been working on an app with alot of gui pages or "tabs" each tab with a jTable|| multiple tables and each tab with multiple JPanels insert/delete/edit functionality..its rmi with database...

    just trying to get the "architecture" ? as clean as possible*, i have basically designed it
    so there is a control object for "significant Pane" in the gui.. it seemed the only sane way to go as there is a fair bit of functionality ( ie hit a name combo and surname combo updates, address fields update etc... ,db conflict checks etx...)

    seems like its roughly the idea

    thanks again :)

    * see having to look at the code again in 6 months or in a couple of years time..

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default

    Put simply, you should be able to run almost every business logic bit by itself without any requirement for Swing imports.

    That way you can check that logic independently of any front end you choose.

    Similarly your back end (the db interaction) should also be independent of much of your business logic.

    You might find the immediate view controllers need to have a db controller sitting "above" them.

  9. #9
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    that's pretty much what's happening ....

    the database stuff is in its own package/classes and
    the business logic call db methods to get the info that it needs

    DataBaseHandler dbHandler = new DataBaseHandler();


    public AddProductListener(dbHandler )
    {
    this.dbHandler = dbHandler;
    }


    String [] getProductsByGroup(String group)
    {
    String query = "select * from products where group = " + group;
    return dbHandler.getStringArrayFromQuery(query);
    }

    not sure what you mean by db controller sitting "above" them ?

    thanks

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default

    Well, in a more complex setup you would often have a business layer that simply did business stuff, without any SQL in it. This would get its data from a database layer, often a DAO (Data Access Object), so isolating the business layer from the exact setup that the data is stored.

    This allows you to write the business stuff without a care whether the data is/will be stored in a text file, a database, behind a web service, or whatever.

    So that SQL above would actually be inside the db layer, not the controller/business layer. Of course in smaller systems that level of abstraction might be overkill.

    So "above" in terms of the Dao being almost a listener for the db...though possibly not te best use of terms there.
    :)

  11. #11
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Thanks again for the response very helpful :)

    a lil confused about implementation...

    at the moment my databaseHandler class returns for example
    resultSets and defaultTableModels

    so if i have a Jtable in my view

    Java Code:
    public MyView extends JFrame{
    
        private JTable mainJTable ;
    
        public MyView(DefaultTableModel controlModel)
         {
                mainJTable = new JTable(controlModel));
    
          }
    
    }
    and i have a ControlClass

    Java Code:
    public class JTableControl {
    
       private DefaultTableModel tableModel;
    
       public JTableControl()
       {
              initialiseTableModel();
       }
    
    
       public void initialiseTableModel()
       {
          
              tableModel  = dataBaseHandler.getDefaultTableModelFromQuery("select * from products");
    
        }
     
    
    }
    does this mean that i would have some sort of additional layer ie ProductServer ..


    Java Code:
    public class ProductServer{
    
          DataBaseHandler db =  new DataBaseHandler();
    
         public ProductServer()
         {
               
    
          }
    
         public DefaultTableModel getAllProductsTable()
         {
                return db.getDefaultTableModelFromQuery("select * from products";
         
         }
     
    }
    // and then the control class JTableControl would call something like

    Java Code:
     public void initialiseTableModel()
       {
          
              tableModel  =  getAllProductsTable();
        }
    or does it mean that my DAO ( what i call my databasehndler ) should return
    dataTypes like string arrays and / vectors / hash maps etc..

    still a little bit fuzzy ?

    thanks

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,809
    Rep Power
    19

    Default

    Think in terms of objects.
    So your query (unless it's really basic, returning a single column) should result in a List<SomeClass> back. Don't return ResultSets. Don't return table models.

    If you return a table model from your db layer then that essentially says "this works with Swing and only Swing". Which is not realy the idea. You should be able to take the db layer as a whole and plug it into a different front end (well, set of controllers) without any change needed.

  13. #13
    simo_mon is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Brilliant :)

    that's the part i wasn't seeing :)

    thanks for the discussion, very helpful :)

Similar Threads

  1. help: I want to design 2D map?
    By mofaker in forum AWT / Swing
    Replies: 4
    Last Post: 10-29-2009, 12:34 AM
  2. JSP Design.
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 04-20-2009, 01:21 PM
  3. Just a Few Questions
    By pringle in forum New To Java
    Replies: 21
    Last Post: 01-09-2008, 06:21 PM
  4. A few questions about Java and design
    By ldb88 in forum New To Java
    Replies: 4
    Last Post: 12-07-2007, 12:51 AM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •