使用单例进行缓存
我最近读到,单例是一种反模式,除非确实需要,否则不应使用。
在我们所有的项目中,我们使用单例模式来保存一些缓存数据。例如:
class SomeClass
{
public SomeClass()
{
var somedata = Singleton.Instance.GetSomeData(stringRepresintation); // or like that
var someData = Singleton.Instance.SomeData;
}
}
保存该数据的推荐设计是什么(静态类或其他)?
I recently read that singleton is an anti-pattern and should not be used unless it is really needed.
In all our projects, we use the singleton pattern to hold some cache data. For example:
class SomeClass
{
public SomeClass()
{
var somedata = Singleton.Instance.GetSomeData(stringRepresintation); // or like that
var someData = Singleton.Instance.SomeData;
}
}
What is the recommended design to hold that data (static class, or something else)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您可以将缓存视为依赖项 - 并将一个实例(可能是同一实例)传递到需要它的所有内容中。这就是我在已经使用依赖注入的情况下所做的。
这将使测试那些需要缓存的类变得更容易 - 您可以为每个测试创建一个新的缓存,而不必担心清除现有的缓存。您还可以并行化测试,而不必担心两个测试会弄乱彼此的缓存。此外,它使缓存依赖性更加清晰。
(是否像 cdhowie 建议的那样使用接口来表示缓存取决于您。您不必这样做,尽管如果您这样做,它会将类与其依赖关系更多地解耦。如果您缓存非常简单,您不介意在测试中使用生产实现,它可能不值得提取接口。)
Well, you could think of the cache as a dependency - and pass an instance (probably the same instance) into everything which needed it. That's what I'd do in a situation where I was already using dependency injection.
That would make it easier to test those classes which need the cache - you could create a new cache for each test, and not have to worry about clearing out an existing one. You could also parallelize tests without worrying about two tests messing up each others caches. Additionally, it makes the cache dependency clearer.
(Whether or not you use an interface to represent the cache as cdhowie suggests is up to you. You don't have to, although it would decouple the classes from their dependencies more if you did. If your cache is very simple and you don't mind using the production implementation in tests, it may not be worth extracting an interface.)
您将创建一些
ICache
接口来定义缓存提供程序所需的成员。然后您将允许将实例传递给SomeClass()
。如果调用者未指定默认实例,您仍然可以使用默认实例(实现ICache
)。这就是依赖注入模式,本质上与单例模式完全相反。这将允许替换不同的缓存机制(或只是不同的缓存实例),而无需对 SomeClass 进行任何更改。
You would create some
ICache
interface that defines the members necessary for a cache provider. Then you would allow an instance to be passed in toSomeClass()
. You can still use a default instance (implementingICache
) if the caller doesn't specify one. This is the dependency injection pattern, which is essentially the complete opposite of the singleton pattern.This will allow a different caching mechanism (or just different cache instances) to be substituted in without any changes to
SomeClass
.