Android 上的装箱和拆箱

发布于 2024-09-27 15:16:52 字数 1013 浏览 4 评论 0原文

我正在开发一个 Android 应用程序。

我有以下接口:

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);
}

对于 enum Table 上定义的每个表,它将是代表该表的对象。

我的问题是关于参数Object item。每次调用其中一个方法时,我都必须拆箱每个项目,我不知道这对于 Android 来说是否会很慢。

您知道更好的解决方案吗?我不想为每个表创建一个 DBAdapter,因为我应该在彼此之间共享 SQLiteDatabase 对象。

更新:

这是我需要传递给这些方法的对象的示例:

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

    public GameDescriptionData() {
    }
}

谢谢。

I'm developing an Android application.

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.

My problem is about parameter 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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给我一枪 2024-10-04 15:16:52

您还没有说要在表中放入什么样的值。除非您插入原始值,否则不会涉及任何装箱。例如,如果 itemString 引用,则不需要装箱,因为它已经是引用了。

出于性能考虑,我建议您在更改之前先尝试一下您的理想设计。诚然,我并不完全相信这是一个很棒的设计,但如果不了解更多关于您的应用程序的信息,就很难说。

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.

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.

离去的眼神 2024-10-04 15:16:52

我建议使用策略模式来实现对每个预期项目的不同关注。
定义< code>DBAdapterStrategyFactory 将根据类类型将所有策略存储在一起。这样,当调用对象项上的操作时,您可以从工厂中提取该策略并共享大部分IDBAdapter代码。

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. This way, when calling operations on Object item you can pull that strategy from the factory and share most of the IDBAdapter code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文