我应该使用单例 - 到服务器的 Http 连接 - Iphone 应用程序设计
所以我一直在阅读有关使用 Singleton 的利弊,我有一个场景,我不确定是否应该使用它,我想咨询一下你们。
我正在编写一个 Iphone 应用程序,它偶尔必须与服务器(HTTP)通信有关登录用户的信息。 我的应用程序的不同部分正在调用此 Web 服务。
我实际上需要的是 2 种类型的类:
- 负责与服务器通信 - 负责 http 设置、url、标头、参数等。
2. 每种类型的 Web 服务 api 的类 - 例如 UpdateUserInfo 类或 SendResults 类。这个 c
第二类将使用第一类,并且在整个应用程序中我需要对第二类进行多次调用。
我应该对这些论文使用 Singleton 吗?最好的设计方法是什么?
谢谢你们!
编辑:
第一个类(我们称之为 DataDownloader)方法的示例是:
(NSData *) downloadDataWithRequest:(NSURLRequest *)
{
ASIHTTPRequest *dlRequest = [[ASIHTTPRequest alloc] initWithURL:[request URL]];
[dlRequest setTimeOutSeconds:20];
if(retryNum < 1)
retryNum = 1;
[dlRequest setNumberOfTimesToRetryOnTimeout:retryNum];
// and more of the same
}
ASIHTTPRequest 是我们用作 HTTP 包装器的东西。
现在我不想每次想向服务器发送 HTTP 请求时都调用这个函数序列,因此选项是将其放入专用类中并创建一个新实例,或者使用单例。
例如,我可以做的是使用类似的东西:
[[UpdateUserInfo sharedInstance] updateInfo:info]
可以通过使用参数 info 设置 NSURLRequest 并调用
[[DataDownloader sharedInstance] downloadDataWithRequest:InfoUrlRequest]
Now 来实现,假设 http 请求是异步的,我仍然可以同时生成多个请求。
我希望现在更清楚了。 您认为最好的设计方法是什么?
So I've been reading about the pros and cons about using Singleton, and I have a scenario which I'm not sure if I should use one, and I thought consulting you guys.
I'm writing an Iphone app which once in a while have to communicate to the server (HTTP) information about the user logged in.
This web service is getting called from different parts of my application.
What I actually need are 2 type of classes:
- Responsible for communication with the server - responsible for http setting,the url,header,parameters and etc.
2.classes for each type of web service api - for exmpale UpdateUserInfo Class or SendResults Class. This c
The 2nd class would use the 1st , and all through the app I would need many calles to the 2nd classes.
Should I use Singleton to any of theses ? What is the best way to design it ?
Thanks guys!
Edit:
Example for the 1st class(Let's call it DataDownloader) method would be :
(NSData *) downloadDataWithRequest:(NSURLRequest *)
{
ASIHTTPRequest *dlRequest = [[ASIHTTPRequest alloc] initWithURL:[request URL]];
[dlRequest setTimeOutSeconds:20];
if(retryNum < 1)
retryNum = 1;
[dlRequest setNumberOfTimesToRetryOnTimeout:retryNum];
// and more of the same
}
ASIHTTPRequest is something we're using as an HTTP wrapper.
Now I wouldn't want to call this sequence of function each time I want to send the server an HTTP request, So the option would be to put that inside a dedicated class and create a new instance, or using a singletion.
What I can do for example is Using something like that :
[[UpdateUserInfo sharedInstance] updateInfo:info]
Which can be implemented by setting up a NSURLRequest using the param info , and calling
[[DataDownloader sharedInstance] downloadDataWithRequest:InfoUrlRequest]
Now, assuming the http request are asynchronous , I still can spawn multiple at the same time.
I hope it's clearer now.
What do you think is the best way to design it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不认为单例模式是解决您要解决的问题的有用解决方案。它实际上被设计为一种控制对单一/有限资源的访问的技术,但据我所知,这种情况没有任何意义。
例如,您是否有任何理由不允许用户同时执行两项与网络相关的活动?
顺便说一句,出于兴趣,您是否查看过 Cocoa 类,例如 NSURLConnection?我不确定您是否需要一个较低级别的类来管理您所设想的服务器通信 - 这个类可能就足够了。 (不得不在不了解更多关于你想要实现的目标的情况下告诉你。)
I wouldn't see the singleton pattern as being a useful solution to the problem you're trying to solve. It's really designed as a technique to control access to a single/finite resource, which isn't meaningfully the case from what I can tell.
For example, is there any reason why you wouldn't permit a user to carry out two network related activities at the same time?
Incidentally, out of interest have you looked at Cocoa classes such as NSURLConnection? I'm not sure you'd need a lower level class to manage the server communications as you're envisaging - this class might suffice. (Had to tell without knowing more about what you're trying to achieve.)
另外不要忘记 ASIHTTPRequest。这是一个功能齐全的网络库
http://allseeing-i.com/ASIHTTPRequest/
Also don't forget ASIHTTPRequest. It's a full featured network library
http://allseeing-i.com/ASIHTTPRequest/
我通常(这是个人偏好)有一个单例来控制网络管理(单例和外观模式合二为一),以保证不超过 5 个允许的连接。对你来说也是可能的。这将是您任务 1 的一部分的单例。
但正如 BobC 已经指出的那样,ASIHTTPRequest 应该做你需要的一切。
不要重新发明轮子!
I usually (and that's personal preference) have one singleton that controls the network management (a singleton and facade pattern in one) as to not having more than the 5 allowed connections. Could be possible for you as well. That would be a singleton for part of your task 1.
But as BobC has already pointed out, ASIHTTPRequest should do everything you need.
Don't reinvent the wheel!
我使用单例模式来控制对使用 ASIHTTPRequest 的基于 Web 的 API 的访问,并且效果非常好。 ASI 使用 NSOperationQueues 进行异步请求,因此您无需担心请求相互干扰。
因为我不知道请求返回的顺序,所以有时我允许我的公共 API 方法提供 userInfo 字典,以便我的回调在触发时具有一些上下文。
I use the singleton pattern to control access to a web-based API that uses ASIHTTPRequest, and it works very well. ASI uses NSOperationQueues for asynchronous requests, so you don't need to worry about requests clobbering each other.
Because I don't know the order that requests are returned, I sometimes allow my public API methods to supply userInfo dictionaries so my callbacks have some context when they fire.
我将使用服务定位器模式来获取应用程序中所需的每项服务。有几种不同的方法来创建服务定位器。与运行时配置相比,我更喜欢初始化时的配置。
http://en.wikipedia.org/wiki/Service_locator_pattern
I would use the Service Locator pattern to obtain each of the services needed in your application. There are a couple different ways to create the service locator. I prefer configuration at initialization over configuration during runtime.
http://en.wikipedia.org/wiki/Service_locator_pattern