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

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


当前回答

一个显著的区别是Singleton附带的不同实例化。

对于静态类,它由CLR创建,我们无法控制它。使用singleton,对象在尝试访问的第一个实例上实例化。

其他回答

单个对象存储在堆中,但静态对象存储在堆栈中。我们可以克隆(如果设计器不允许)单例对象,但不能克隆静态类对象.单类遵循OOP(面向对象原则),静态类则不遵循。我们可以用Singleton类实现接口,但类的静态方法(例如C#静态类)不能。

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

在单例模式中,可以将单例创建为派生类型的实例,但不能使用静态类。

快速示例:

if( useD3D )
    IRenderer::instance = new D3DRenderer
else
    IRenderer::instance = new OpenGLRenderer

Singleton的一个主要优势:多态性例如:使用Class工厂创建实例(比如基于某些配置),我们希望这个对象是真正的单实例。

正如我所理解的,静态类和非静态Singleton类之间的区别,静态在C#中只是一个非实例化的“类型”,其中Singleton是一个真正的“对象”。换句话说,静态类中的所有静态成员都被分配给该类型,但在Singleton中,这些静态成员都位于对象之下。但请记住,静态类的行为仍然像引用类型,因为它不是Struct那样的值类型。

这意味着当你创建一个Singleton时,因为类本身不是静态的,但它的成员是静态的,所以它的优点是Singleton内部的静态成员(指的是它自己)连接到一个实际的“对象”,而不是一个空的“类型”。这就澄清了静态和非静态Singleton之间的区别,超越了它的其他特性和内存使用,这让我感到困惑。

两者都使用静态成员,它们是成员的单个副本,但Singleton将引用的成员包装在一个真正的实例化“对象”周围,该对象的地址除了静态成员之外还存在。该对象本身具有财产,其中可以传递和引用in,从而增加值。静态类只是一种类型,因此除了指向其静态成员外,它不存在。这个概念某种程度上巩固了Singleton vs Static Class超越继承和其他问题的目的。