Singleton 应该是可继承的还是不可继承的?

发布于 2025-01-02 07:28:24 字数 238 浏览 2 评论 0 原文


Singleton 应该是可继承的还是不应该?

根据 Gof 的说法,“当唯一的实例应该可以通过子类化进行扩展时,并且客户端 应该能够使用扩展实例而不修改其代码。”

但是为什么我会在 MSDN

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2025-01-09 07:28:24

在我的项目中,我使用 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();
    }

}
半寸时光 2025-01-09 07:28:24

我认为你在这里混合了两种不同的东西。单例模式需要所有调用者使用的单个实例。继承只是意味着我可以在类层次结构之间共享通用逻辑。我觉得这是单例模式的实现:
(为了示例,忽略锁定/线程安全性的缺乏)

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 }
}

我们仍然有一个单例,通过 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)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文