基于Android設(shè)計(jì)模式之--SDK源碼之策略模式的詳解
策略模式其實(shí)特別簡(jiǎn)單(聽(tīng)到這句話,大家是不是心里一下子放松了?)。
比如排序,官方告訴大家我這里有一個(gè)排序的接口ISort的sort()方法,然后民間各盡其能,實(shí)現(xiàn)這個(gè)排序的方法:冒泡,快速,堆等等。
這些方法就是“不同的策略”。
然后,某個(gè)模塊下,需要一個(gè)排序方法,但是暫時(shí)不能指定具體的sort方法(出于擴(kuò)展的考慮),就需要使用ISort接口了。
最后,具體什么場(chǎng)景下,傳入什么具體的sort方法,實(shí)現(xiàn)靈活的排序。
這就是策略模式!
下面,我們分析Android中的動(dòng)畫(huà)是如何使用策略模式的。
1. 意圖
定義一系列的算法,把它們一個(gè)個(gè)封裝起來(lái),并且使它們可互相替換。
策略模式使得算法可獨(dú)立于使用它的客戶而變化。
2. 結(jié)構(gòu)圖和代碼
Animation不同動(dòng)畫(huà)的實(shí)現(xiàn),主要是依靠Interpolator的不同實(shí)現(xiàn)而變。
定義接口Interpolator:
package android.animation;
/**
* A time interpolator defines the rate of change of an animation. This allows animations
* to have non-linear motion, such as acceleration and deceleration.
*/
public interface Interpolator {
/**
* Maps a value representing the elapsed fraction of an animation to a value that represents
* the interpolated fraction. This interpolated value is then multiplied by the change in
* value of an animation to derive the animated value at the current elapsed animation time.
*
* @param input A value between 0 and 1.0 indicating our current point
* in the animation where 0 represents the start and 1.0 represents
* the end
* @return The interpolation value. This value can be more than 1.0 for
* interpolators which overshoot their targets, or less than 0 for
* interpolators that undershoot their targets.
*/
float getInterpolation(float input);
我們以AccelerateInterpolator為例,實(shí)現(xiàn)具體的策略,代碼如下:
package android.view.animation;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
/**
* An interpolator where the rate of change starts out slowly and
* and then accelerates.
*
*/
public class AccelerateInterpolator implements Interpolator {
private final float mFactor;
private final double mDoubleFactor;
public AccelerateInterpolator() {
mFactor = 1.0f;
mDoubleFactor = 2.0;
}
/**
* Constructor
*
* @param factor Degree to which the animation should be eased. Seting
* factor to 1.0f produces a y=x^2 parabola. Increasing factor above
* 1.0f exaggerates the ease-in effect (i.e., it starts even
* slower and ends evens faster)
*/
public AccelerateInterpolator(float factor) {
mFactor = factor;
mDoubleFactor = 2 * mFactor;
}
public AccelerateInterpolator(Context context, AttributeSet attrs) {
TypedArray a =
context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AccelerateInterpolator);
mFactor = a.getFloat(com.android.internal.R.styleable.AccelerateInterpolator_factor, 1.0f);
mDoubleFactor = 2 * mFactor;
a.recycle();
}
public float getInterpolation(float input) {
if (mFactor == 1.0f) {
return input * input;
} else {
return (float)Math.pow(input, mDoubleFactor);
}
}
}
其他的Interpolator實(shí)現(xiàn)在此不列舉了。
如何在Animation模塊實(shí)現(xiàn)不同的動(dòng)畫(huà)呢?
在這里我想提一個(gè)應(yīng)用很廣的概念:依賴(lài)注入。
在Animation模塊里實(shí)現(xiàn)不同的動(dòng)畫(huà),就是需要我們把各個(gè)Interpolator以父類(lèi)或者接口的形式注入進(jìn)去。
注入的方法一般是構(gòu)造函數(shù),set方法,注釋等等。
我們看看animation類(lèi)是怎么做的:
public abstract class Animation implements Cloneable {
Interpolator mInterpolator;
// 通過(guò)set方法注入
public void setInterpolator(Interpolator i) {
mInterpolator = i;
}
public boolean getTransformation(long currentTime, Transformation outTransformation) {
// ... ...
// 具體調(diào)用
final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
applyTransformation(interpolatedTime, outTransformation);
// ... ...
}
// 缺省實(shí)現(xiàn),是個(gè)小技巧,順便提下,這個(gè)不是重點(diǎn)
protected void ensureInterpolator() {
if (mInterpolator == null) {
mInterpolator = new AccelerateDecelerateInterpolator();
}
}
}
策略模式其實(shí)就是多態(tài)的一個(gè)淋漓精致的體現(xiàn)。
3. 效果
(1).行為型模式
(2).消除了一些if...else...的條件語(yǔ)句
(3).客戶可以對(duì)實(shí)現(xiàn)進(jìn)行選擇,但是客戶必須要了解這個(gè)不同策略的實(shí)現(xiàn)(這句話好像是廢話,總而言之,客戶需要學(xué)習(xí)成本)
(4).代碼注釋中提到了缺省實(shí)現(xiàn),可以讓客戶不了解策略,也能實(shí)現(xiàn)默認(rèn)的策略
(5).注入的方式有多種:構(gòu)造函數(shù),set方法,注釋。配置解析等等
- android設(shè)計(jì)模式之單例模式詳解
- Android開(kāi)發(fā)中的MVC設(shè)計(jì)模式淺析
- Android應(yīng)用開(kāi)發(fā)中控制反轉(zhuǎn)IoC設(shè)計(jì)模式使用教程
- Android設(shè)計(jì)模式之適配器(Adapter)模式
- Android 單例模式 Singleton 簡(jiǎn)單實(shí)例設(shè)計(jì)模式解析
- Android設(shè)計(jì)模式系列之組合模式
- Android設(shè)計(jì)模式系列之工廠方法模式
- Android中的設(shè)計(jì)模式
- android開(kāi)發(fā)設(shè)計(jì)模式之——單例模式詳解
- Android設(shè)計(jì)模式之代理模式Proxy淺顯易懂的詳細(xì)說(shuō)明
相關(guān)文章
Android 帶有彈出收縮動(dòng)畫(huà)的扇形菜單實(shí)例
本篇文章主要介紹了Android 帶有彈出收縮動(dòng)畫(huà)的扇形菜單實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06Android?線程死鎖場(chǎng)景與優(yōu)化解決
線程死鎖是老生常談的問(wèn)題,線程池死鎖本質(zhì)上屬于線程死鎖的一部分,線程池造成的死鎖問(wèn)題往往和業(yè)務(wù)場(chǎng)景相關(guān),本文主要介紹了Android?線程死鎖場(chǎng)景與優(yōu)化,感興趣的可以了解一下2023-12-12android?viewflipper實(shí)現(xiàn)左右滑動(dòng)切換顯示圖片
這篇文章主要為大家詳細(xì)介紹了android?viewflipper實(shí)現(xiàn)左右滑動(dòng)切換顯示圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē)
這篇文章主要為大家詳細(xì)介紹了Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06詳解OpenGL Shader彩虹條紋效果的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何通過(guò)OpenGL Shader實(shí)現(xiàn)彩虹條紋效果,最后的效果和圖片處理軟件colorow中的彩虹效果濾鏡相似,需要的可以參考一下2022-02-02android在連拍菜單中增加連拍張數(shù)選項(xiàng)功能實(shí)現(xiàn)代碼
想要增加連拍張數(shù)選項(xiàng)需要在entries, entryvalues中添加兩項(xiàng),同時(shí)在mtk_strings.xml中添加相應(yīng)的字符串,具體如下,感興趣的朋友可以參考下哈2013-06-06