连接管理器:Singleton还是不是Singleton? [英] Connection Manager: Singleton or not Singleton?

查看:111
本文介绍了连接管理器:Singleton还是不是Singleton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程式会向Web服务提出许多不同的要求。每个请求都是对ConnectionManager对象的方法的调用。当响应从Web服务到达时,调用委托的方法来通知感兴趣的接收者。此外,为了保持会话活动,需要每X秒轮询一次。



这样说,你认为最好是ConnectionManager是一个Singleton吗? p>

单例更简单(因为我不需要传递一个ConnectionManager的引用给所有需要向Web服务请求的人,或者我不必创建更多的ConnectionManager )。此外,它很容易处理轮询的问题:我只是添加两个方法startPolling和stopPolling在ConnectionManager上。但是我不喜欢使用带有单例的委托(因为只能有一个委托,如果没有一个集合的情况下响应发生了什么?),同时我不喜欢使用通知。我也不喜欢单身人士。)



你有关于替代模式的建议吗?

解决方案

我经历过类似的想法,最终得到了这种模式:



ConnectionManager [singleton] - 负责用于维护与服务器的连接并提交&接收请求



ConnectionQueue [singleton] - 存储一堆等待完成的请求



请求 - 每次需要从服务器中获取内容时创建。它包含所有请求数据(网址,参数等)和对代理的引用。



响应



将所有这些连接在一起...


  1. ConnectionManager在启动时启动,并创建ConnectionQueue

  2. 当需要调用服务器时,创建一个Request对象,传入所有必需的参数并将其添加到ConnectionQueue <

  3. 该队列让管理员知道有一个需要处理的请求。


  4. 管理员创建响应并将其发送回委托人。


My iOS app does a lot of different requests to a Web service. Each request is a call to a method of a ConnectionManager object. When the response arrives from the Web service, a delegate's method is called to notify an interested receiver. Moreover, to maintain the session active, a polling every X seconds is required.

Said so, in your opinion it is better if ConnectionManager is a Singleton or not?

The singleton is simpler (because I do not have to pass a ConnectionManager's reference to all those who need to do a request to the Web service or I do not have to create more ConnectionManagers). Moreover, it is easy to handle the issue of polling: I just add two methods startPolling and stopPolling on the ConnectionManager. But I do not like to use the delegates with a singleton (because there can be only one delegate, and what happens if a response comes when there is not one set?) and at the same time I do not like to use the notifications. I do not like the singleton, too :)

Do you have advice on alternative patterns?

解决方案

I went through similar thinking as you and ended up with this pattern:

ConnectionManager [singleton] - responsible for maintaining a connection to the server and submitting & receiving requests

ConnectionQueue [singleton] - Stores a stack of Requests waiting to be fulfilled

Request - Created each time something is needed from the server. It contains all the request data (urls, params etc) and a reference to the delegate.

Response - A container for the data retrieved from the server along with the original request.

Hooking it all together...

  1. The ConnectionManager is started at startup and it creates the ConnectionQueue
  2. When a call to the server is needed create a Request object, pass in all required params and add it to the ConnectionQueue
  3. The queue lets the manager know there's a request that needs to be processed
  4. The manager removes the request from the queue & makes the call to the server
  5. Data is received
  6. The manager creates the response and sends it back to the delegate.

这篇关于连接管理器:Singleton还是不是Singleton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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