静态类和单例模式之间存在什么实际的区别?

两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?


当前回答

a.序列化-静态成员属于类,因此无法序列化。

虽然我们已经将构造函数私有化,但静态成员变量仍将被携带到子类中。

c.我们不能进行延迟初始化,因为只有在类加载时才会加载所有内容。

其他回答

静态类是一个只有静态方法的类,对于这个类,更好的词是“函数”。静态类中体现的设计风格纯粹是过程性的。

另一方面,Singleton是OO设计特有的模式。它是一个对象的实例(具有其中固有的所有可能性,例如多态性),具有一个创建过程,确保在整个生命周期中只有一个特定角色的实例。

单个静态类实例(即一个类的单个实例,它恰好是一个静态或全局变量)与指向堆上该类实例的单个静态指针之间存在巨大差异:

当应用程序退出时,将调用静态类实例的析构函数。这意味着如果您将该静态实例用作单例,则单例将停止正常工作。如果仍有代码在运行,例如在不同的线程中使用该单例,则该代码可能会崩溃。

Singleton类在应用程序生命周期期间提供一个对象(仅一个实例),例如java.lang.Runtime而静态类只提供静态方法,如java.lang.MathJava中的静态方法不能被重写,但Singleton类中定义的方法可以通过扩展来重写。Singleton类能够继承和多态性来扩展基类、实现接口并能够提供不同的实现。而不是静态的。

例如:java.lang.Runtime是java中的Singleton类,调用getRuntime()方法返回与当前java应用程序关联的运行时对象,但确保每个JVM只有一个实例。

从测试角度来看,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模拟静态,但并非所有产品都可以使用它。

我头脑中的不同是实现面向对象编程(Singleton/Prototype)或函数编程(Static)。

我们过于关注单例模式创建的对象数量,而我们应该关注的是最终我们持有一个对象。正如其他人已经说过的,它可以扩展,作为参数传递,但最重要的是它是状态满的。

另一方面,静态用于实现函数式编程。静态成员属于一个类。他们是无国籍的。

顺便问一下,您是否知道可以创建单例静态类:)