静态类和单例模式之间存在什么实际的区别?
两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?
静态类和单例模式之间存在什么实际的区别?
两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?
当前回答
静态类是一个只有静态方法的类,对于这个类,更好的词是“函数”。静态类中体现的设计风格纯粹是过程性的。
另一方面,Singleton是OO设计特有的模式。它是一个对象的实例(具有其中固有的所有可能性,例如多态性),具有一个创建过程,确保在整个生命周期中只有一个特定角色的实例。
其他回答
一个显著的区别是Singleton附带的不同实例化。
对于静态类,它由CLR创建,我们无法控制它。使用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模拟静态,但并非所有产品都可以使用它。
静态类别:-
无法创建静态类的实例。当加载包含类的程序或命名空间时,由.NET Framework公共语言运行库(CLR)自动加载。我们不能将静态类传递给方法。我们不能将静态类继承到C#中的另一个静态类。具有所有静态方法的类。更好的性能(静态方法在编译时绑定)
辛格尔顿:-
您可以创建对象的一个实例并重用它。当用户请求时,首次创建Singleton实例。您可以创建singleton类的对象并将其传递给方法。Singleton类没有说明继承的任何限制。我们可以处理单例类的对象,但不能处理静态类的对象。方法可以被重写。可以在需要时延迟加载(静态类总是加载的)。我们可以实现接口(静态类不能实现接口)。
我阅读了以下内容,认为这也很有意义:
打理业务记住,最重要的OO规则之一是对象对自己负责。这意味着关于类的生命周期的问题应该在类中处理,而不是委托给静态等语言构造。
来自《面向对象的思维过程》第四版。
单例只是一个被实例化的普通类,但仅从客户端代码间接实例化一次。静态类未实例化。据我所知,静态方法(静态类必须有静态方法)比非静态方法更快。
编辑:FxCop性能规则描述:“不访问实例数据或调用实例方法的方法可以标记为静态(在VB中共享)在这样做之后,编译器将向这些成员发出非虚拟调用点,这将防止在运行时对确保当前对象指针为非空的每个调用进行检查。这可以为性能敏感的代码带来可衡量的性能增益。在某些情况下,无法访问当前对象实例表示正确性问题。"我不知道这是否也适用于静态类中的静态方法。