Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Design Patterns

Reply
Thread Tools

Design Patterns

 
 
dougmmika@gmail.com
Guest
Posts: n/a
 
      02-03-2013
Hi to all

I'm writing an application and I have the following problem. When I write EventListeners I write them in seperate classes (and seperate files) for reasons of clearity. But these event listeners I want to modify other classes that aren't defined in the EventListeners. They already exist in the main program. The problem I have is how best to get these eventlisteners to know of these classes (which are also in seperate files)? Up to know I have used Singletons (I made the classes the event listeners were to modify global) but this seems ugly. How are java applications written to let the eventlisteners see the classes they are to modify (sometimes these classes are seperate application components in seperate files)?

I hope this makes sense
Doug
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      02-03-2013
On 2/2/2013 7:17 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm writing an application and I have the following problem. When I
> write EventListeners I write them in seperate classes (and seperate
> files) for reasons of clearity. But these event listeners I want to
> modify other classes that aren't defined in the EventListeners.
> They already exist in the main program. The problem I have is how
> best to get these eventlisteners to know of these classes (which are
> also in seperate files)? Up to know I have used Singletons (I made
> the classes the event listeners were to modify global) but this seems
> ugly. How are java applications written to let the eventlisteners
> see the classes they are to modify (sometimes these classes are
> seperate application components in seperate files)?


If you don't use the nested anonymous classes, then you will need
to send the refs you need over in the constructor.

Do not use singletons for this.

Arne


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      02-03-2013
(E-Mail Removed) writes:
>When I write EventListeners I write them in seperate classes
>(and seperate files) for reasons of clearity.


This expresses the underlying assumption that writing them
in separate classes and files is more clear. I am not so
sure about this!

>But these event listeners I want to modify other classes that
>aren't defined in the EventListeners.


In Java SE, you usually do not modify classes at runtime.

>The problem I have is how best to get these eventlisteners to
>know of these classes (which are also in seperate files)?


Well, by »class«, I assume, you mean reference values
(vulgo: objects).

Usually, this results from the OOD, for example:

- you can give static or non-static idenfiers or getter
methods public visibility (which you do not seem to like)

- you can pass the references when creating the instances
(as arguments of the instance creating expressions)

- you can insert them later via setters or
dependency injection

- or you an write the event listeners as inner
classes of the classes they need to access
(which you also do not seem to like, but which
I like)

You might look up Swing code from Oracles Java examples
and tutorials and see how those things are solved there.

You might read the book Applying UML and Patterns by Craig
Larman.

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      02-03-2013
On 2/2/2013 7:42 PM, Stefan Ram wrote:
> You might look up Swing code from Oracles Java examples
> and tutorials and see how those things are solved there.


If it is good enough for Oravle, then it should be good enough
for OP.

> You might read the book Applying UML and Patterns by Craig
> Larman.


That is a very good book.

But much broader than this discussion.

Arne


 
Reply With Quote
 
Doug Mika
Guest
Posts: n/a
 
      02-03-2013
What I mean is the following:
I have a file MainWindow.java class
I have a file MenuBar.java class which instantiates a MenuWindow.java
MenuWindow.java is to make changes to MainWindow.java object.

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?

I know this is explained in a lacklustre way, but I hope it gets the point across. Any free resources on the web would be appreciated.


On Saturday, February 2, 2013 6:42:21 PM UTC-6, Stefan Ram wrote:
> (E-Mail Removed) writes:
>
> >When I write EventListeners I write them in seperate classes

>
> >(and seperate files) for reasons of clearity.

>
>
>
> This expresses the underlying assumption that writing them
>
> in separate classes and files is more clear. I am not so
>
> sure about this!
>
>
>
> >But these event listeners I want to modify other classes that

>
> >aren't defined in the EventListeners.

>
>
>
> In Java SE, you usually do not modify classes at runtime.
>
>
>
> >The problem I have is how best to get these eventlisteners to

>
> >know of these classes (which are also in seperate files)?

>
>
>
> Well, by »class«, I assume, you mean reference values
>
> (vulgo: objects).
>
>
>
> Usually, this results from the OOD, for example:
>
>
>
> - you can give static or non-static idenfiers or getter
>
> methods public visibility (which you do not seem to like)
>
>
>
> - you can pass the references when creating the instances
>
> (as arguments of the instance creating expressions)
>
>
>
> - you can insert them later via setters or
>
> dependency injection
>
>
>
> - or you an write the event listeners as inner
>
> classes of the classes they need to access
>
> (which you also do not seem to like, but which
>
> I like)
>
>
>
> You might look up Swing code from Oracles Java examples
>
> and tutorials and see how those things are solved there.
>
>
>
> You might read the book Applying UML and Patterns by Craig
>
> Larman.


 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      02-03-2013
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.

 
Reply With Quote
 
Doug Mika
Guest
Posts: n/a
 
      02-03-2013
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.

On Saturday, February 2, 2013 10:54:07 PM UTC-6, markspace wrote:
> 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.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-03-2013
Doug Mika wrote:
> I know this is explained in a lacklustre way, but I hope it gets the point
> across.


Not really.

> Any free resources on the web would be appreciated.


http://sscce.org/

Please do not top-post.
> Stefan Ram wrote:

....

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-03-2013
On Sat, 2 Feb 2013 16:17:09 -0800 (PST), (E-Mail Removed) wrote,
quoted or indirectly quoted someone who said :

>How are java applications written to let the ev=
>entlisteners see the classes they are to modify (sometimes these classes ar=
>e seperate application components in seperate files)?


I can think of three ways to link to a class.

pass an X.class parameter

call a X.somestatic method

pass a reference to an X object and call a X.someinstance method

I mean "pass" in the more general sense of pass parm or leave ref in
package scope var.

Event handlers have no extra properties. See
http://mindrod.com/jgloss/anonymousclasses.html
--
Roedy Green Canadian Mind Products http://mindprod.com
The first 90% of the code accounts for the first 90% of the development time.
The remaining 10% of the code accounts for the other 90% of the development
time.
~ Tom Cargill Ninety-ninety Law
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      02-03-2013
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.

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.

Personally I think factories are harder to work with, as they making
testing more difficult.


In a modern app, you also have to consider synchronization, especially
if your app has special threading requirements, like Swing GUI code.
This applies to both the factory and the global context.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Has anybody here read Design Patterns Explained: A New Perspective on Object-Oriented Design John Java 0 06-01-2007 02:45 PM
Design Pattern Relationship Diagram - Design Patterns - Gang of Four Tim Smith C++ 2 12-15-2004 05:22 PM
ebook on Software Design Patterns (C#.NET) Siz ASP .Net 1 12-12-2004 09:32 PM
New open source software to use Design Patterns Josh28 ASP .Net 0 11-29-2004 10:54 AM
where to find good patterns and sources of patterns (was Re: singletons) crichmon C++ 4 07-07-2004 10:02 PM



Advertisments