简体   繁体   English

单例动作与多实例动作

[英]Singleton Action versus Multi instance Action

I have a dude about how to implement Actions in Swing. 我对如何在Swing中实现Actions有好感。

My idea is create a Class for each action of my application extending AbstractAction so I can use in many components that must have the same behavior. 我的想法是为扩展AbstractAction的应用程序的每个操作创建一个Class,以便可以在必须具有相同行为的许多组件中使用。 So I finaly have something as: 所以我最终有了一些东西:

public class ActionExample extends AbstractAction {

    @Override
    public void actionPerformed(ActionEvent arg0) {

        System.out.println("Do something");

    }

}

Well, now when I want to use it I have three options in my mind: 好吧,现在当我想使用它时,我想到了三个选择:

public void makeUI1() {

    JButton btn = new JButton(new ActionExample("Do it"));

    JMenuItem mi = new JMenuItem(new ActionExample("Do it"));

}

public void makeUI2() {
    Action a = new ActionExample("Do it");
    JButton btn = new JButton(a);
    JMenuItem mi = new JMenuItem(a);
}

Or use it as a singleton (also changing ActionExample): 或将其用作单例(也更改ActionExample):

public void makeUI2() {

    JButton btn = new JButton(ActionExample.getInstance());
    JMenuItem mi = new JMenuItem(ActionExample.getInstance());
}

public class ActionExample extends AbstractAction {

    private static final ActionExample INSTANCE = new ActionExample("Do it");

    public static Action getInstance() {
        return INSTANCE;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        System.out.println("Do something");

    }

}

My first opinion was make it through singleton instance but I see in oracle tutorial that it make a new instance before setting it into components and in the I also see that many code create new instance for each component so I don't know what it's better and why. 我的第一意见是通过单例实例进行创建,但是我在oracle教程中看到它在将其设置为组件之前先创建了一个新实例,并且我还看到很多代码为每个组件创建了一个新实例,所以我不知道这会更好以及为什么。

Is preferred one method to be used over the other? 是首选的一种方法,而不是另一种?

The multi instance action allows you to save data in the moment of the action for further use. 多实例操作允许您在操作时保存数据以备将来使用。

Imagine you want to add undo/redo functionality. 假设您要添加撤消/重做功能。 You need to save what actions have been done for every action. 您需要保存为每个操作执行的操作。

Singleton does not provide any advantage in this case. 在这种情况下,Singleton不提供任何优势。

I think the best thing to do would be to use the MVC pattern. 我认为最好的办法是使用MVC模式。 Your AbstractAction class is a controller. 您的AbstractAction类是一个控制器。 It's responsible for extracting the information necessary for the model (ie: business logic) to use. 它负责提取使用模型所需的信息(即业务逻辑)。 The model/business logic is the part you reuse, but the controller may differ greatly even if it uses the same business logic. 模型/业务逻辑是您重用的部分,但是控制器即使使用相同的业务逻辑也可能会有很大的不同。

For example, you may have a JComponent that you need to add a KeyListener to. 例如,您可能需要向其添加KeyListener的JComponent。 Suddenly, your pre-made AbstractAction has become worthless because it can't be used in this situation. 突然之间,您预制的AbstractAction变得一文不值,因为在这种情况下无法使用它。 But, as long as you reuse all the business logic code in your KeyListener that you used in your AbstractAction, you're doing things right. 但是,只要您在AbstractAction中使用的KeyListener中重用所有业务逻辑代码,您所做的一切就正确了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM