打印
[华禹工控]

写更好的J2ME程序

[复制链接]
1333|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xingyangqing|  楼主 | 2010-3-2 11:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
TI, AN, IO, ic, se
转载:http://www.study-bbs.com/thread-28990-1-3.html
Writing Portable J2ME Applications
by Anurag Gupta
中文 by tomsu

1. Using "switch-case"
多用“switch-case”。

2. Following is the list of the restriction keywords applicable for
methods starting from the slowest one in the terms of performance.
以下是J2ME中“方法”修饰关键字按照对程序性能影响的排列,顺序从慢到快。

synchronized
interface
instance
final
static

因此,要尽量在需要的地方使用 final 和 static。

3. Arguments in the methods
引用方法和传递变量都会带来花费,尽量让程序中的方法最少。
Calling the methods and passing arguments in methods cause overheads.
So try to minimize the number of methods in the application.

4. Accessing the Arrays
避免多次的直接引用数组成员,保存到本地变量然后使用。
Instead of accessing an array element a number of times, store that
element in a local variable and use it.

5. Using the device specific API
尽量避免使用设备特有的API。但无法避免时也需要使用。

6. Separate Game Logic from View
分离程序的操作逻辑与界面类。

7. Use of shift operator for multiplication and divsion
使用位移操作来完成乘除操作。

8. Garbage Collector
避免频繁调用垃圾回收程序,因为这样会导致严重的性能问题。
Avoid calling the garbage collector too often in your application as
this result into serious performance issue.
Garbage collection process takes a lot of time and resources and may
cause your application to hang.
垃圾回收进程需要占用很多处理时间和资源,而且可能导致程序挂起。最好把
垃圾回收工组留给KVM,写程序时尽快释放掉不再使用的对象(设置为NULL)。
Its better to leave garbage collection to KVM and to write your
application so that you free objects (set them to NULL) soon as their
utility is over.

9. Instance Variables
Generally it's a very good practice of keeping your variables private
and writing the setters and getters for accessing them. But NOT in j2me.
一般建议声明内部变量为私有,然后为访问该变量添加 set 和 get 方法, 但是
在 J2ME 里不是这样。
The methods generate byte code, which ultimately increases your
application size. Apart from overhead of calling methods, many devices
have very small application size. Hence it's advisable to use public
variables if you can afford to do that.
方法编译后会变成字节码,最终会增加程序的大小。除了引用方法时的开销,许多
机器平台也会限制程序的大小。因此,建议如果可以的话,使用public变量。

10. Number of Classes
Try to minimize the number of classes in your application. More the
number of classes, more overhead on the KVM and also more is the application
size.
尽量减少程序中的类的数量。类越多,KVM的开销越大,程序体积也越大。

11. Using "setClip"
程序界面中有些地方是不变的,因此刷新界面时,只需刷新需要的地方。使用
setClip 方法可以很容易做到。
Painting is very time costly process.

12. Object Creation
Try to minimize the creation / deletion of objects in your application.
Creating objects is a memory consuming process. Also continuous creation
and destroying of ojbects causes memory fragmentation that ultimately
leaks memory that cannot be reused unless KVM (and hence device) is
restarted. So it's better to create the object pool and then use the objects
from that pool.
尽量减少程序中创建和销毁对象的次数。对象创建是消耗内存的过程。不断的创建
和销毁对象导致内存碎片,最终会是内存泄漏。除非重启KVM,泄漏的内存都没法
再用。因此最好创建一个对象池,这样就可以使用池中的对象。

13. Packages
不要用 package, 所有的类包含在默认包中。
Don't use the packages in your class structure and put all your classes
in the default package. This would reduce the processing needs and may also
keep the application size slightly on lower side.

14. Avoid deep hierarchies in your class struct
避免过深的类结构层次。因为在Java中,对象绑定发生在运行时,而过程又很慢。
最好不要用过深层次的类结构。
Since in java, the object binding is done at run time, which is a slow
process, it's better not to use the deep hierarchies in your class structure.

15. Obfuscation
Obfuscation is a technique that helps compress your application and make it
secure against hacking at same time. It shortens your variables and method
names, removes the repetitive code and compresses the graphics. Since the
class files can be decompiled, decompiling obfuscated files is not of much
help as the code has been jumbled up so much that it will take a lot of time
to know what exactly is written in the file. Until now, obfuscation is a
one-way process.
Obfuscation 是一项压缩程序同时反逆向工程的技术。它可以使变量和方法名字变短,
移除重复的代码并压缩图像。虽然class文件能够被反编译,但反编译处理过的文件是
没有多大用的,因为代码会比较混乱,要搞明白需要更多时间。至今,obfuscation
仍然是一项不可逆的处理过程。

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

14

主题

23

帖子

1

粉丝