Re: Design Patterns
On 2/2/2013 7:43 PM, Doug Mika wrote:
MainWindow.java and MenuBar.java are instantiated in Program.java
How can events in MenuWindow.java cause changes in MainWindow.java
ie. what's the cleanest way to do this?
The thing is, this is called "programming." There's a very large number
of ways to do this, and they all are valid, given various design
assumptions.
Static variables, "globals" as I think you mentioned, are OK if the
program is small and not going to be maintained (i.e., a small school
assignment). More robust methods all make some assumption as to rates
of code change, presence of frameworks, team size, total costs of
ownership, etc.
Your immediate problem could be solved by using constructors, and just
hiding and showing various windows, rather than destroying them and
re-creating them.
public static void main( String... args ) {
// startup
...
createAndShowGui();
...
}
private void createAndShowGui() {
SwingUtilities.invokeLater( new Runnable() {
MainWindow mw = new MainWindow();
MenuBar mb = new MenuBar();
MenuWindow menuw = new MenuWindow( mw );
mw.pack();
mw.setVisible( true );
}; );
}
Now MenuWindow has a reference to mw so it can manipulate mw. But I
can't think of at least three other ways to do this, and they all have
advantages and short-comings, so you have to decide what is best for
your programs.