开发者

What's the problem with using singleton in an app that use a lot of HTTP request?

I have a singleton class that basically interface all my HTTP requests. So it pretty much looks like this:

Server <--> Singleton <---> view controllers --> views

Is it bad? Why do people tell me that singleton breaks modularity? I think it's a good way to implement http requests, since I need to do som开发者_开发技巧e stuff with the JSON response, and I don't want my view controllers to handle those.


Yes, this is a good idea. In fact, your singleton is simply a controller which talks to other controllers. This is not bad MVC.

Also, this is handier if you have multiple output formats (JSON, XML, HTML, etc). You can let the Singleton handle this. Plus it's DRYer.


I think, you are using good approach, Instead of Creating multiple instances to deal with HTTP, It's nice to have an single gateway.

You could further internally have two more class one for constructing the HTTP requests by sending the appropriate type and other will handle your JSON parsing stuff and pass an well structured JSON data to your ViewConrollers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜