静态类和单例模式之间存在什么实际的区别?
两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?
静态类和单例模式之间存在什么实际的区别?
两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?
当前回答
静态类通常用于库,如果我只需要特定类的一个实例,则使用单实例。但是,从内存的角度来看,存在一些差异:通常在堆中只分配对象,分配的唯一方法是当前正在运行的方法。静态类的所有方法都是静态的,从一开始就在堆中,所以通常静态类会消耗更多的内存。
其他回答
在我写的一篇文章中,我描述了为什么单例比静态类好得多的观点:
静态类实际上不是规范类——它是一个包含函数和变量的命名空间由于打破了面向对象的编程原则,使用静态类不是一种好的做法静态类不能作为其他类的参数传递静态类不适合“惰性”初始化静态类的初始化和使用始终是硬跟踪的实现线程管理很困难
真正的答案是Jon Skeet在这里的另一个论坛上给出的。
单例允许访问单个已创建实例-该实例(或而是对该实例的引用)可以作为参数传递给其他方法,并视为正常对象静态类只允许静态方法。
从测试角度来看,Singleton是更好的方法。与静态类不同,singleton可以实现接口,您可以使用mock实例并注入它们。
在下面的示例中,我将对此进行说明。假设您有一个使用getPrice()方法的方法isGoodPrice(。
提供getPrice功能的singleton:
public class SupportedVersionSingelton {
private static ICalculator instance = null;
private SupportedVersionSingelton(){
}
public static ICalculator getInstance(){
if(instance == null){
instance = new SupportedVersionSingelton();
}
return instance;
}
@Override
public int getPrice() {
// calculate price logic here
return 0;
}
}
getPrice的使用:
public class Advisor {
public boolean isGoodDeal(){
boolean isGoodDeal = false;
ICalculator supportedVersion = SupportedVersionSingelton.getInstance();
int price = supportedVersion.getPrice();
// logic to determine if price is a good deal.
if(price < 5){
isGoodDeal = true;
}
return isGoodDeal;
}
}
In case you would like to test the method isGoodPrice , with mocking the getPrice() method you could do it by:
Make your singleton implement an interface and inject it.
public interface ICalculator {
int getPrice();
}
最终Singleton实施:
public class SupportedVersionSingelton implements ICalculator {
private static ICalculator instance = null;
private SupportedVersionSingelton(){
}
public static ICalculator getInstance(){
if(instance == null){
instance = new SupportedVersionSingelton();
}
return instance;
}
@Override
public int getPrice() {
return 0;
}
// for testing purpose
public static void setInstance(ICalculator mockObject){
if(instance != null ){
instance = mockObject;
}
测试等级:
public class TestCalculation {
class SupportedVersionDouble implements ICalculator{
@Override
public int getPrice() {
return 1;
}
}
@Before
public void setUp() throws Exception {
ICalculator supportedVersionDouble = new SupportedVersionDouble();
SupportedVersionSingelton.setInstance(supportedVersionDouble);
}
@Test
public void test() {
Advisor advidor = new Advisor();
boolean isGoodDeal = advidor.isGoodDeal();
Assert.assertEquals(isGoodDeal, true);
}
}
如果我们选择使用静态方法来实现getPrice(),那么很难模拟getPrice)。您可以使用power mock模拟静态,但并非所有产品都可以使用它。
静态类示例
public class Any {
private static Any instance = new Any();
private Singleton() {
System.out.println("creating");
}
}
单例模式只存在一个实例:
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {
System.out.println("creating");
if (instance != null) {
throw new RuntimeException("Imposible create a new instance ");
}
}
}
与静态类的区别
JDK有单例和静态的例子,一方面java.lang.Math是带有静态方法的最终类,另一方面java.lang.Runtime是单例类。
单例的优点
如果您需要维护状态,那么单例模式比静态类更好,因为在静态类中维护状态会导致错误,特别是在并发环境中,如果没有多个线程进行适当的同步并行修改,则可能会导致竞争条件。如果Singleton类是一个沉重的对象,那么它可以被延迟加载,但静态类没有这样的优势,总是急切地加载。使用singleton,您可以使用继承和多态性来扩展基类、实现接口并提供不同的实现。由于Java中的静态方法不能被重写,它们会导致灵活性。另一方面,您可以通过扩展singleton类来重写它中定义的方法。
静态类的缺点
为单例编写单元测试比静态类更容易,因为只要需要单例,就可以传递模拟对象。
静态类的优点
静态类提供了比单例更好的性能,因为静态方法是在编译时绑定的。
单例模式有几种实现方式,每种实现方式都有优缺点。
Eager加载单例双重检查锁定单例按需初始化持有者习惯用法基于枚举的单例
详细描述每一个都太冗长了,所以我只需要链接到一篇好文章-你想知道的关于Singleton的所有信息