Re: Design Patterns
On 2/3/2013 11:24 AM, markspace wrote:
On 2/2/2013 9:11 PM, Doug Mika wrote:
So passing MainWindow to MenuWindow is one possible solution. But
what are the other three. No, this is not to be a school
assignmennt, I'm looking for a clean and proper way to do this.
Here's two patterns I see in JEE programming a lot.
1. Global context. All of your important objects are put into a global
context object and then are accessible. In JEE this is similar to a
Map: you put in objects identified by strings and retrieve them the same
way.
globalContext.put( "my window", object );
thing = globalContext.get( "my window" );
In a less general framework, I'd divide this into major sections (GUI,
business logic, config, maybe logging, persistence, etc.) and possibly
provide some specialized logic for each.
The global context should passed into objects somehow, similar to the
ctor we discussed earlier.
It is common.
But I would hesitate using it for OP's problem.
2. Factories. You can also just have a factory object, which is
ultimately a static method.
thing = GuiFactory.getWindow( "main window" );
- or -
thing = GuiFactory.getMainWindow();
In larger frameworks it's common for the static method to fetch another
factory, which then does the work of making objects for you. Obviously,
you have to load the main window into the factory at some point.
That is used all the time everywhere.
Personally I think factories are harder to work with, as they making
testing more difficult.
Typical factories are considered more test friendly than new.
Arne