简体   繁体   中英

Singleton design pattern vs static class

当Singleton设计模式优于静态类并且静态类优于Singleton设计模式时,有哪些情况?

Generally singletons are superior to static classes.

Singleton in contrary to static class :

  • can inherit, and can be inherited;
  • can implement interface;
  • can be serialized;
  • can be passed to other classes;
  • can be disposed.

If you choose static class then you choose concrete , there's no flexibility. However, if you use singleton you have to remember to make the instantiation of it thread safe .

This is not really an either or scenario.

Singletons are instances with a static getter, and a private constructor. They are not static classes.

Singleton with certain provisos is a way of ensuring you only have one instance of class.

So the first question is. Do you need an instance, ie does this thing have a state, the second question is given how difficult they make unit testing, do you want one at all.

Have a look at the Service Locator pattern, for instance.

如果您只使用类作为某些函数的容器,请使用静态类..但在大多数其他情况下,您最好使用Singleton设计模式,因为您可能希望重用该对象或实例化它作为一个非单身人士。

Static class are specialty difficult to test. And you can't use the constructor for anything useful.

static classes are preferred in helper methods like the MVC helper.

You can see here tome limitations of a static class. They can only have static members and are sealed.

My final take from this discussion: 1. An object has some state. State means current values of object's attributes. So, if you want to have a scenario, where you want to have some state that can be changed and also want to have only one instance, then use Singleton class. eg suppose there is a log file that you want to update after some successful operation or on some exception. To update this log file we must have a lock on it to avoid any inconsistent data, and it can be achieved through Singleton class. 2. When you do not require state of your object and want to load your class into the memory when application gets started & be remain there till life of application - use Static class.

You can use a static class to offer you simple methods that do not require any state and when you do not need to instatiate an object.

Using a singleton means that you only want to instatiate an object once and you can pass it around and alter its state. With singletons you can also have inheritance or implement an interface.

The main difference between the two is that a singleton can implement an interface, and that allows changing it's behavior for either testing or runtime reasons. A static class is static, and while it can have state, drastically changing it's behavior is going to be difficult.

A singleton, being an instance of a class, can implement an interface, and if used with methods that expect that interface, can easily be be replaced with different behavior.

Logging is a common usage for both.

A static logger is unlikely to be able to log to different medium (database, xlm file, text file, json, stream, webservice), because you either have to use a different API for the calls, or set some state and then have all of the methods impelement all of the different kinds of of persistence.

A singleton logger can implement ILog, and then if you need to switch from logging to a database to logging to a web service, you just use another class (which might or might not be a singleton).

A singleton can be used to move away from a static class slowly. A static class can replace a singleton when you realize that you are never going to be changing behavior.

Neither are hard to test in themselves. But it can be a problem to test other classes that use them. Particularly, a static class -- it is not unknown to want slightly different behavior in some respect (say logging, or having the source/destination of files be a file share vs sharepoint) when testing vs production. In that case, having the class be a singleton allows more easily changing that behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM