• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

a delegation event model

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a delegation event model?

�Java specifies a delegation event model that supports the goals of publish-subscribe.�

My guess is, delegation means although the ActionListeners are registered with the JButton, the JButton delegates to the AWT Event Handler which invokes actionPerformed(ActionEvent) on the ActionListener. Without delegation, the JButton would notify the subscribers/observers.

Do you think that is what it means?
[ November 17, 2004: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my confusion.

Model (Publisher)
View (Subscriber)
Controller

JButton (Publisher)
ActionListener (Subscriber)

JPanel (View)
ActionListener (Controller)

Therefore, some subscribers are views and some subscribers are controllers. Some publishers are models and some are not. Is that correct?
[ November 17, 2004: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your examples are correct.

./pope
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ali for checking my thoughts. So it�s reasonable to think of an ActionListener as a Controller in the MVC pattern?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most probably in the case where your ActionListener plays the controller role you will delegate the real job to some other object rather to place a whole lot of business inside the listener.

./pope
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ali Pope. Yes, I was thinking generally of the case where the real work gets forwarded to some business objects.

Since my confusion seems to be disappearing by trying to articulate my questions and by your answers, I am tempted to keep asking questions.

Suppose some object C1 of the presentation layer invokes a method of some object C2 of the application layer. C2 might have the role of controller because of its position on the edge of the application layer. Suppose the presentation layer is a GUI and object C1 is an ActionListener. C1 might have the role of controller because of its relation to some JPanel and purpose in the GUI.

I seem to have two controllers where there should be one. My explanation would be C1 is a controller in the context of the GUI. The whole application layer is the model. C2 is a controller in the context of the application layer. The whole presentation layer is the View. Is that fair?

Regards, Marlene
[ November 18, 2004: Message edited by: Marlene Miller ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I've lost the track of the words . So, I would express it this way:
- all gui controls presenting info to client represent the view
- some of the gui controls provide events to listeners delegating to application functionality. Application functionality is the controller
- the way your organize/keep your data is the model

In many real cases the MVC is not sufficient for describing a complex thin client. In these cases the hierarchical mvc should be used (if you want to find out more just google for HMVC).

./pope

ps: hope this help
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Thank you.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for grins, here's an unusual approach to Swing in MVC. At least I've never seen anyone else do it. There is one listener for most or all events. It delegates each event to a command based on the source of the event. The command has only one line of code to tell the controller the event happened:

Let me know if that fits your view of delegating events or if it's just confusing.
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The formatting is confusing.... :-), but the idea is nice .

./pope
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stan for your example. In your example, is the controller part of
the presentation layer or the domain layer?

(I am getting confused trying to talk to a very experienced C++ programmer
about a design. I want to be sure we mean the same thing when we use the
same words. Controller seems to a problem word.)

Thank you Ali for mentioning HMVC. I found two interesting links.
[ November 18, 2004: Message edited by: Marlene Miller ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Controllers are an independent layer (as their name implies). Stan's solution - which one of the most used - is meant just to take event from the view and delegate them to the correct controller. As the view has knowledge about its corresponding controller(s) this is quite an easy job.

./pope
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan, in your example, aren't you just reinventing the Action interface ?
D.
[ November 19, 2004: Message edited by: Don Kiddick ]
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. In the case you're talking you should have more than one action listeners. In Stan's example there just one .

./pope
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wrongly read . Stan view-controllor relation is just more decoupled from the underlying Swing specifics.

./pope
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to avoid multiple listeners, so I have only one. Sometimes when you see somebody use one listener you'll also see a largish if-else structure testing the widget that generated the event. I wanted to avoid that, too, so I used a map. The little command things are a necessary glue between the event and the controller method. I wanted to keep them as small and dumb as possible. They encapsulate the mapping between event source and controller method and absolutely nuthin else. Well, except that one that closes the window. I donno how he got in there. When I add a new widget to the screen, I add one line (with the wacky formatting) to add a glue command and it runs. In another app I pass the command to a factory method that creates the widget, registers the listener and puts the command in a map all at once:

Good clean fun. Not enough fun to make me want to do more Swing, but that much fun wouldn't be legal.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what's the advantage of

goButton = createButton("Go", new ICommand ...);

over

goButton = createButton("Go", new ActionListener ...);

???
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
imo the solution offers possibly reusable commands.

./pope
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ali Pope:
[QB]imo the solution offers possibly reusable commands.



Mhh, I currently can't think of an ActionListener that I'd want to reuse as a non-ActionListener. Most often they actually just delegate to the enclosing instance. I will keep my eyes open for opportunities of this kind of reuse in the future, though.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ilja, I'm not sure there is any advantage to the commands over listeners. I think I had a reason for only wanting one listener - a place to manage some darned thing - but probably could have met the needs by having one abstract listener. Maybe I was just command crazy that week. I did use some of the commands more than once in another app - hooked up to buttons and menu items for example, but again nothing one couldn't do with a listener. I don't do enough Swing to pretend to be good at it, but those things still run, which is about all I ask of em most days.
 
Liar, liar, pants on fire! refreshing plug:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic