1,105,214 Community Members

separate model and gui code

Member Avatar
game06
Junior Poster
109 posts since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

need help with design to separate model and gui code?

les say i have 3 classes.

Main.java
BoxGui.java
BoxModel.java



public classBoxModel
{
    BoxGui bg = new BoxGui();

    public void moveBox(){
        int x = bg.getX();
        bg.setX(x+1);
    }
}

public class BoxGui
{
    int x,y,width,height;
    public void paint(Graphics g){
        g.drawRect(x, y, width, height);
    }
}


public class Main{
    BoxModel bm = new BoxModel();
     BoxGui bg = new BoxGui();
    ...
     public void paintComponent(Graphics g) {
         bm.moveBox();
     }
     ...

      public void paint(Graphics g){
          bg.paint(g);
      }
}

I guess i dont understand which class should have which class object. for ex:
in main do we create BoxModel or BoxGui or both obj?
in BoxModel do we need to create BoxGui obj?
in BoxGui do we need to create BoxModel obj?

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Typically you would have main create the model then create the GUI, passing a ref to the model into the GUI constructor, so the GUI can use the model's accessor methods.

Model m - new Model(...) // model knows nothing about the GUI
GUI g = new GUI(m, ...)  // ... but the GUI has to use the model

This minimises the copupling between the GUI and the model, reducing it to the absolute minimum needed to make it work.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article