因此,我编写了满足我要求的接口,有两个实现: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 自动生成的文档可以在这里找到。
带有接口的 Mercurial 存储库和所有实现都可以在这里找到。
我如何使用它:
正如我在问题中已经说过的,我有适用于 Android 的应用程序和适用于 J2ME 的应用程序,这两个应用程序都做了类似的事情。(如果有人感兴趣,他们通过蓝牙与远程嵌入式设备进行通信)
这两个应用程序都有共同的低级部分来完成主要工作。
我有interface IMainApp
类似的东西:
public interface IMainApp {
public ISDataStoreFactory getDataStoreFactory();
/*
* ... some other methods
*/
}
两个应用程序(对于 Android 和 J2ME)都有自己的该接口实现,并且它们将其引用传递给低级部分。当底层部分想要打开一些数据存储时,它使用ISDataStoreFactory
返回的IMainApp.getDataStoreFactory
. 它就像我希望它工作一样工作。
希望它对任何人都有用。