Android的凌空:​​静态VS对象静态、对象、Android、VS

2023-09-05 02:59:50 作者:时光在反复中等待沦落╰

我是一名大三的Andr​​oid开发人员,我几乎完成了字母版本的我的第一个大项目。我认为,我有的Java 的好知识,但我不知道我是否orgnized我的应用程序的权利。

I am a junior android developer and I almost finished the alpha version of my first big project. I think that I have good knowledges of java but I am not sure if I orgnized my app right.

简短说明:我用我的应用程序的抽射库来发送和从服务器接收数据。正因为如此,我创建了一个来管理服务器的方法。在该类我创造了很多的静态方法为每个连接到服务器,我需要(这样的例子):

Short description: I use in my app the volley library to send and receive data from server. Because of that I created a class to manage server methods. In that class I created a lot of static methods for every connection to server I need(like this example):

public static void sendDataToServer(final Context context, final String data) {
    StringRequest mStringRequest = new StringRequest(Request.Method.POST, URL_VERIFY, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            // get response
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
           // get error response
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            // the POST parameters:
            params.put(API_KEY, API_KEY_VALUE);
            params.put(API_KEY_DATA, data);
            return params;
        }
    };

    Volley.newRequestQueue(context).add(mStringRequest);
}

所以在我的活动课我把这种喜欢 MyServerClass.sendDataToServer(...)

我的问题是:它是确定调用了象我的服务器的方法?或者我应该让他们非静态和实例 MyServerClass 当活动开始了吗?不得不提的是我在这个类约5的方法。

My question is: Is it ok to call my server methods like that? Or should I make them non-static and instantiate MyServerClass when activity is started? I must mention that I have about 5 methods in that class.

我有另一个类一样,用的方法来检查数据的准确性。我应该让这种非静态和实例化它,我需要?

I have another class like that with methods to check data accuracy. Should I make this non-static and instantiate it in the activies I need?

任何参考或建议,是值得欢迎的。在此先感谢!

Any reference or advice is welcome. Thanks in advance!

推荐答案

没有,你的情况,这两种方式都会有相同的结果...

No, in your case, both ways will have the same result...

唯一要提的是,如果你需要接收响应你的要求太(可能在将来),您将需要一个代表/回调/接口添加到类,向右得到的结果回到您的通话活动实例......在这种情况下,这将是最好创建一个非静态实例方法的方式......但是,你可以添加一个非静态方法上您的课太多,所以我看不到任何东西反对。

The only thing to mention is, that if you need to receive the response to your request too (may be in the future), you will need to add a Delegate / Callback / Interface to your class, to get the result right back to your calling activity instance... In that case it would be better to create a "non-static instance method" way... But you can add a non-static Method to your Class too so I don't see anything against it.

更新发表评论

嗯,例如,如果你想提供一个ListView使用图像......在大多数情况下,你第一次请求的JSONArray你的ListView条目,其中包含位于远程服务器上的链接,以位图...

Well for example, if you want to provide a ListView with Images... In most cases you first request an JSONArray with your ListView entries, which contains the links to Bitmaps located on the remote Server...

如果您下载图像异步,并把它们变成在ListView的行(当用户滚动)的ImageViews,它可能是可能是图像的加载时间更长,在ListView将显示在错误的地方图片...对于一些这样你将需要一个Singleton模式,将管理下载你...这会不会有可能与你的类/静态方法

If you download Images Async and put them into the ImageViews in the rows of a ListView (while the user scrolls), it could be possible that images are loaded longer and the ListView will show images in wrong places... For something like that you will need a Singleton Pattern, which will manage the downloads for you... This will not be possible with your class/static Method