A Singleton should be inheritable or They should not be ?
According to Gof "when the sole instance should be extensible by subclassing, and clients
should be able to use an extended instance without modifying their code."
but then why do i see Sealed and Private constructor examples on MSDN
在我的项目中,我使用 Mark Seemann 的《.NET 中的依赖注入》一书中的环境上下文实现。使用该模式的要点是,当您请求 Current 实例时,必须有一些东西,并且 Context 可以通过其他实现进行切换。
FE
public class TimeContext
{
private static TimeContext _instance;
public static TimeContext Current
{
get
{
if (_instance == null)
{
_instance = new DefaultContext();
}
return _instance;
}
set
{
if (value != null)
{
_instance = value;
}
}
}
public abstract DateTime GetDateTime();
}
和 context 的具体实现应该是这样的:
public class DefaultContext : TimeContext
{
public DateTime GetDateTime()
{
return DateTime.Now();
}
}
In my projects, I use an Ambient Context implementation from Mark Seemanns book Dependency Injection in .NET. The main point of use of that pattern is that always when you are asking for Current instance, there has to be something and also Context can be switched by other implementation.
F.E.
public class TimeContext
{
private static TimeContext _instance;
public static TimeContext Current
{
get
{
if (_instance == null)
{
_instance = new DefaultContext();
}
return _instance;
}
set
{
if (value != null)
{
_instance = value;
}
}
}
public abstract DateTime GetDateTime();
}
And concrete implementation of context should be like:
public class DefaultContext : TimeContext
{
public DateTime GetDateTime()
{
return DateTime.Now();
}
}
public class Singleton
{
private static Singleton _instance;
public static Singleton Instance
{
get
{
if (_instance == null)
{
// This is the original code.
//_instance = new Singleton();
// This is newer code, after I extended Singleton with
// a better implementation.
_instance = new BetterSingleton();
}
return _instance;
}
}
public virtual void ActualMethod() { // whatever }
}
public class BetterSingleton : Singleton
{
public override void ActualMethod() { // newer implementation }
}
I think you're mixing two different things here. The singleton pattern calls for a single instance that is used by all callers. Inheritance just means I can share common logic between a class hierarchy. This, I feel, is an implementation of the singleton pattern:
(ignore the lack of locking/thread safety, for the example's sake)
public class Singleton
{
private static Singleton _instance;
public static Singleton Instance
{
get
{
if (_instance == null)
{
// This is the original code.
//_instance = new Singleton();
// This is newer code, after I extended Singleton with
// a better implementation.
_instance = new BetterSingleton();
}
return _instance;
}
}
public virtual void ActualMethod() { // whatever }
}
public class BetterSingleton : Singleton
{
public override void ActualMethod() { // newer implementation }
}
We still have a singleton, accessed through the Singleton class's static Instance member. But the exact identity of that instance can be extended through subclassing.
发布评论
评论(2)
在我的项目中,我使用 Mark Seemann 的《.NET 中的依赖注入》一书中的环境上下文实现。使用该模式的要点是,当您请求 Current 实例时,必须有一些东西,并且 Context 可以通过其他实现进行切换。
FE
和 context 的具体实现应该是这样的:
In my projects, I use an Ambient Context implementation from Mark Seemanns book Dependency Injection in .NET. The main point of use of that pattern is that always when you are asking for Current instance, there has to be something and also Context can be switched by other implementation.
F.E.
And concrete implementation of context should be like:
我认为你在这里混合了两种不同的东西。单例模式需要所有调用者使用的单个实例。继承只是意味着我可以在类层次结构之间共享通用逻辑。我觉得这是单例模式的实现:
(为了示例,忽略锁定/线程安全性的缺乏)
我们仍然有一个单例,通过 Singleton 类的静态 Instance 成员进行访问。但该实例的确切身份可以通过子类化来扩展。
I think you're mixing two different things here. The singleton pattern calls for a single instance that is used by all callers. Inheritance just means I can share common logic between a class hierarchy. This, I feel, is an implementation of the singleton pattern:
(ignore the lack of locking/thread safety, for the example's sake)
We still have a singleton, accessed through the Singleton class's static Instance member. But the exact identity of that instance can be extended through subclassing.