单例设计模式

发布于 2024-12-04 03:54:55 字数 1277 浏览 2 评论 0原文

可能的重复:
对单例进行子类化的正确方法是什么Objective-C 中的类?

@implementation Singleton

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

+(Singleton *)singleton
{
    static Singleton * singleton;
    if (singleton == nil)
    {
        singleton =[[Singleton alloc]init];
    }
    return singleton;
}
@end

现在到目前为止一切顺利。实际上,init 部分应该是私有的。但我稍后会担心这个。

现在,假设我想对单例类进行子类化。我应该如何修改呢?

另外让我们看看这里的代码:

+(Singleton *)singleton
{
    static Singleton * singleton;
    if (singleton == nil)
    {
        singleton =[[Singleton alloc]init];
    }
    return singleton;
}

我应该将其修改为

+(self *)singleton
{
    static self * singleton;
    if (singleton == nil)
    {
        singleton =[[self alloc]init];
    }
    return singleton;
}

以便单例方法始终返回子类而不是父类

那么我得到了编译错误

现在我知道子类是“父类”。所以子类化单例是没有意义的。

如果我不需要严格的单例呢?

我想要

@interface classA:单例 @interface classB:singelton

是否都有一个引用classA和classB的单个实例的单例方法?

Possible Duplicate:
What's the correct method to subclass a singleton class in Objective -C?

@implementation Singleton

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

+(Singleton *)singleton
{
    static Singleton * singleton;
    if (singleton == nil)
    {
        singleton =[[Singleton alloc]init];
    }
    return singleton;
}
@end

Now so far so good.Actually, the init part should be private. But I'll worry about that later.

Now, say I want to subclass the singleton class. How should I modify it?

Also let's see the code here:

+(Singleton *)singleton
{
    static Singleton * singleton;
    if (singleton == nil)
    {
        singleton =[[Singleton alloc]init];
    }
    return singleton;
}

I should modify that to

+(self *)singleton
{
    static self * singleton;
    if (singleton == nil)
    {
        singleton =[[self alloc]init];
    }
    return singleton;
}

So that the singleton method always return the subclass rather than the parent class

Well I got compile error

Now I know a subclass is a "parent". So subclassing a singleton doesn't make sense.

What about if I do not need a strict singleton.

I want

@interface classA: singleton
@interface classB: singelton

to all have a singleton method that refer to a single instance of classA and classB?

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

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

发布评论

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

评论(1

‖放下 2024-12-11 03:54:55

正如达菲莫在他的评论中提到的那样,对单例进行子类化是没有意义的。让我尝试解释一下。

假设您创建了两个不同的子类,它们都继承自单例基类。
当您实例化它们中的每一个时,只有第一个会实际创建一个实例(因为这是单例的要点)。第二个子类实例将不会被实例化。
但是,子类中添加的功能将在每个单独的子类中实例化。正如您所看到的,它变得很混乱:-)

希望这有帮助......

问候

As duffymo mentions in his comments subclassing a singleton doesn't make sense. Let me try to explain.

Let's say you make two different subclasses both inheriting from your singleton base class.
When you instance each of them, only the first will actually create an instance (as this is the point of a singleton). The second subclass instance will not get instantiated.
However, the functionality added in the subclasses will get instanced in each seperate subclass. It's getting messy as you might see :-)

Hope this helps...

Regards

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