简体   繁体   English

Android静态方法与对象创建

[英]Android static method vs object creation

Having a problem with my Android application. 我的Android应用程序有问题。 I currently have a CatDownloadService which is suppose to do 2 things in the background: 我目前有一个CatDownloadService ,它假设在后台执行2件事:

  1. Download the information of the cat (id, name, imageid, etc...) from the server as JSON 从服务器以JSON格式下载cat的信息(id,名称,imageid等)。
  2. Parse the JSON into an Cat object 将JSON解析为Cat对象
  3. Insert the Cat object into the database through a CatDatabaseAccessObject 通过CatDatabaseAccessObject将Cat对象插入数据库中
  4. Download image of cat 下载猫的图片
  5. Save image onto external storage. 将图像保存到外部存储器。

I can't seem to decide between the various ways of achieving this. 我似乎无法在实现此目标的各种方式之间做出决定。 Can someone point me into the right direction? 有人可以指出我正确的方向吗? What I understand about each method is in the (). 我对每种方法的了解都在()中。

Parse JSON 解析JSON

  1. method in CatDownloadService (Since my JSON parsing would only be needed in this class) CatDownloadService中的方法(因为仅在此类中需要我的JSON解析)

     parseCatJSON(jsonString); 
  2. method in CatJSONParser.java (Neater, since the logic for JSON parsing is in a class by itself) CatJSONParser.java中的方法(更简洁,因为JSON解析的逻辑本身在类中)

     CatJSONParser catJsonParser = new CatJSONParser(); catJsonParser.parseCatJSON(jsonString); 
  3. static method in CatJSONParser.java (Same as 2. Don't need object creation) CatJSONParser.java中的静态方法(与2相同。不需要创建对象)

     CatJSONParser.parseCatJSON(jsonString); 

Add into database through DatabaseAccessObject 通过DatabaseAccessObject添加到数据库

  1. method in CatDatabaseAccessObject.java (Each context that uses the DBHelper is different) CatDatabaseAccessObject.java中的方法(使用DBHelper的每个上下文都不同)

     CatDatabaseAccessObject catDAO = new CatDatabaseAccessObject(this); catDAO.addCat(cat); 
  2. static method in CatDatabaseAccessObject.java (Ensure there is only 1 connection to DB) CatDatabaseAccessObject.java中的静态方法(确保与数据库只有1个连接)

     CatDatabaseAccessObject.addCat(cat); 

Save Cat image to external storage 将Cat图片保存到外部存储

  1. method in CatStorageManager.java CatStorageManager.java中的方法

     CatStorageManager catStorageManager = new CatStorageManager(); catStorageManager.writeCatImage(catInputStream); 
  2. static method in CatStorageManager.java CatStorageManager.java中的静态方法

     CatStorageManager.writeCatImage(catInputStream); 

I'm really lost. 我真的迷路了。

I categories static Method in two ways - 我通过两种方式对静态方法进行分类-

  • Harmful if they are keeping some misleading and unnecessary references. 如果他们保留一些误导和不必要的参考文献,将对其有害。

     public static InputStream is = null; public static void harfulStaticMethod(InputStream is) { BatteryManagerC.is = is; /* Read Content */ /* Leave open or is have used at multiple places */ } 
  • Non Harmful only used to access method variables 非有害仅用于访问方法变量

     public static void unharmFullStaticMethod(int a, int b) { int c = a + b; Log.e("Some Is=", String.valueOf(c)); } 

As your question is broad and lead to many points. 由于您的问题涉及面很广,并引出许多要点。 so its just reflect one View. 所以它只反映一个视图。 Hope it help you 希望对你有帮助

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

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