Can I replace Singleton with Factory?

There are already quite a few posts about Singleton-Pattern, but I would like to start another one on this topic, since I would like to know if Factory -Pattern would be the right approach to remove this "anti -pattern".

In the past, I used singleton quite a lot, as well as my fellow colleagues, as it is so easy to use. For example, the Eclipse IDE or better its workplace model also makes great use of singletones. This was due to some reports of E4 (the next big version of Eclipse) that made me start rethinking singleton.

The bottom line is that due to these singleton dependencies in Eclipse 3.x are closely related.

Suppose I want to completely get rid of all singletones and use factories instead.
My thoughts were as follows:

  • hide difficulty
  • less connection
  • I have control over how many instances are created (just save the link I have a private factory field)
  • mock factory for testing (with Injection Dependency) when it is behind the interface
  • In some cases, factories may have more than one singleton outdated (logical / component composition depending on the business)
It makes sense? If not, indicate why you think so. An alternative solution is also being evaluated.

thanks

Mark

+4
source share
6 answers

I agree that sometimes it causes feelings. However, it depends on what you are building.

If you replace every single singleton with a factory just because it is "better", you are doing it wrong imho. This should serve the purpose. If you are not going to taunt, if you are sure that you need only one copy, etc., then instead of replacing it is simply "architectural masturbation";)

Do not get me wrong, architecture is very important, but you should not overdo it.

+7
source

In my eyes, the main drawback of singletons is a tight connection (with all its consequences, such as crappy testing, etc.).

The factory template allows you to combine clutch much more freely, so yes: replacing singleton with factory sounds like a good idea.

As Peter Kelly mentions, factories can sometimes also be solitary. However, factories, at least, do not return an instance of themselves, but rather an instance of some implementation of the interface, and this is a huge plus.

+3
source

In my understanding, you cannot "replace" Singleton with Factory, but you can use these templates together. A Factory will return an instance of the object to the caller from the Factory method. If you want to make sure that there is only one instance of a specific object returned by Factory, then this object must be Singleton. You are hiding Singleton in Factory.

If you store a private instance of an object in Factory, and not in the object itself, then how can you ensure that only one instance of the object exists in the system? How can you ensure that this object is created only by your Factory?

You can replace a singleton with an IoC in the order, and indeed, it seems that Singleton has a lot of bad feelings as an anti-template, and even Gamma said that he wanted him to leave it from the GoF book ...

+2
source

Factory is definitely an alternative to Singleton, and I'd rather have the first one over it. Singletones are very difficult to verify and result in a tight connection. Factories, if implemented correctly (using clean interfaces, etc.), result in more testable code and less grip.

However, this does not mean that you should use Factories as a sledgehammer for every problem. There are other patterns that can also lead to less grip than Singleton, but would be a more suitable solution. For example: inverse of a control.

+2
source

Singletones, as described in the design pattern, typically use static variables that make code verification difficult.

I would recommend reading this from Miลกko Hevery and the next two articles 1 and 2 . Here he describes the difference between a single-instance object and a Singleton object, as in the design pattern, and what is preferred is the first.

Thus, your described approach will be a way, in my opinion.

+1
source

Singleton is, of course, an antipattern. And if you use TDD, you should avoid such decisions. Any IoC Framework, such as Windsor or Unity, can emulate this functionality for you without scary static classes. However, if you are not using TDD, and your project is quite simple, you can use it. By the way, I would recommend the IoC Framework over makeshift factories. Do not reinvent the wheel.

+1
source

Source: https://habr.com/ru/post/1308759/


All Articles