简体   繁体   English

如何防止类实例相乘

[英]how to prevent instances of classes from multiplying

How do I prevent multiple instances of the Scanner class from running? 如何防止Scanner类的多个实例运行? There is information about threads & locks etc but I cannot get them to work on my application. 有关于线程和锁等的信息,但是我无法让它们在我的应用程序上工作。 For example, I click on Pay, then an instance of Scanner appears, then Cancel which creates Pay. 例如,我单击“付款”,然后出现“扫描仪”实例,然后单击“取消”创建付款。 Then when I click on Pay, the number of Scanner(s) doubles. 然后,当我单击“付款”时,扫描仪的数量将增加一倍。 Heres a short summary: 简短摘要:

@ PlaceOrder.java, i have: @ PlaceOrder.java,我有:

btnPay.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) 
{
    java.awt.EventQueue.invokeLater(new Runnable() 
    {
           public void run() 
           {
                 PlaceOrder.this.setVisible(false);

                 new boundary.ScannerUI().setVisible(true);  
                 PlaceOrder.this.dispose();                                 
           }
    });             

} }); }});

@ Scanner.java, i have: @ Scanner.java,我有:

btnCancel.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent evt)
            {
                //ScannerUI.this.setVisible(false);
                //ScannerUI.this.dispose();                     

                new boundary.PlaceOrder().setVisible(false);
                ScannerUI.this.dispose();
            }
        }); 

What you need is called the Singleton-Pattern , which is a quite common and basic design pattern. 您需要的称为Singleton-Pattern ,这是一种非常常见的基本设计模式。

Basicly, the singleton pattern provides a static method which instantiates the object (if it is not already instantiated) and returns the (already) created instance. 基本上,单例模式提供了一种静态方法,该方法实例化对象(如果尚未实例化)并返回(已经)创建的实例。

Singletons should have 单身人士应该有

  • a private constructor, 私有构造函数
  • a static instance, 一个静态实例,
  • and a static access method, which creates and returns the instance. 以及一个静态访问方法,该方法创建并返回实例。

In your case the ScannerUI class would look like this, 在您的情况下,ScannerUI类看起来像这样,

public class ScannerUI extends JFrame { //extending JFrame is just my assumption
// as the OP didn't provided the class declaration code


    //static instance
    private static volatile ScannerUI instance;

    //private constructor
    private ScannerUI() {
        // Do nothing.
    }

    //static access method, which will be called instead of the constructor
    //UPDATE: added isDisplayable because it is disposable.
    public static synchronized ScannerUI getInstance() {
        if (instance == null || !instance.isDisplayable()) {
            instance = new ScannerUI();
        }
        return instance;
    }
}

UPDATE 更新

To access an singleton object, you need to call its public accessor method and use it like other objects. 要访问单例对象,您需要调用其公共访问器方法,并将其与其他对象一样使用。

ScannerUI scanUI = ScannerUI.getInstance();
scanUI.setVisible(true);
scanUI.dispose();
etc.

UPDATE 更新

You can of course implement the private constructor as your present one, you should just make sure, that the constructor is not visible outside of this class, as this would violate the concept of singletons. 当然,您可以将私有构造函数实现为当前构造函数,只需确保该构造函数在此类之外是不可见的,因为这将违反单例的概念。

"Do nothing." “没做什么。” is just a placeholder, as I don't know what your actual constructor looks like. 只是一个占位符,因为我不知道您的实际构造函数是什么样子。

If you want to reuse a single instance of ScannerUI , then don't ever call its constructor; 如果要重用ScannerUI的单个实例,则永远不要调用其构造函数; instead provide factory method which will create an instance and save it in a static variable. 而是提供将创建实例并将其保存在静态变量中的工厂方法。 Basic idea: 基本思路:

static ScannerUI instance;

ScannerUI newScanner() {
   return instance == null || instance.isDisposed()? 
      instance = new ScannerUI() : instance;
}

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

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