简体   繁体   English

在Android上装箱和拆箱

[英]Boxing and UnBoxing on Android

I'm developing an Android application. 我正在开发一个Android应用程序。

I have the following interface: 我有以下界面:

public interface IDBAdapter {

    public enum Table {
        ...
    }
    public int insertItem(Table table, Object item);
    public boolean removeItem(Table table, long rowIndex);
    public Cursor getAllItemsCursor(Table table);
    public Cursor setCursorToItem(Table table, long rowIndex);
    public void getItem(Table table, long rowIndex);
    public boolean updateItem(Table table, long rowIndex, Object item);
}

For each table defined on enum Table , it will be an object that represents that table. 对于在enum Table上定义的每个enum Table ,它将是一个代表该表的对象。

My problem is about parameter Object item . 我的问题是关于参数Object item I will have to unbox each item every time I call one of this methods and I don't know if this will be very slow for Android. 每次调用其中一种方法时,我都必须将每个项目拆箱,但我不知道这对于Android是否会非常慢。

Do you know a better solution? 您知道更好的解决方案吗? I don't want to create a DBAdapter for each table because I should share SQLiteDatabase object between each other. 我不想为每个表创建一个DBAdapter,因为我应该在彼此之间共享SQLiteDatabase对象。

UPDATE: 更新:

Here is an example of object that I need to pass to those methods: 这是我需要传递给这些方法的对象的示例:

public class GameDescriptionData {
    public int gameId;
    public char[] language = new char[5];
    public String name;
    public String description;

    public GameDescriptionData() {
    }
}

Thanks. 谢谢。

You haven't said what kind of values you're putting into the tables. 您没有说要在表中输入什么样的值。 Unless you're inserting primitive values, there won't be any boxing involved. 除非您要插入原始值,否则不会涉及任何装箱操作。 For example, if item is a String reference, that doesn't need boxing because it's a reference already. 例如,如果itemString引用,则不需要装箱,因为它已经是引用。

I suggest you try your ideal design before changing it for the sake of performance concerns. 我建议您出于性能方面的考虑,在进行更改之前先尝试理想的设计。 Admittedly I'm not entirely convinced this is a great design to start with, but it's hard to say without knowing more about your application. 诚然,我并不完全相信这是一个很棒的设计,但是如果不了解您的应用程序就很难说。

I would suggest the strategy pattern to impl the distinct care for each expected item. 我建议采取策略模式 ,对每个预期项目都赋予独特的关怀。
Define DBAdapterStrategyFactory that will store all the strategies together based on their class type. 定义DBAdapterStrategyFactory ,它将基于所有策略的类类型将它们存储在一起。 This way, when calling operations on Object item you can pull that strategy from the factory and share most of the IDBAdapter code. 这样,当调用Object item上的操作时,您可以从工厂中提取该策略并共享大多数IDBAdapter代码。

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

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