J2ME Pattern: ActiveRecord

Fri, 10. Oct 2008

Categories: en development Tags: ActiveRecord J2ME pattern

Though J2ME forces you to be close with adding classes, type-safe & convenient persistence might be something you don’t want to miss.

And as RecordStores are the primary application persistence stores, there’s some boilerplate code you can refactor out into one single common base class.

Also โ€“ especially from a pattern standpoint โ€“ it’s a must and increases the unterstandability and maintainability of your code.

So what’s the design assumptions and decisions?

  1. multiple RecordStores can be kept open at least as long as an application is active. So open them in startApp() and close them in pauseApp() and destroyApp(),
  2. use the ActiveRecord pattern rather than ValueBean/DAO to keep the number of classes small,
  3. never access RecordStores directly but shield them with ActiveRecords,
  4. keep track of the ActiveRecord instances and update them via RecordListeners,
  5. provide getBytes/setBytes instance methods to the ActiveRecords,
  6. maybe make ActiveRecords even RecordListener Singletons and refill the data on usage,
  7. maybe even use a single Hashtables subclass as ActiveRecords to even save more classes (requires very disciplined unit testing!),
  8. create few multi-purpose RecordFilter and RecordComparator subclasses per RecordStore,

Check back later for sample code.

Or just don’t code persistence manually but use the floggy J2ME persistence framework. Looks interesting, but I didn’t try it out yet.