gpt4 book ai didi

android - Android、J2ME、PC通用的简单数据存储接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:18 25 4
gpt4 key购买 nike

我看了很多关于Android、J2ME和RecordStore的问题,但还是找不到令我满意的答案。

我需要实现我的 Java 应用程序的低级部分,它应该可以在不同的平台上运行,现在是 Android 和 J2ME,将来它也应该可以在 PC 上运行。我需要存储简单的数据集,这几乎类似于 J2ME 中的 RecordStore:

应用应该拥有多个记录存储,每个记录有:

  • id(但它应该是“my”id,而不是像在 RecordStore 中那样自动返回的 id),
  • 数据(只是一个字节数组)。

我想我应该写一个包含所需方法的Interface,每个平台都应该有自己的Interface实现。

但是这个任务似乎很常见(至少,对于 Android + J2ME),所以,也许已经有一些轻量级的实现?我问只是因为我不喜欢重新发明轮子。

也许还有一些建议?

最佳答案

因此,我编写了满足我的要求的界面,有两个实现:Android 和 J2ME。

这是界面的样子:

public interface ISDataStore {


/**
* Get number of records in data store.
*/
public int getNumRecords() throws SDataStoreException;

/**
* Get size of one record with specified id in bytes.
*
* @param record_id id of the record
*/
public int getRecordSize(int record_id) throws SDataStoreException;

/**
* Get record.
*
* @param record_id id of the record to read
* @param data byte array where to put the data
* @param offset offset in 'data' array from which should start to copy
*/
public void getRecord(int record_id, byte[] data, int offset) throws SDataStoreException;

/**
* Get record.
*
* @param record_id id of the record to read
*/
public byte[] getRecord(int record_id) throws SDataStoreException;

/**
* Resolves is record with specified id exists or not.
*
* @param record_id id of the record
* @return true if record exists, otherwise false
*/
public boolean isRecordExists(int record_id) throws SDataStoreException;

/**
* Put new record or update existing one.
*
* @param record_id id of the record
* @param data byte array of data
* @param offset offset in the data byte array
* @param length number of bytes to store
*
* @return true if operation was successful, otherwise false
*
*/
public boolean setRecord(int record_id, byte[] data, int offset, int length) throws SDataStoreException;

/**
* Delete the record.
*
* @param record_id id of the record
*
* @return true if operation was successful, otherwise false
*/
public boolean deleteRecord(int record_id) throws SDataStoreException;

/**
* Clear all the records.
*/
public void deleteAll() throws SDataStoreException;

/**
* Close the data store.
*/
public void close() throws SDataStoreException;

}

还有一个用于数据存储的工厂:

public interface ISDataStoreFactory {

/**
* @param dataStoreId id of the data store
* @return ISDataStore with given id. Type of this id depends on target platform
*/
public ISDataStore getDataStore(Object dataStoreId) throws SDataStoreException;

/**
* Destroys data store with given id.
* @param dataStoreId id of the data store. Type of this id depends on target platform
*/
public void destroyDataStore(Object dataStoreId) throws SDataStoreException;

}

可以找到 Doxygen 自动生成的文档 here .

可以找到带有接口(interface)和所有实现的 Mercurial 存储库 here .

如何使用:

正如我在问题中所说,我有适用于 Android 的应用程序和适用于 J2ME 的应用程序,这两个应用程序的功能相似。 (如果有人感兴趣,他们通过蓝牙与远程嵌入式设备进行通信)

这两个应用程序都有共同的低级部分来完成主要工作。

我有 interface IMainApp,类似这样的东西:

public interface IMainApp {

public ISDataStoreFactory getDataStoreFactory();

/*
* ... some other methods
*/
}

两个应用程序(适用于 Android 和 J2ME)都有自己的此接口(interface)实现,并且它们将其引用传递给低级部分。当底层部分想要打开一些数据存储时,它使用 IMainApp.getDataStoreFactory 返回的 ISDataStoreFactory。它就像我希望的那样工作。

希望对大家有用。

关于android - Android、J2ME、PC通用的简单数据存储接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com