[英]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件事:
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
method in CatDownloadService (Since my JSON parsing would only be needed in this class) CatDownloadService中的方法(因为仅在此类中需要我的JSON解析)
parseCatJSON(jsonString);
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);
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添加到数据库
method in CatDatabaseAccessObject.java (Each context that uses the DBHelper is different) CatDatabaseAccessObject.java中的方法(使用DBHelper的每个上下文都不同)
CatDatabaseAccessObject catDAO = new CatDatabaseAccessObject(this); catDAO.addCat(cat);
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图片保存到外部存储
method in CatStorageManager.java CatStorageManager.java中的方法
CatStorageManager catStorageManager = new CatStorageManager(); catStorageManager.writeCatImage(catInputStream);
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.