2015年3月26日 星期四

[Design Partern] Strategy pattern case 1: Android load NDK library

在 Android 的開發中使用 NDK 最佳化的時候,可能會因為平台的差異,導致 native code 無法執行,這時候必須提供 Java 版本的 solution。

Design Pattern 裡面有一個Strategy pattern,他的特性如下:
In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern

1. defines a family of algorithms,
2. encapsulates each algorithm, and
3. makes the algorithms interchangeable within that family.

載入 native library 的時機點也是在 runtime,剛好符合 Startegy pattern 的 case:
例子如下:

2015年3月20日 星期五

[Performance] Fibonacci演算法探討演算法最佳解



這是基本的解法,但是會 overflow,遞迴會造成 stack 爆掉, performance很差

Fibonacci的演算公式,可減少程式的複雜度





接近完美的解法,倒入 cache 機制和 Android 特有的 SparseArray: