Software design patterns are very important for long-term projects. Each pattern can contribute in its way, sure, but were all the patterns born equal?
Some practices are considered better than others.
best practice is a method or technique that has been generally accepted as superior to any alternatives because it produces results that are superior to those achieved by other means or because it has become a standard way of doing things
Wikipedia
Bad practice is an unwise thing to do
Merriam-Webster
Anti-patterns are design patterns that are considered a bad practice and we (in most cases) should avoid using it.
One of the popular infamous anti-patterns is the singleton pattern.
This pattern restricts the user from creating more than one instance of a class. Sometimes we need only one instance of a class, then why is it an unwise thing to do?
First, restrictions limit our software design and implementations. In some cases, the class can be initialized more than once, but the pattern makes us implement the code differently.
Second, using singleton is like declaring a non-const global variable because anyone can access it. It makes debugging and maintaining the code hard since it reduces the modularity and flexibility of the program. We should try to minimize the variable scope.
Third, in multi-threaded applications, we have to verify that the instance is thread-safe (no deadlocks, no starvations, and obviously, no race conditions). Using mutex/semaphore/atomic/lock will slow the program.
As I mentioned before, rarely an anti-pattern is exactly what we need, but I suggest thinking twice before choosing such a pattern.
References
If you have any questions feel free to comment or contact me directly.
Thank you for reading this. Please share and visit soon again,
Orian Zinger.