简体   繁体   中英

Android static method vs object creation

Having a problem with my Android application. I currently have a CatDownloadService which is suppose to do 2 things in the background:

  1. Download the information of the cat (id, name, imageid, etc...) from the server as JSON
  2. Parse the JSON into an Cat object
  3. Insert the Cat object into the database through a CatDatabaseAccessObject
  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

  1. method in CatDownloadService (Since my JSON parsing would only be needed in this class)

     parseCatJSON(jsonString); 
  2. method in CatJSONParser.java (Neater, since the logic for JSON parsing is in a class by itself)

     CatJSONParser catJsonParser = new CatJSONParser(); catJsonParser.parseCatJSON(jsonString); 
  3. static method in CatJSONParser.java (Same as 2. Don't need object creation)

     CatJSONParser.parseCatJSON(jsonString); 

Add into database through DatabaseAccessObject

  1. method in CatDatabaseAccessObject.java (Each context that uses the DBHelper is different)

     CatDatabaseAccessObject catDAO = new CatDatabaseAccessObject(this); catDAO.addCat(cat); 
  2. static method in CatDatabaseAccessObject.java (Ensure there is only 1 connection to DB)

     CatDatabaseAccessObject.addCat(cat); 

Save Cat image to external storage

  1. method in CatStorageManager.java

     CatStorageManager catStorageManager = new CatStorageManager(); catStorageManager.writeCatImage(catInputStream); 
  2. static method in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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