Understand Team Pattern in Swing

I see Swing using the Decorator and Observer pattern.

Observer: each component (for example JButton) is a subject that can add observers(ActionListeners). When someone presses a button, he notifies all of his own ActionListeners, calling them actionPerformed(ActionEvent e).

But what about the Command Pattern?

When I do the classes that implement ActionListener(for example MyActionListener), actionPerformed(ActionEvent e)is now performing team?

It bothers me what is actionPerformed(ActionEvent e)used as a method execute()and update(). I'm here?

+3
source share
4 answers

, . , , , , ActionListener. , invoker- , .

+5

, , , , .

Action Swing. Action ActionListener, , Action, actionPerformed.

, , Action, , . .

, AbstractAction , Action, , . AbstractAction :

class MySpecialAction extends AbstractAction {
    @Override
    public void actionPerformed(ActionEvent e) {
        // Perform operations.
    }
}

MySpecialAction - - , . :

MySpecialAction action = new MySpecialAction("Special Action", mySpecialIcon);

, JButton s, JMenuItem .. MySpecialAction:

JMenuItem specialMenuItem = new JMenuItem(action);

/* ... */

JButton b = new JButton(action);

, , , MySpecialAction . , MySpecialAction , , - , , .

+3

, , , -, , . , Command Command Pattern.

0

:

http://blue-walrus.com/2011/10/swing-and-design-patterns-%E2%80%93-part-3-command-pattern/

The key is that your action is a block of interchangeability. This action can be performed with a few buttons or menus. For example. the Save action may hang after the Save menu option, and also hang behind the Save icon.

0
source

Source: https://habr.com/ru/post/1710077/


All Articles