简体   繁体   English

Android中的Http请求:善用Singleton还是静态类?

[英]Http requests in Android: Good use of Singleton or static class?

I generally dislike the usage of singletons or static class, since I can refactor them to something different most of the time. 我通常不喜欢单例或静态类的用法,因为我通常可以将它们重构为不同的东西。

However, I am currently designing my access point to a HTTP API on an Android app, and I was thinking that I have the following environment: 但是,我目前正在设计对Android应用程序上的HTTP API的访问点,并且我认为自己具有以下环境:

  • I need to send HTTP requests in the majority of my code modules (Activities). 我需要在大多数代码模块(活动)中发送HTTP请求。
  • The code for sending a request does not depend on the request being sent 发送请求的代码不取决于发送的请求
  • There will always only be one specific user on the app per session (unlike the server-side that has to handle different users etc) 每个会话上应用程序上始终只有一个特定用户(不同于必须处理不同用户的服务器端,等等)

Therefore, I was thinking that this could be a situation where it is justifiable to use a Singleton, or even a static class, to place HTTP requests - In the rest of my code, I would then simply have to use something like: 因此,我认为这可能是有理由使用Singleton甚至是静态类来放置HTTP请求的情况-在我的其余代码中,我只需要使用类似以下内容的方法:

MyHttpAccess.attemptLogin(name, pass, callback)

in order to complete the request. 为了完成请求。 I'm even leaning towards using a static class, as I do not have any variable data that I can think of needing to store. 我什至倾向于使用静态类,因为我没有想存储的任何变量数据。

Does this seem like good or bad design, and what should I potentially change? 这看起来是好设计还是坏设计,我应该改变什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM