Pradit
Pradit

Reputation: 719

How are notifications carried in JAVA?

I was reading about the Event Style architecture and came across this

There are different ways to deliver notifications, but the most common technique uses an indirect method call that is made through a pointer initialized at runtime.

But when Java does not have pointers how does it deliver the notifications ?

Upvotes: 2

Views: 126

Answers (2)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Java Swing uses listeners to deliver notifications about events.

Here's a snippet of code from a Swing class

        Font font = model.getActivityFont();
        activityTextFontButton = new JButton(getFontText(font));
        activityTextFontButton.setFont(font);
        activityTextFontButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                JFontChooser fontChooser = new JFontChooser();
                fontChooser.setSelectedFont(model.getActivityFont());
                int result = fontChooser.showDialog(dialog);
                if (result == JFontChooser.OK_OPTION) {
                    Font font = fontChooser.getSelectedFont();
                    String text = getFontText(font);
                    model.setActivityFont(font);
                    activityTextFontButton.setText(text);
                    activityTextFontButton.setFont(font);
                    JButton dummy = new JButton(text);
                    setButtonSizes(activityTextFontButton, 
                            connectorTextFontButton, dummy);
                    dialog.validate();
                    dialog.pack();
                }
            }
        });

When the button is pressed, the JButton class executes the code that's part of the ActionListener. That's because the JButton class has a reference to the ActionListener instance and knows that the method to execute is actionPerformed.

There are other ways in Java to drive events (see the Observer / Observed classes), but listeners are a pretty cool way to deliver notifications about events.

Upvotes: 1

Brian
Brian

Reputation: 17309

Java's event system is based on the Observer pattern, except in Java Swing, everything is called a "listener" instead of "observer", and typically implements EventListener (as you can tell from the beast at the top of that link).

For example, a component such as a JButton allows you to add listeners to it that care about when something happens with the button. If your listener cares about button clicks, you would add an ActionListener to the button, and JButton adds it to an internal list to be used later (specifically, it uses EventListenerList). When the user clicks the button, the JButton class will "fire" an event to all of the ActionListener implementations that have been registered with it.

This is basically the observer pattern because you have a subject that's being observed (the JButton) and an observer that wants to be notified about the subject's changes or events (the ActionListener).

Java doesn't have pointers, per se, but that doesn't mean that this can't be accomplished. In C++, you would pass a function pointer to the subject, and the subject would just call that function. Since Java doesn't have function pointers, you pass an object that implements a particular interface to the subject, and the subject will call an exact method on the interface. In the example above, this would be the ActionListener.actionPerformed(ActionEvent) method.

Note that this method is also popular among other Java libraries besides Swing because of the absence of mechanisms like function pointers and the prevalence of Java interfaces.

Upvotes: 1

Related Questions