Command Pattern Usefulness when using JComponents

我的未来我决定 提交于 2019-12-29 07:06:13

问题


So, I'm developing a program using the Swing library and I obviously have buttons and menu items. Some of these are supposed to do the same stuff, and I thought using the Command Pattern should be the way to do it, e.g. I have a "save" button and a "save" menu item and they have to implement the same saving algorithm. Command Pattern seems to be ok but I can't get who's the receiver in all that. Isn't a comand supposed to work on an object which implements some sort of "receiver interface", so that you can use different commands on different receivers coupling them aribtrarily? It looks like there's no "receiver" in my implementation of the pattern. Another doubt i have is should a command be implemented as a singleton, since you could potentially call its functions from differents parts of the same project, and it would be handly to instantiate it only once and make it statically invokable?

Thank you.


回答1:


" I obviously have buttons and menu items. Some of these are supposed to do the same stuff,"

As @nIcEcOw noted, that's what Actions are for. This Answer Shows exactly this.

As stated in the How to use Actions :

An Action can be used to separate functionality and state from a component. For example, if you have two or more components that perform the same function, consider using an Action object to implement the function. An Action object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields. The state that an action can handle includes text, icon, mnemonic, enabled, and selected status.

An There only three Actions. Ont to open, save, and new. Each Action has an ActionCommand, and icon, and and action to perform. Both the JMenuItem and JToolBar button share the same Action and do the same thing. Here is the code you can run.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class ActionTest {

    public ActionTest() {
        ImageIcon openIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/open.gif"));
        ImageIcon saveIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/save.gif"));
        ImageIcon newIcon = new ImageIcon(
                ActionTest.class.getResource("/resources/image/new.gif"));

        Action openAction = new AbstractAction("Open", openIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Open File");
            }
        };
        Action saveAction = new AbstractAction("Save", saveIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Save File");
            }
        };
        Action newAction = new AbstractAction("New", newIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("New File");
            }
        };

        JMenuItem openMenuItem = new JMenuItem(openAction);
        JMenuItem saveMenuItem = new JMenuItem(saveAction);
        JMenuItem newMenuItem = new JMenuItem(newAction);

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(newMenuItem);
        menuBar.add(fileMenu);

        JToolBar toolBar = new JToolBar();
        toolBar.add(Box.createHorizontalGlue());
        toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        toolBar.add(newAction);
        toolBar.add(openAction);
        toolBar.add(saveAction);

        JFrame frame = new JFrame("Toolbar and Menu Test");
        frame.setJMenuBar(menuBar);
        frame.add(toolBar, BorderLayout.PAGE_START);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ActionTest();
            }
        });
    }
}

As stated in the quote from the above mentioned tutorial, you can do more than just add an image and an action command to the Action. You can use it to set mnemonics and accelorators. Here is a custom Action class that takes

  1. An action command String
  2. an icon
  3. a description for tooltips
  4. a mnemonic
  5. and a key accelorator.

    private class MyAction extends AbstractAction {
    
        String name;
    
        public MyAction(String name, Icon icon) {
            super(name, icon);
            this.name = name;
        }
    
        public MyAction(String name, Icon icon, String desc,
                        Integer mnemonic, KeyStroke accelorator) {
            super(name, icon);
            putValue(Action.SHORT_DESCRIPTION, desc);
            putValue(Action.MNEMONIC_KEY, mnemonic);
            putValue(Action.ACCELERATOR_KEY, accelorator);
            this.name = name;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            switch (name) {
                case "Open":
                    System.out.println("Open");
                    break;
                case "New":
                    System.out.println("New");
                    break;
                case "Save":
                    System.out.println("Save");
                    break;
            }
        }
    }
    

Here's an instantiation of this Action

Action newAction = new MyAction("New", newIcon,
            "Creates a new file",
            new Integer(KeyEvent.VK_N),
            KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));

And here's the new result. You will see the actionCommand in the menu, along with the key mnemonics and accelerators, tooltips, and you will see the jtoolbar buttons share the same traits. You will also see in the final code, that never once once a component created. All you do is add the Action to the JToolBar and the JMenu and let them work their magic.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ActionInterfaceDemo extends JFrame {

    public ActionInterfaceDemo() {
        ImageIcon openIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/open.gif"));
        ImageIcon saveIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/save.gif"));
        ImageIcon newIcon = new ImageIcon(ActionInterfaceDemo.class.getResource("/resources/image/new.gif"));

        Action openAction = new MyAction("Open", openIcon,
                "Opens a file",
                new Integer(KeyEvent.VK_O),
                KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        Action saveAction = new MyAction("Save", saveIcon,
                "Saves a file",
                new Integer(KeyEvent.VK_S),
                KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        Action newAction = new MyAction("New", newIcon,
                "Creates a new file",
                new Integer(KeyEvent.VK_N),
                KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        setJMenuBar(menuBar);
        menuBar.add(fileMenu);

        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.add(saveAction);


        JToolBar toolBar = new JToolBar("Alignment");
        toolBar.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        toolBar.add(Box.createHorizontalGlue());
        toolBar.add(newAction);
        toolBar.add(openAction);
        toolBar.add(saveAction);

        add(toolBar, BorderLayout.PAGE_START);
        add(new JScrollPane(new TextArea(10, 40)), BorderLayout.CENTER);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Action Interface Demo");
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    private class MyAction extends AbstractAction {

        String name;

        public MyAction(String name, Icon icon) {
            super(name, icon);
            this.name = name;
        }

        public MyAction(String name, Icon icon, String desc,
                Integer mnemonic, KeyStroke accelorator) {
            super(name, icon);
            putValue(Action.SHORT_DESCRIPTION, desc);
            putValue(Action.MNEMONIC_KEY, mnemonic);
            putValue(Action.ACCELERATOR_KEY, accelorator);
            this.name = name;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            switch (name) {
                case "Open":
                    System.out.println("Open");
                    break;
                case "New":
                    System.out.println("New");
                    break;
                case "Save":
                    System.out.println("Save");
                    break;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ActionInterfaceDemo();
            }
        });
    }
}

UPDATE

The better explain the relationship of Action and Command Patterns

As noted in Command Pattern

The command pattern is a commonly used pattern which encapsulates a method call or action-like code into a single class. The advantages of being able to package a method (or methods) into a class become evident when you have multiple invokers for a single action (for example a button and a menu item may perform the same action).

In Swing and Borland Delphi programming, an Action is a command object. In addition to the ability to perform the desired command, an Action may have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only the Action object.

So basically Swing uses the concept of the command pattern through the use of Actions

As for OP's question

"Command Pattern seems to be ok but I can't get who's the receiver in all that."

As for the receiver, the wiki uses a text editor as an example and defines the receiver as such

Receiver, Target Object: the object that is about to be copied, pasted, moved, etc. The receiver object owns the method that is called by the command's execute method. The receiver is typically also the target object. For example, if the receiver object is a cursor and the method is called moveUp, then one would expect that the cursor is the target of the moveUp action. On the other hand, if the code is defined by the command object itself, the target object will be a different object entirely.

The main more components of a Command Pattern are stated as follows

Four terms always associated with the command pattern are command, receiver, invoker and client.

Client, Source, Invoker: the button, toolbar button, or menu item clicked, the shortcut key pressed by the user.

So to put it all together:

  1. The MenuItem (client) invokes it
  2. Action (command object) which calls it actionPerformed which in turn
  3. Invokes an method on the receiver.

The wiki article is good read with a Java example




回答2:


When two or more components are mean to do exactly the same thingy, one should look at Action, which reduces the duplicate code.

Small example for further help :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ActionExample {

    private JFrame frame;
    private JButton button;
    private JMenuItem exitItem;
    private Action commonActions;

    private class CommonActions extends AbstractAction {

        public CommonActions(String title, String desc) {
            super(title);
            putValue(SHORT_DESCRIPTION, desc);
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            JOptionPane.showMessageDialog(frame,
                "Closing Frame", "Information", JOptionPane.INFORMATION_MESSAGE);
            frame.dispose();
        }
    };

    private void displayGUI() {
        frame = new JFrame("Action Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);                

        commonActions = new CommonActions("Exit", "To Exit the Application");

        JPanel contentPane = new JPanel();
        button = new JButton();
        button.setAction(commonActions);
        contentPane.add(button);

        frame.setJMenuBar(getMenuBar());
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JMenuBar getMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");

        exitItem = new JMenuItem(commonActions);
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);

        return menuBar;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new ActionExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

ADDED an example with SINGLETON PATTERN (though I am not sure of this approach(about how good this approach is))

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ActionExample {

    private JFrame frame;
    private JButton button;
    private JMenuItem exitItem;

    private void displayGUI() {
        frame = new JFrame("Action Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        CommonActions.setValues(frame);

        JPanel contentPane = new JPanel();
        button = new JButton();
        button.setAction(CommonActions.getInstance());
        contentPane.add(button);

        frame.setJMenuBar(getMenuBar());
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JMenuBar getMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");

        exitItem = new JMenuItem(CommonActions.getInstance());
        fileMenu.add(exitItem);
        menuBar.add(fileMenu);

        return menuBar;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new ActionExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class CommonActions extends AbstractAction {

    private static CommonActions commonActions = null;
    private static JFrame frame = null;

    static {
        try {
            commonActions = new CommonActions("Exit", "To Exit the Application");
        } catch (Exception e) {
            throw new RuntimeException("BINGO, an error");
        }
    }

    private CommonActions(String title, String desc) {
        super(title);
        putValue(SHORT_DESCRIPTION, desc);
    }

    public static CommonActions getInstance() {
        return commonActions;
    }

    public static void setValues(JFrame f) {
        frame = f;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        JOptionPane.showMessageDialog(frame,
            "Closing Frame", "Information", JOptionPane.INFORMATION_MESSAGE);
        frame.dispose();
    }
}


来源:https://stackoverflow.com/questions/21512639/command-pattern-usefulness-when-using-jcomponents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!