带有委托的单例:好主意还是坏主意?
我创建了作为 Web 服务接口的对象。一个典型的对象是“TaskService”。当客户端使用这些对象之一时,它会调用服务的方法之一(例如“GetTasks”),并且该服务将异步调用远程 Web 服务,并通过委托回发检索到的数据。
目前,要使用其中一项服务,您必须使用 [[TaskService alloc] init] 创建它,但我认为将每个服务制作为单例对象更有意义。
保存对委托的引用的单例对象是否很常见?我的设计主要问题是,每个对象,每当需要使用特定服务时,都必须在调用该服务之前将自己设置为委托,这对我来说似乎不太正确......如果另一个对象怎么办?是在将自己设置为代表并调用服务之间将自己设置为代表吗?
非常感谢!
问候, 缺口
I have created objects that are interfaces to a web service. One typical object would be a "TaskService". When a client uses one of these objects, it invokes one of the service's methods (such as "GetTasks") and the service will asynchronously go off to call the remote web service, and post back the retrieved data via a delegate.
Currently, to use one of these services you have to create it with [[TaskService alloc] init], but I decided it makes more sense to make each service into a singleton object.
Is it common to see singleton objects that hold reference to delegates? My main issue with the design, is that each object, whenever it requires use of a particular service, will have to set itself as the delegate before invoking the service, which doesn't seem quite right to me... What if another object were to set itself as the delegate inbetween you setting yourself as the delegate and invoking the service?
Many thanks in advance!
Regards,
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,由于您引用的原因,这不是一个好主意。单例模式实际上适用于只有其中一种服务的事物,但听起来您的应用程序可能需要这些服务的多个实例。我认为当您实际上只需要实例化服务的多个实例时,您最终会解决这个问题(使用操作队列或某种委托多路复用器)。
Imo this is not a good idea for the reason you cited. The Singleton pattern is really for things there are only one of, but it sounds like your app can have need for multiple instances of these services. I think you'd wind up working around this (using an operations queue or some kind of delegate multiplexer) when you really just need to instantiate multiple instances of your service.
当需要使用 Singleton 对象时,我总是出于您引用的原因避免委托。单例的消费者无法知道(没有一些丑陋的编码)他们是否通过将自己设置为单例的唯一委托而踩到了其他消费者的脚趾。
NSNotifications
是一个更简洁的工具;任意数量的监听者都可以使用通知,而无需关心还有谁在监听。当类之间有明确的所有权时,委派效果最好。没有人拥有单身人士。
When the occasion warrants the use of a Singleton object, I always avoid delegation for the reason you cite. Consumers of a singleton can't know (without some ugly coding) if they're stepping on some other consumer's toes by setting themselves as the one-and-only delegate of the singleton.
NSNotifications
are a much cleaner tool for the job; any arbitrary number of listeners can consume the notifications without caring who else may be listening.Delegation works best when there is clear ownership between the classes. Nobody owns a singleton.
单例并不是真正的问题,通过简单地实例化一个类并将其作为全局变量传递,您会导致同样类型的问题。
正如其他人提到的,队列是一种可能性,或者当您在 Singleton 中的服务上调用任务时,让它实例化一个传入方法和回调委托的 TaskRequest 对象,这样请求就不会互相干扰。事实上,无论如何使用队列来做到这一点都是一个好主意。
Singleton isn't really the problem, you cause the same sort of issues by simply instancing a class and passing it about as a global variable.
As other's have mentioned a queue is a possibility, or when you invoke a task on a service in your Singleton have it instance a TaskRequest object passing in the method and the call back delegate, that way requests can't trample on each other. In fact it would be a good idea to do that with a queue anyway.
单例的范围是整个应用程序。例如:让我们考虑购物应用程序的示例,记录器数据,关于需要在应用程序的不同部分(如订单、付款、购物车等)访问的用户 ID。
委托用于 1 对 1 通信,例如:您可以以您有两类电视和遥控设备为例。您想更改电视频道。 TV 改变频道的委托方法在远程控制设备类中实现。因此,您可以使用遥控设备来更改电视频道。
单例用于与多个接收者通信,而委托模式通常用于一对一通信。
The scope of a singleton is entire application. For example: Let consider the example of shopping application the logger data, about the user ID which need to be accessible on different part of application like order, payment, cart etc.
Delegates are used for 1 to 1 communication, for example: You can take as example You have two classes TV and remote control device. You want to change the channel of TV. Delegate methods of TV for changing channel are implemented in remote control device class. So you use remote control device and you change the channel of the TV.
The singleton is used to communicate with multiple receivers, while the delegation pattern is used usually for 1 to 1 communication.