While there are many situations where using a singleton cache may be more convenient than having to maintain and pass around a caching context, many situations may arise where such designs end up creating difficulties. If the purpose of a cache is to store things that are by nature genuinely immutable (e.g. numbers and their cube roots), and the cache does not allow outside observation of its state, then having the cache be a singleton would be unlikely to cause any trouble. If, however, a cache is meant to be attached to some mutable entity and track its state, then having the cache be a singleton will effectively compel the observed object to be a singleton as well.
↧