简体   繁体   English

单例与静态变量

[英]Singleton Vs static variable

I need to use a global counter in my application which increments count for every request made. 我需要在我的应用程序中使用一个全局计数器,该计数器会为每个请求增加计数。 I am going to put this counter in a separate class something like this: 我将把这个计数器放在一个单独的类中,如下所示:

public class Counter{  
  private static int count = 0;  
  public synchronized static update()
  {
    count += 1;
  }  

  public synchronized static int getCount()
  {
    return count;
  }  
}

There exists only one Counter throughout the lifetime of the application. 在应用程序的整个生命周期中,仅存在一个计数器。 Do I get any benefit by making it a singleton since there is a single one? 既然有一个单身人士,我会从中获得任何好处吗? Does it make more sense to create a single instance instead of having a class with static variables? 创建单个实例而不是使用带有静态变量的类是否更有意义? What would be the benefit 有什么好处

I would make it either static (field and methods) or not. 我将其设为静态(字段和方法),或者设为非静态。 You appear to have a combination which is bound to confuse. 您似乎有一个必定会造成混淆的组合。


In this case, I would just use an AtomicInteger : 在这种情况下,我将只使用AtomicInteger

public enum Counter {
    public static final AtomicInteger COUNTER = new AtomicInteger();
}

so that you can do 这样你就可以

import static Counter.COUNTER;

int num = COUNTER.incrementAndGet();

I don't see any need to make it a singleton or static. 我认为没有必要将其设置为单例或静态。 Instantiate a new Counter in a logical spot (instance variable of a class that's generating the requests?) and use it from there. 在逻辑位置(正在生成请求的类的实例变量?)实例化一个新的Counter ,然后从那里使用它。 That way, the Counter class is still reusable elsewhere if you need it and you don't have to worry about anyone else grabbing and updating your counter... 这样, Counter类在您需要时仍可在其他地方重用,而您不必担心其他人会抓取和更新您的Counter ...

It is more common to use singleton when the class has state (it has fields). 当类具有状态(具有字段)时,使用单例更为常见。 When the class is stateless - it is a utility class and usually its methods are static. 当类是无状态的时,它是一个实用程序类,通常其方法是静态的。

Did not see that this is a question tagged with Java and not C++; 没有看到这是用Java而不是C ++标记的问题; Anyways I will leave it here unless someone wants this removed. 无论如何,除非有人要删除它,否则我将其留在这里。

The thing about static class members is that they are closely coupled with a class rather than an class instance. 关于静态类成员的事情是,它们与类而不是类实例紧密耦合。 In other words there is only one memory allocation for the static variable, that will be shared among all the instances of this class (class objects). 换句话说,静态变量只有一个内存分配,该内存分配将在此类的所有实例(类对象)之间共享。

In your code above, in the getCount() method, you 在上面的代码中,在getCount()方法中,您可以

return this.count;

Remember that static members do not have this pointer, meaning they are to be accessed using classname::static_member when accessing from outside of the class, and use just the variable name when defining class methods, like you did above. 请记住,静态成员没有此指针,这意味着从类外部进行访问时将使用classname :: static_member对其进行访问,并且在定义类方法时仅使用变量名,就像上面所做的那样。 So your code should look similar to: 因此,您的代码应类似于:

return count;

If you want only one copy of the class members, for any number of class objects created, then you are better off with static methods and - static methods can only operate with static members. 如果只需要一个类成员的副本,那么对于创建的任何数量的类对象,最好使用静态方法,并且-静态方法只能对静态成员进行操作。

If you do not like static methods and static members, singleton is not a bad approach. 如果您不喜欢静态方法和静态成员,那么单例并不是一个坏方法。

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

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