简体   繁体   中英

What's the right design approach for writing an application for both Android and J2ME by maximizing code-reuse?

Most people are asking about migrating from J2ME to Android. I'm not. I'd like to develop applications and have them work on both Android and J2ME. Obviously for each application I will have an Android project and a J2ME project and both will source in a shared dependent project that will have common code.

I'd like to know what do people think the right design approach should be to maximize code reuse (maximize the code in the shared project).

In my typical J2ME application there is a controller/model and a bunch of views.
So each view holds a reference to the controller and when the user interacts with the view, the view invokes a method of the controller and the controller decides which view should be displayed next and what data it should have in it. The views don't know about each other. I would expect the controller/model code should be mostly reusable if written properly.

The question is: How does this 1 controller to many views translate to Android where each activity performs a subset of the whole application?

One approach would be to have a single activity (the controller code in it).
That means the application would have to handle Back (as it would anyway for J2ME).
It also means that when onAddContextMenu is called it would have to know which kind of page is currently being displayed to add the page specific context menu.

Another approach would be the following: each application view becomes an activity and the model-controller is passed around from one activity to another.

Any opinions which is better? other approaches?

I suggested using LWUIT framework. You can develop Android, Blackberry and Midlet based application with small changes using LWUIT . But in android, you can't use RMS concept, PIM API and JSR 172 and some APIs. So you have to change that things only. LWUIT supports both touch and non-touch mobiles.

And one more info, you can use pure Java functionality on Android platform. Because normally Java ME doesn't have Map, Set, ArrayList, some string functionality and etc. In Android, you can use all of above. It's not necessary to change but it reduces the complexity of code.

Ok, so now that i have some experience developing an app for both platforms (actually 2 apps for each platform) i can answer my own question. first of all, i must point out that the differences between the platforms are not just the UI. there are a lot of things that are done differently, for example Bluetooth, localization, notifications, storage, etc'.

1) i used a single controller that switches among many views but i think, the android way is better, in other words, the controller should be broken up into smaller sub-controolers, so it corresponds more to the android's activities. This ensures that only the code that is needed is triggered rather than having all the controller in memory when the app is active.
2) i used source linking rather than project dependencies. Each of my application is a project which pulls in several source folders (using linked sources). An Android app for example points to an android platform impl folder, a generic platform folder, an android bluetooth impl folder, an generic bluetooth folder, an Android Storage impl folder, a generic storage folder, a generic controller folder and an andtroid specific controller folder, and similarly for storage... (J2me app pulls in all the generic folders but pulls in the J2me folders rather than the android folders) each source folder contains several packages... 3) i use Factories, so i have android factories which implement generic interfaces that create android objects (which come from android source folders) and j2me factories that create j2me objects (which come from j2me source folders) and of course all objects created implement generic interfaces (that come from generic source folders) 4) i learned how to handle Bluetooth, notification, storage, etc' in each platform, and the fun part was to abstract the different way you do things in each of the platforms into a generic a bstract way that applies to both platforms.

The advantages of doing it this way (source linking) are:
1) i have all the projects/applications in the same workspace (i now have a family of j2me applications and another of android applications). i can see all the common code in every project and i can modify it easily while working on some project and obviously it immediately affects all other projects that it uses.
2) refactoring works very well too. i can easily move classes from one source folder to another.
3) What's interesting about doing it this way is that normally programmers write a base class and specialize it in sub-classes. But by linking in different source code you can have some class extending two different super-classes. In other words, i can have class X extend class Y, but in two different projects class Y is different. this allows me to have two different platform specific base-classes for the same generic class... (this must be very confusing to the reader)

so even though each one of my projects links to common code and unique code all my sources are in one place and each project binaries are in the project's space. so a common .class file may be duplicated in each project that uses them. so what abstracted all the work into generic abstract classes and my controller deals with them. The abstract classes are usually implemented in a platform specific manner transparent to my abstract/generic controller.

well, sorry that this answer is very abstract so let me give one more concrete example:
so now i have for example something called BluetoothController. it's an abstract entity that has an implementation in J2ME and android. in the future, if i need to use Bluetooth in a new android project, i just pull in the generic code and the android-specific code for Bluetooth. similarly for j2me if i want to develop a midlet. my reusable component is composed of generic and platform-specific entities.

I hope this answer provides some help for people who are trying to develop an app for both J2ME and android (or some other future Java platform)
if you have any questions, you can try contacting me...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM