Objective-C中Singleton的替代方案,用于更好的应用程序设计 [英] Alternative to Singleton in Objective-C for better application design

查看:98
本文介绍了Objective-C中Singleton的替代方案,用于更好的应用程序设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在似乎很多Objective-C代码都在使用Singleton。

It seems a lot of Objective-C code is using Singleton nowadays.

虽然很多人抱怨Singleton,例如Google(所有单身人士去哪里了? ),他们的同事也会使用它: http://code.google.com/ mobile / analytics / docs / iphone /

While a lot of people complaining about Singleton, e.g. Google (Where Have All the Singletons Gone?), their fellow engineers also use it anyway: http://code.google.com/mobile/analytics/docs/iphone/

我知道我们已经在Stack Overflow中得到了一些答案,但它们并不完全特定于Objective-C作为动态语言:目标C有类别,而许多其他语言没有。

I know we had some answers in Stack Overflow already but they are not totally specific to Objective-C as a dynamic language: Objective C has categories, while many other languages do not.

那你有什么看法?你还在使用辛格尔顿吗?如果是这样,您如何使您的应用程序更可测试?

So what is your opinion? Do you still use Singleton? If so, how do you make your app more testable?

更新:我认为我们需要使用代码作为更具体讨论的例子,所以关于SO的讨论都是基于理论的,没有一行代码

Updated: I think we need to use codes as example for more concrete discussion, so much discussions on SO are theory based without a single line of code

我们以Google Analytics iOS SDK为例:

Let's use the Google Analytics iOS SDK as an example:

// Initialization
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-0000000-1"
                                        dispatchPeriod:kGANDispatchPeriodSec
                                              delegate:nil];
// Track page view
[[GANTracker sharedTracker] trackPageview:@"/app_entry_point"
                                   withError:&error];

上面代码的美妙之处在于使用方法 startTrackerWithAccountID ,您可以在整个应用程序中运行方法 trackPageview ,而无需通过配置。

The beauty of the above code is once you have initialized using the method "startTrackerWithAccountID", you can run method "trackPageview" throughout out your apps without passing through configurations.

如果您认为Singleton不好,可以你改进了上面的代码吗?

If you think Singleton is bad, can you improve the above code?

非常感谢你的投入,祝你周五愉快。

Much thanked for your input, have a happy Friday.

推荐答案

这篇文章很可能是down down,,但我真的不明白为什么单身人士没有爱情。它们完全有效,你只需要了解它们对它们有用。

This post is likely to be downvote-bait, but I don't really understand why singletons get no love. They're perfectly valid, you just have to understand what they're useful for.

在iOS开发中,你有一个且只有一个你当前的应用程序实例。 。你只是一个应用程序,对吧?你不是两个或零申请,是吗?因此,该框架为您提供了一个UIApplication单例,通过它可以获得应用程序级的操作系统和框架功能。它适当地模拟一些东西,使其成为一个单身人士。

In iOS development, you have one and only one instance of the application you currently are. You're only one application, right? You're not two or zero applications, are you? So the framework provides you with a UIApplication singleton through which to get at application-level os and framework features. It models something appropriately to have that be a singleton.

如果您的数据字段可以且应该只有一个,并且您需要从应用中的所有位置访问它们,那么完全是将单个模型建模为单例也没有错。将单例创建为全局变量桶可能是对模式的误用,我认为这可能是大多数人反对它们的原因。但是,如果你正在建模一些具有单一性的东西,那么单身就好了。

If you've got data fields of which there can and should be only one, and you need to get to them from all over the place in your app, there's totally nothing wrong with modeling that as a singleton too. Creating a singleton as a globals bucket is probably a misuse of the pattern, and I think that's probably what most people object to about them. But if you're modeling something that has "singleness" to it, a singleton might well be the way to go.

一些开发者似乎对单身人士有一种根本的厌恶但是当实际问到为什么时,他们会忽略全局,名称空间和美学。我想我能理解,如果你真的一劳永逸地解决了Singletons的反模式,并且在所有情况下都会被憎恶。但那时你再也没想过了。框架设计不同意你的看法。

Some developers seem to have a fundamental disgust for singletons, but when actually asked why, they mumble something about globals and namespaces and aesthetics. Which I guess I can understand, if you've really resolved once and for all that Singletons are an anti-pattern and to be abhorred in all cases. But you're not thinking anymore, at that point. And the framework design disagrees with you.

这篇关于Objective-C中Singleton的替代方案,用于更好的应用程序设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆