选择显示字体大小

j2me编程实践之灵活的rms应用

midp应用程序的标准持久化方案就是使用rms。rms类似于一个小型数据库,recordstore相当于数据库的表,每个“表”由若干记录(record)构成,一条记录就是一个用int表示的记录号recordid和用byte[]表示的内容。记录号可以看作是“主键”,byte[]数组存储内容。

rms提供的记录操作可以实现根据id直接获得记录,或者枚举出一个表中的所有记录。

枚举记录是非常低效的,因为只能比较byte[]数据来确定该记录是否是所需的记录。通过id获得记录是高效而方便的,类似于sql语句“select bytearraydata from recordstorename where recordid=?”。然而,通常应用程序很难知道某条记录的id号,而rms记录的“主键”又仅限于int类型,无法使用其他类型如string作为“主键”来查找。因此,对于需要存取不同类型对象的应用程序而言,就需要一个灵活的rms操作框架

我们的基本设想是,如果能使用string作为“主键”来查找记录,就能非常方便地获得所需的内容。例如,应用程序设置可以通过"sys.settings"获得byte[]数组,并依次读取出设置,用户登录信息可以通过"user.info"获得byte[]数组,再分解出用户名和口令。

因此,我们实现一个storagehandler类,提供唯一的rms访问接口,使得其他类完全不必考虑底层的rms操作,只需提供能标识自身的一个string即可。

如果我们能实现一种类似于数据库索引的查找表,就能根据string关键字查找某条记录。因此,我们使用一个名为"index"的recordstore来存储所有的索引,每一条索引都指向某一条具体记录的id,设计一个indexentry表示一条索引

class indexentry {

private int selfid; // indexentry的id

private int recordid; // 对应记录的id

private string key; // 访问记录的key

}

根据索引查找,分3步进行:

1.在名为"index"的recordstore中根据string查找对应的indexentry。

2.取出indexentry,获得记录id号。

3.根据id号获得另一个recordstore的记录,然后就可以读取、更新和删除该记录。

如下图所示:

由于indexentry保存的数据很少,为了加快查找速度,可以在应用程序启动时,把所有的indexentry读入一个vector,在后面的操作中更新这个vector并与recordstore保持同步。

为了处理不同类型的数据,所有可通过storagehandler存取的类都必须实现一个storable接口:

public interface storable {

string getkey();

void getdata(dataoutputstream output) throws ioexception;

void setdata(datainputstream input) throws ioexception;

}

前面已经提到,在midp应用程序中,序列化一个类的最佳方法是使用datainputstream和dataoutputstream。因此,需要持久化的类可以通过getdata()和setdata()方法非常方便地存取。假定应用程序的类userinfo保存了用户的登录名、口令和是否自动登录的信息:

public class userinfo {

string username;

string password;

boolean autologin;

}

为了能将userinfo存入rms,需要实现storable接口:

class userinfo implements storable {

string username;

string password;

boolean autologin;

public string getkey() { return "user.info"; }

// 提供一个唯一标识符即可

public void getdata(dataoutputstream output) throws ioexception {

output.writeutf(username);

output.writeutf(password);

output.writeboolean(autologin);

}

public void setdata(datainputstream input) throws ioexception {

username = input.readutf();

password = input.readutf();

autologin = input.readboolean();

}

// getters here...

}

要保存userinfo,只需调用storagehandler的保存方法:

storagehandler.storeorupdate(userinfo);

要读取userinfo,调用storagehandler的读取方法:

userinfo userinfo = new userinfo();

storagehandler.load(userinfo);

这样,需要读取或保存数据的类完全不必涉及底层的rms操作,大大简化了应用程序的设计,增强了源代码的可复用性与可维护性。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons