具有可触发初始化的 C# 单例模式
我需要一个单例:
- 延迟加载
- 线程安全
- 在构造时加载一些值
- 这些值可以随时查询
- 初始化可能在查询开始之前的某个精确时间发生 - 所以我必须能够从外部触发它不知何故。当然,多次触发应该只进行一次初始化。
我使用.NET 3.5。
我已经开始使用 Jon Skeet 的 实现(第 5 版),使用静态子类:
public sealed class Singleton
{
IEnumerable<string> Values {get; private set;}
private Singleton()
{
Values = new[]{"quick", "brown", "fox"};
}
public static Singleton Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}
这打勾几乎所有的框,除了“从外部触发初始化”。由于实际的初始化发生在 ctor 内部,因此不能多次发生。
如何才能做到这一点?
单例将像这样使用:
public static void Main(){
//do stuff, singleton should not yet be initialized.
//the time comes to initialize the singleton, e.g. a database connection is available
//this may be called 0 or more times, possibly on different threads
Singleton.Initialize();
Singleton.Initialize();
Singleton.Initialize();
//actual call to get retrieved values, should work
var retrieveVals = Singleton.Instance.Values;
}
I need a singleton that:
- is lazy loaded
- is thread safe
- loads some values at construction
- those values can be queried at any time
- the initialization MAY happen at some precise time, before the querying begins - so I must be able to trigger it from the outside somehow. Of course, triggering multiple times should only do the initialization once.
I use .NET 3.5.
I've started with Jon Skeet's implementation (5th version) using a static subclass:
public sealed class Singleton
{
IEnumerable<string> Values {get; private set;}
private Singleton()
{
Values = new[]{"quick", "brown", "fox"};
}
public static Singleton Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}
This ticks almost all the boxes, except the "trigger initialization from outside". Since the actual initialization happens inside the ctor, it can't happen more than once.
How can this be accomplished?
The singleton will be used like this:
public static void Main(){
//do stuff, singleton should not yet be initialized.
//the time comes to initialize the singleton, e.g. a database connection is available
//this may be called 0 or more times, possibly on different threads
Singleton.Initialize();
Singleton.Initialize();
Singleton.Initialize();
//actual call to get retrieved values, should work
var retrieveVals = Singleton.Instance.Values;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
似乎您可以这样做:
或者创建一个将更新的单个实例:
第三种变体基于您的不可变注释和删除嵌套类注释:
Seems like you could do:
Or to create a single instance that will be updated:
And the third variation based on your immutable comment and removal of Nested class comment:
我的第一个想法是只使用分配给单例实例的一次性变量,这将(可能?)触发初始化
......但是通过副作用进行编程是我努力避免的,所以让我们将意图作为更清楚一点。
所以用法是:
顺便说一句,这也可以:
除了编译器优化之外,我认为这可以满足所有要求。
The first idea I had was to just use a throwaway variable assigned to the singleton's instance, which would (probably?) trigger the initialization
... but programming by side-effects is something I try hard to avoid, so let's make the intention a bit clearer.
so the usage would be:
Btw this would also work:
Compiler optimization aside, I think this deals with all the requirements.
如果您需要稍后进行初始化,您可以设置一个可以从外部触发的 Initialize 方法,但如果每次触发时的值都不同,则它不能是静态的,这违反了单例模式。
根据你的例子,它没有变量,我假设你只是在初始化发生时延迟(例程而不是构造函数),但你的问题表明你想要不同的值,但如果多个初始化同时发生,它只初始化一次,所以我对此有点困惑。
我不确定您是否需要仅单例实现,但如果没有有关 Initialize() 是否每次都运行相同代码或具有某种类型的变量性质的信息,则无法完全回答。
You can set up an Initialize method that can be fired from outside, if you need the initialize to happen later, but if the values are different on each time it is fired, then it cannot be static, which violates the Singleton pattern.
Based on your example, which has no variables, I assume you are just delaying when the initialization happens (routine rather than constructor), but your question suggests you want different values, but if multiple initializations happen close together, it only initializes once, so I am a bit confused on this.
I am not sure you need a Singleton only implmentation, but cannot fully answer without information on whether or not the Initialize() runs the same code every time or has some type of variable nature.
您可以使用双重检查锁定模式。只需在 Singleton 类中添加以下代码:
You can use double-checked locking pattern. Just add following code in you Singleton class:
你可以做这样的事情
Usage
Which 输出
You can do something like this
Usage
Which outputs