I'm writing on my own wizard and find myself duplicating code throughout my view class (using a MVC design pattern).
I have basic methods that create panels that are used by the wizard. Some of the panels share common components but may have different functionality.
My example below is not actual code, but just an example of my layout:
public class View
{
private Panel panel1;
private Panel panel2;
private Panel panel3;
private Panel panel4;
private JButton panel1Button1;
private JButton panel1Button2;
private JButton panel1Button3;
private JButton panel1Button4;
private JButton panel2Button1;
private JButton panel2Button2;
private JButton panel2Button3;
private JButton panel2Button4;
private JButton panel3Button1;
private JButton panel3Button2;
private JButton panel3Button3;
private JButton panel3Button4;
private JButton panel1Button1;
private JButton panel1Button2;
private JButton panel1Button3;
private JButton panel1Button4;
View()
{
panel1 = createPanel1();
panel2 = createPanel2();
panel3 = createPanel3();
panel4 = createPanel4();
}
private JPanel createPanel1()
{
// panel1 code which may have buttons, scroll panes, etc
return panel1;
}
private JPanel createPanel2()
{
// panel2 code which may have buttons, scroll panes, etc
return panel2;
}
private JPanel createPanel3()
{
// panel3 code which may have buttons, scroll panes, etc
return panel3;
}
private JPanel createPanel4()
{
// panel4 code which may have buttons, scroll panes, etc
return panel4;
}
// Getters for all the components, but only some buttons listed only for brevity
private JButton getPanel1Button1() { return panel1Button1; }
private JButton getPanel1Button2() { return panel1Button2; }
private JButton getPanel1Button3() { return panel1Button3; }
private JButton getPanel1Button4() { return panel1Button4; }
//etc
// Getterss for panels
private JPanel getPanel1() { return panel1; }
//etc
}
That is the basic layout of my code, but as you can see just adding one component that may be used in multiple panels creates a lot of duplicate work.
Buttons are easy because I can find them using a method that searches for buttons in a panel using the getText
method, but this doesn't work for other components.
How can I reduce duplicating my code and reduce the amount of references for each panel (i.e. I have cancel, back and next buttons on each panel)? Or must I uniquely create a reference for each component of each panel and supply a getter to adequately set up an MVC?
Aucun commentaire:
Enregistrer un commentaire