Singleton Design Pattern Alternatives
We can use Singleton pattern while creating objects of thread pools, caches etc to avoid wasting resources. If you want to store global information like item price of products etc. It’s now a anti-pattern and you should avoid it by different substitutes.
Important Note: It has now become Anti-design pattern and we should avoid by using following techniques:
1. Dependency Injection
2. Using Factory design Pattern
3. Using Enum class etc. (Introduced in Java 1.5)
1
2
3
4
5
| // Enum singleton - the preferred approach public enum MySingleton{ INSTANCE Consolas, Monaco, monospace; } |