Disclaimer: I speak in java terms here
Singleton is now considered an antipater, mainly because many people have abused recently, as they are a quick and convenient way to exchange data throughout the application, which is a somewhat excessive extension of the design pattern, which is more suitable for providing access control to a shared resource.
consider the standard output of the program: access to this resource must be protected by one access point to ensure synchronization of records, so you have, for example, System.out as a static instance in java.
the problem is that when you start singleton, you need to know all the detailed details about what you are doing, because you make many strict assumptions in your singleton class, the most important of which is the only class in the system. then you will start using it, believing that it will always be one entry point to your resource, and then an unpleasant error occurs because your class is now deployed to the ejb server, and each ejb context has its own singleton, plus one more singleton for each jsp that was rebooted from the server, plus one singleton for every time your singleton was serialized and deserialized (since you probably forgot to override the readResolve () method).
therefore, therefore, the singleton should be used with great attention and is now considered an antipatter, despite the fact that it is completely useful for their intended use.
in the case of a database cache, it would be a better approach for each class to need a cache using a proxy server for this “cache” resource, so you can add the “find the resource” logic within the proxy itself instead of binding the logic to retrieving a cache single that may or may not work depending on the environment.
therefore, in a few words using singleton as a means of sharing a resource, this is bad because you hard code the resource search method (and ignore single traps), having a single-color resource control for synchronization purposes is completely acceptable.
think of semaphores, they only work if you can always get the same semaphore. in this latter case, the problem may be accessing the singleton from wherever you need to access this semaphore: here you will need some class to wrap the singleton and provide finer control over the life cycle of the semaphore itself.
proxies are designed to cover the role of "providing a resource in the system", whether it be a single application, a client server system, various components of the same system, etc., with the added benefit that using èrpxy the resource search method is opaque. you can provide them with a singleton containing a hash map of cached values, you can access memcached on the network, you can have them read csv during tests, without changing the way they are called from the application.