通过最大化代码重用来编写适用于 Android 和 J2ME 的应用程序的正确设计方法是什么?

发布于 2024-10-17 03:06:27 字数 633 浏览 5 评论 0原文

大多数人都在询问从 J2ME 迁移到 Android 的问题。我不是。我想开发应用程序并让它们在 Android 和 J2ME 上运行。显然,对于每个应用程序,我都会有一个 Android 项目和一个 J2ME 项目,并且这两个项目都将在具有公共代码的共享依赖项目中获取源代码。

我想知道人们认为正确的设计方法应该是什么 最大化代码重用(最大化共享项目中的代码)。

在我典型的 J2ME 应用程序中,有一个控制器/模型和一堆视图。
因此,每个视图都保存对控制器的引用,当用户与视图交互时,视图调用控制器的方法,控制器决定接下来应该显示哪个视图以及其中应该包含哪些数据。视图之间互不了解。我希望如果编写得当,控制器/模型代码应该大部分是可重用的。

问题是:这个 1 个控制器到多个视图如何转换为 Android,其中每个活动执行整个应用程序的一个子集?

一种方法是拥有单个活动(其中的控制器代码)。
这意味着应用程序必须处理 Back(就像 J2ME 一样)。
这还意味着,当调用 onAddContextMenu 时,它必须知道当前正在显示哪种页面才能添加特定于页面的上下文菜单。

另一种方法如下:每个应用程序视图成为一个活动,并且模型控制器从一个活动传递到另一个活动。

大家有什么意见更好吗?其他方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行雁书 2024-10-24 03:06:27

我建议使用 LWUIT 框架。您可以使用 LWUIT。但在android中,你不能使用RMS概念、PIM API和JSR 172以及一些API。
所以你只需要改变那些事情。 LWUIT 支持触摸和非触摸移动设备。

还有一则信息,您可以在 Android 平台上使用纯 Java 功能。因为通常Java ME没有Map、Set、ArrayList、一些字符串功能等。在Android中,您可以使用以上所有功能。没有必要改变,但它降低了代码的复杂性。

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.

赠佳期 2024-10-24 03:06:27

好的,既然我有一些为两个平台开发应用程序的经验(实际上每个平台有 2 个应用程序),我可以回答我自己的问题。首先,我必须指出
平台之间的差异不仅仅是用户界面。有很多事情都是以不同的方式完成的,例如蓝牙、本地化、通知、存储等。

1)我使用了一个在多个视图之间切换的控制器,但我认为,android方式更好,换句话说,控制器应该分解成更小的子控制器,所以它更符合android的活动。这可确保当应用程序处于活动状态时,仅触发所需的代码,而不是将所有控制器都存储在内存中。
2)我使用源链接而不是项目依赖项。我的每个应用程序都是一个项目,其中包含多个源文件夹(使用链接源)。例如,Android 应用程序指向 android 平台 impl 文件夹、通用平台文件夹、android 蓝牙 impl 文件夹、通用蓝牙文件夹、Android Storage impl 文件夹、通用存储文件夹、通用控制器文件夹和 android 特定控制器文件夹,以及类似的存储...(J2me 应用程序拉入所有通用文件夹,但拉入 J2me 文件夹而不是 android 文件夹)
每个源文件夹包含多个包...
3)我使用工厂,所以我有android工厂,它实现创建android对象(来自android源文件夹)的通用接口和创建j2me对象(来自j2me源文件夹)的j2me工厂,当然创建的所有对象都实现通用接口(来自通用源文件夹)
4)我学习了如何在每个平台中处理蓝牙、通知、存储等,有趣的部分是将每个平台中的不同处理方式抽象为适用于这两个平台的通用抽象方式。

这样做的优点(源链接)是:
1)我在同一个工作区中拥有所有项目/应用程序(我现在有一系列j2me应用程序和另一个android应用程序)。我可以看到每个项目中的所有通用代码,并且可以在处理某些项目时轻松修改它,显然它会立即影响它使用的所有其他项目。
2)重构也非常有效。我可以轻松地将类从一个源文件夹移动到另一个源文件夹。
3)这种方式的有趣之处在于,通常程序员会编写一个基类并将其专门化为子类。但是通过链接不同的源代码,您可以让某个类扩展两个不同的超类。换句话说,我可以让 X 类扩展 Y 类,但在两个不同的项目中,Y 类是不同的。这允许我为同一个通用类拥有两个不同的平台特定基类...(这对读者来说一定很困惑)

,所以即使我的每个项目都链接到公共代码和唯一代码,我的所有源代码都在一处,每个项目二进制文件都在项目空间中。因此,公共 .class 文件可能会在使用它们的每个项目中重复。
那么什么将所有工作抽象为通用抽象类,然后我的控制器处理它们。抽象类通常以对我的抽象/通用控制器透明的平台特定方式实现。

好吧,抱歉这个答案非常抽象,所以让我举一个更具体的例子:
所以现在我有一个叫做蓝牙控制器的东西。它是一个抽象实体,在 J2ME 和 android 中都有实现。将来,如果我需要在新的 Android 项目中使用蓝牙,我只需引入蓝牙的通用代码和 Android 特定代码即可。如果我想开发一个 midlet,对于 j2me 来说也是如此。我的可重用组件由通用和特定于平台的实体组成。

我希望这个答案能为那些尝试为 J2ME 和 android(或其他未来的 Java 平台)开发应用程序的人们提供一些帮助
如果您有任何疑问,可以尝试联系我...

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 abstract 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文