Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DI and Singleton Pattern in one implementation

I want to refactor some code using the Windsor IOC/DI framework, but my issue is that I have some Singleton classes and Factory pattern classes and I am not sure that one can implement a Singleton or Factory using DI.

Has anyone any ideas if that is possible and how?

like image 239
Tony The Lion Avatar asked Dec 16 '09 19:12

Tony The Lion


People also ask

Can you explain various design patterns and implement singleton design pattern?

Singleton Pattern says that just"define a class that has only one instance and provides a global point of access to it". In other words, a class must ensure that only single instance should be created and single object can be used by all other classes. Early Instantiation: creation of instance at load time.

Does dependency injection use singleton?

The use of singletons and dependency injection is not mutually exclusive. A singleton can implement an interface, therefore can be used to satisfy a dependency on another class. The fact that it is a singleton does not force every consumer to obtain a reference through it's "GetInstance" method/property.

Is Dependency Injection same as singleton?

Because Dependency Injection (DI) is one way to ensure that a class has only a single instance. Using the singleton design pattern is another. If you configure your DI setup to return the same instance whenever someone asks for one, the instance essentially becomes a singleton.

What is a DI singleton?

Also note that "singleton" lifetime with regards to DI containers means that there is one instance of a class per container, and that this instance lives at most as long as the container. This is different from SP since there is nothing "global" about this.


2 Answers

The Singleton design pattern is at odds with DI. While it's possible to open up a Singleton so much that DI and the Open/Closed Principle starts to make sense, this will change the Singleton so much that it almost stops being a Singleton.

Thread safety is one big issue that comes to mind when you start opening up a Singleton.

It's much better to simply define your services and classes without considering their scope too much. If you have an object that you would like to share between multiple consumers, most DI Containers have the concept of a Singleton lifetime, which mimics the benefits of the Singleton design pattern without suffering from any of the drawbacks.

In short: Singletons are evil and should be avoided.

Abstract Factory, on the other hand, is very useful for DI purposes.

like image 52
Mark Seemann Avatar answered Oct 03 '22 10:10

Mark Seemann


You don't, you let the IOC container do it. Where before you had explicit calls to a factory to get a singleton instance of an object, now you have the IOC container create a graph of objects for you, and it hooks everything up where it belongs. The container makes sure your singletons are singletons, and it acts as the factory.

If you are talking about having a factory decide at runtime what kind of object to serve up, DI isn't applicable there, except you can have the DI container inject the factory where you want it and manage its scope for you.

like image 40
Nathan Hughes Avatar answered Oct 03 '22 09:10

Nathan Hughes