单身:优点,缺点,设计关注 [英] Singletons: Pros,Cons, Design Concerns

查看:152
本文介绍了单身:优点,缺点,设计关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我承认。我使用单身。我知道你可能会说,坦率地说,看到所有这些答案在互联网上,说到单身人士的坏方面,并建议反对他们真的让我质疑我的编程实践。

I admit it. I am using singletons. I know what you may all say, and frankly, seeing all these answers on the Internet, saying about the bad aspects of singletons, and advising against them really made me question my programming practices.

我已经在StackOverflow中阅读了关于Singletons的一些帖子,但是我发布这个问题不仅仅是为了询问他们,而是看看我在程序中使用它们的方式。

I have already read some posts in StackOverflow regarding Singletons, but I post this question not only to ask about them, but to see some insights about the way I use them in my programs.

我觉得我必须在这里澄清一些事情,并询问正确的方向。

I feel I must really clarify some things here and ask fore directions.

所以让我们考虑一些情况下,我使用singletons很多: p>

So lets consider some cases where I use singletons a lot:


  1. 创建全局变量的访问器,如我的根视图控制器,特定的和始终存在的视图控制器,应用程序状态,我的全局托管对象上下文。

  2. 创建实用程序类,其作业是处理应用程序范围内的数据。例如,我创建一个Singleton将操作我的缓存数据库,它依赖于Core Data。因为我需要创建缓存和其他东西在不同的视图中被放入数据库,它以某种方式感到更好地创建一个类,将处理数据库输入/输出(仔细的线程安全)。

  3. 处理网络会话。实际上,我使用它来保持连接,并每XX秒向服务器发送类似PINg的东西。

我认为总结起来。我真的很喜欢其他开发者的意见。

I think that about sums it up. I would really like opinions from other developers on the matter.

你认为上述问题有更好的解决方案吗?

Do you think that there are better solutions for these problems above?

你认为对单例有更好的替代方案,应该避免它们吗?

Do you think that there are always better alternatives to singletons and that they should be avoided?

在多线程中是否更好地忘记单例?

Is it better in terms of multithreading to forget about singletons?

任何建议和想法都是有用的,最受欢迎。

Any recommendations and thoughts would be useful, and most welcome.

推荐答案

p> singletons当然不总是邪恶的,但正如你提到的,你必须小心线程的安全性(关于这一主题,检查这

Singletons are certainly not always evil, but as you mention you have to be careful about thread safety (on that topic, check out this blog post on singleton initialization).

为什么单身人士经常被谴责的原因之一是:邪恶的是,如果他们太依赖于单例和它的行为,他们可以使它更难以扩展程序的一部分。你提到数据库访问,服务器或桌面应用程序可能开始使用单例实现来处理所有数据库需求,然后尝试使用多个连接来加速独立请求等。打破这样的代码可能是非常困难的。

One reason why singletons are often decried as evil is that they can make it harder to "scale" parts of a program if they rely too much on a singleton and its behavior. You mention database access, a server or desktop application might start out with singleton implementation to handle all database needs and then later try to use multiple connections to speed up independent requests, etc. Breaking up such code can be very hard.

即使在使用CoreData的iOS应用程序中,也可以不依赖于应用程序委托或某个根视图控制器上的全局ManagedObjectContext。

Even in an iOS application using CoreData it can be useful to not rely on a global ManagedObjectContext on your application delegate or some root view controller.

如果你通过每个视图控制器对一个ManagedObjectContext的引用,你可以获得一些灵活性。大多数时候,你只需要将相同的上下文从一个视图控制器传递到下一个视图控制器,但是如果你以后决定,可以为编辑视图创建一个新的ManagedObjectContext,你可以使用undo上下文,但是如果用户决定保存它们或者容易地丢弃它们,则仅将这些改变合并到根上下文中。或者也许你想对一整套对象做一些后台处理。如果一切都在同一个环境下运行,那么最终会出现同步问题。

If you pass each view controller a reference to a ManagedObjectContext you can gain some flexibility. Most of the time you'll just pass the same context from one view controller to the next, but if you ever decide to in the future, you can for example create a new ManagedObjectContext for an editing view where you can use the undo features of the context but only merge the changes back into the "root" context if the user decides to save them or easily discard them otherwise. Or maybe you want to do some background processing on a whole set of objects. If everything operates on the same context, you'll end up with synchronization issues.

这篇关于单身:优点,缺点,设计关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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