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

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


当前回答

静态类别:-

无法创建静态类的实例。当加载包含类的程序或命名空间时,由.NET Framework公共语言运行库(CLR)自动加载。我们不能将静态类传递给方法。我们不能将静态类继承到C#中的另一个静态类。具有所有静态方法的类。更好的性能(静态方法在编译时绑定)

辛格尔顿:-

您可以创建对象的一个实例并重用它。当用户请求时,首次创建Singleton实例。您可以创建singleton类的对象并将其传递给方法。Singleton类没有说明继承的任何限制。我们可以处理单例类的对象,但不能处理静态类的对象。方法可以被重写。可以在需要时延迟加载(静态类总是加载的)。我们可以实现接口(静态类不能实现接口)。

其他回答

主要区别在于:

Singleton有一个实例/对象,而静态类是一堆静态方法Singleton可以在静态时通过接口进行扩展班级不可能。Singleton可以继承,它支持另一方面,静态类不能继承SOLID原则我们需要自己做出改变。Singleton对象可以传递给方法,而静态类作为它没有实例不能作为参数传递

我们有一个连接到后端的DB框架。为了避免跨多个用户的脏读,我们使用了单例模式来确保在任何时候都有一个实例可用。

在c#中,静态类不能实现接口。当单个实例类需要实现用于业务合同或IoC目的的接口时,这就是我在没有静态类的情况下使用Singleton模式的地方

Singleton提供了一种在无状态场景中维护状态的方法

希望这对你有帮助。。

单例的另一个优点是它可以很容易地序列化,如果您需要将其状态保存到磁盘或远程发送到某个位置,这可能是必要的。

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

静态类通常用于库,如果我只需要特定类的一个实例,则使用单实例。但是,从内存的角度来看,存在一些差异:通常在堆中只分配对象,分配的唯一方法是当前正在运行的方法。静态类的所有方法都是静态的,从一开始就在堆中,所以通常静态类会消耗更多的内存。