我希望为我们的开发人员构建一个包含单例 模式的框架。 但它不能是基类,因为它有一个私有构造函数, 因此可以继承。 我想也许可以使用模板,但C#不支持 模板(将是C# 2005年中期的泛型。 有没有人能够解决单例模式如何编写,作为框架/基础结构类,以便用户可以使用这个班级和 延伸到他们的愿望吗? - 问候 Sharon G 。
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this class and extend it to their desire? -- Regards Sharon G.
推荐答案Sharon< Sh **** @ discussion.microsoft>写道: Sharon <Sh****@discussions.microsoft> wrote: 我希望为我们的开发人员构建一个包含单例模式的框架。但它不能是基类,因为它有一个私有构造函数和<因此可以继承。我想也许可以使用模板,但C#不支持模板(2005年中期将是C#泛型)。 仿制品与模板不一样 - 我看不出他们怎么在这里帮助帮助。 有没有人能够解决单例模式如何在C#中作为框架/基础结构类编写,以便用户可以使用这个类并将其扩展到他们的愿望? I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Generics aren''t the same as templates anyway - I can''t see how they''d help here. Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this class and extend it to their desire?
你基本上不可能。然而,单身模式可以正确地实现,因此简单地说,我认为在每个地方你需要一个单身人士,这不是太麻烦。 。在它的最近,它只是: 公共类Singleton { public static readonly Singleton Instance = new Singleton (); Singleton() { } } 如果你想保证懒惰,你可以将 实例初始化为静态构造函数,但大部分时间你都不会 真的需要。 - Jon Skeet - < sk *** @ pobox> www.pobox/~skeet 如果回复小组,请不要给我发邮件
You basically can''t. However, the singleton pattern can be correctly implemented so simply, I think it''s not too much hassle to do it in each place you need a singleton. At its barest, it''s just: public class Singleton { public static readonly Singleton Instance = new Singleton(); Singleton() { } } If you want to guarantee laziness, you could be the initialization of Instance into a static constructor, but most of the time you won''t really need that. -- Jon Skeet - <sk***@pobox> www.pobox/~skeet If replying to the group, please do not mail me too
Sharon写道: Sharon wrote: 我希望为我们的开发人员构建一个框架这将包括单例模式。但它不能是基类,因为它有一个私有构造函数,因此可以继承。我想也许一个模板可以是用法但是,C#不支持模板(将于2005年中期成为C#泛型)。有没有人能够解决如何编写单例模式,在C#中,作为一个框架/基础设施类,所以用户可以使用这个类并将它扩展到他们的愿望吗? I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this class and extend it to their desire?
具体模式不是(总是)适合成为框架' 类层次结构。模式是设计模式。他们可能 由一堆类等组成。 可以完成一个可继承的单例,但它很难看: 公共类A { public static A Instance = new A(); protected A(){} } 公共类B:A { public static new B Instance = new B(); protected B ():base(){} } 这是不值得的。 bye Rob
Concrete patterns are not (always) suitable to be part of a framework''s class hierarchy. Patterns are, well, design patterns. They may consist of a bunch of classes, etc. An inheritable singleton can be done, but it''s quite ugly: public class A { public static A Instance = new A(); protected A() {} } public class B : A { public static new B Instance = new B(); protected B() : base() {} } It''s not worthwhile. bye Rob
这与Jon不一样,因为C#编译器只是在生成的开头放置静态字段初始值设定项(如果一个人不存在的话)静态构造函数 问候 Richard Blewett - DevelopMentor staff.develop/richardb/weblog 如果你想保证懒惰,你可以初步确定 实例进入静态构造函数,但大部分时间你都不需要b $ b确实需要它。 Wouldn''t that be the same thing Jon, as the C# compiler simply puts static field initializers at the start of a generated (if one doesn''t already exist) static constructor Regards Richard Blewett - DevelopMentor staff.develop/richardb/weblog If you want to guarantee laziness, you could be the initialization of Instance into a static constructor, but most of the time you won''t really need that.
更多推荐
为单例模式创建框架?
发布评论