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:
例子如下:

沒有留言:

張貼留言