静态类和单例模式之间存在什么实际的区别?
两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?
静态类和单例模式之间存在什么实际的区别?
两者都可以在没有实例化的情况下调用,两者都只提供一个“实例”,而且都不是线程安全的。还有其他区别吗?
当前回答
为了说明Jon的观点,如果Logger是一个静态类,下面所示的操作是无法完成的。类SomeClass希望ILogger实现的一个实例传递到其构造函数中。
Singleton类对于实现依赖注入非常重要。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var someClass = new SomeClass(Logger.GetLogger());
}
}
public class SomeClass
{
public SomeClass(ILogger MyLogger)
{
}
}
public class Logger : ILogger
{
private static Logger _logger;
private Logger() { }
public static Logger GetLogger()
{
if (_logger==null)
{
_logger = new Logger();
}
return _logger;
}
public void Log()
{
}
}
public interface ILogger
{
void Log();
}
}
其他回答
单个静态类实例(即一个类的单个实例,它恰好是一个静态或全局变量)与指向堆上该类实例的单个静态指针之间存在巨大差异:
当应用程序退出时,将调用静态类实例的析构函数。这意味着如果您将该静态实例用作单例,则单例将停止正常工作。如果仍有代码在运行,例如在不同的线程中使用该单例,则该代码可能会崩溃。
我不是一个伟大的OO理论家,但据我所知,我认为与Singleton相比,静态类唯一缺少的OO特性是多态性。但如果您不需要它,那么使用静态类,您当然可以继承(不确定接口实现)以及数据和函数封装。
Morendil的评论,“静态类中体现的设计风格纯粹是程序性的”我可能是错的,但我不同意。在静态方法中,您可以访问静态成员,这与单例方法访问其单个实例成员完全相同。
编辑:我现在实际上在想,另一个区别是,静态类在程序开始时被实例化,并且在整个程序的生命周期中都存在,而单例在某个时刻被显式实例化,也可以被销毁。
*或者它可能在第一次使用时被实例化,这取决于语言,我认为。
主要区别在于:
Singleton有一个实例/对象,而静态类是一堆静态方法Singleton可以在静态时通过接口进行扩展班级不可能。Singleton可以继承,它支持另一方面,静态类不能继承SOLID原则我们需要自己做出改变。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模拟静态,但并非所有产品都可以使用它。
在许多情况下,这两者没有实际差异,特别是如果单例实例从未更改或更改非常缓慢,例如保持配置。
我想说,最大的区别是单例仍然是一个普通的JavaBean,而不是专门的仅静态Java类。正因为如此,单身汉在很多情况下都被接受;它实际上是默认的Spring框架的实例化策略。消费者可能知道也可能不知道它是一个被传递的单例,它只是把它当作一个普通的Javabean。如果需求发生变化,而单例需要成为原型,正如我们在Spring中经常看到的那样,它可以完全无缝地完成,而无需对消费者进行一行代码更改。
前面有人提到,静态类应该是纯过程的,例如java.lang.Math。在我看来,这样的类永远不应该被传递,它们永远不应该将静态final以外的任何属性作为属性。对于其他一切,使用单例,因为它更灵活,更易于维护。