麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

基于Android設計模式之--SDK源碼之策略模式的詳解

2020-04-11 12:26:38
字體:
來源:轉載
供稿:網(wǎng)友

策略模式其實特別簡單(聽到這句話,大家是不是心里一下子放松了?)。
比如排序,官方告訴大家我這里有一個排序的接口ISort的sort()方法,然后民間各盡其能,實現(xiàn)這個排序的方法:冒泡,快速,堆等等。
這些方法就是“不同的策略”。
然后,某個模塊下,需要一個排序方法,但是暫時不能指定具體的sort方法(出于擴展的考慮),就需要使用ISort接口了。
最后,具體什么場景下,傳入什么具體的sort方法,實現(xiàn)靈活的排序。
這就是策略模式!
下面,我們分析Android中的動畫是如何使用策略模式的。

1. 意圖
定義一系列的算法,把它們一個個封裝起來,并且使它們可互相替換。
策略模式使得算法可獨立于使用它的客戶而變化。

2. 結構圖和代碼
Animation不同動畫的實現(xiàn),主要是依靠Interpolator的不同實現(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為例,實現(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實現(xiàn)在此不列舉了。
如何在Animation模塊實現(xiàn)不同的動畫呢?
在這里我想提一個應用很廣的概念:依賴注入。
在Animation模塊里實現(xiàn)不同的動畫,就是需要我們把各個Interpolator以父類或者接口的形式注入進去。
注入的方法一般是構造函數(shù),set方法,注釋等等。
我們看看animation類是怎么做的:
復制代碼 代碼如下:

public abstract class Animation implements Cloneable {
    Interpolator mInterpolator;
    // 通過set方法注入  
    public void setInterpolator(Interpolator i) {
         mInterpolator = i;
     }

    public boolean getTransformation(long currentTime, Transformation outTransformation) {
        // ... ...
        // 具體調用
        final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
        applyTransformation(interpolatedTime, outTransformation);
       // ... ...
    }

     // 缺省實現(xiàn),是個小技巧,順便提下,這個不是重點
     protected void ensureInterpolator() {
         if (mInterpolator == null) {
             mInterpolator = new AccelerateDecelerateInterpolator();
         }
     }

}

  策略模式其實就是多態(tài)的一個淋漓精致的體現(xiàn)。

3. 效果
(1).行為型模式
(2).消除了一些if...else...的條件語句
(3).客戶可以對實現(xiàn)進行選擇,但是客戶必須要了解這個不同策略的實現(xiàn)(這句話好像是廢話,總而言之,客戶需要學習成本)
(4).代碼注釋中提到了缺省實現(xiàn),可以讓客戶不了解策略,也能實現(xiàn)默認的策略
(5).注入的方式有多種:構造函數(shù),set方法,注釋。配置解析等等

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 精品91av| 国产正在播放 | 久草在线公开视频 | 欧美黑大粗硬毛片视频 | 亚洲欧美日韩在线 | 久久久免费观看完整版 | 综合国产一区 | 看片一区| 黄色网址你懂的 | 日韩精品久久久久久久电影99爱 | 免费午夜视频 | 黄色免费高清网站 | 激情夜色 | a级高清免费毛片av在线 | 91成人在线免费 | 激情小说激情图片激情电影 | 一本到免费视频 | 成人午夜一区 | 黄视频网站免费观看 | 国产精品1区| 国产一区二区免费在线观看 | 日本在线视频一区二区三区 | 久久在线| 免费看日产一区二区三区 | 91麻豆精品国产91久久久更新资源速度超快 | 伊人亚洲精品 | 爱高潮www亚洲精品 chengrenzaixian | 日韩欧美综合在线 | 精品中文视频 | 精品国产一区二区三区四 | 国产日韩在线观看一区 | 国产91在线亚洲 | 久久在草 | a网在线 | 牛牛碰在线 | 老师你怎么会在这第2季出现 | 中国嫩模一级毛片 | 91性视频 | 午夜伦情电午夜伦情电影 | 久久91精品国产91久久yfo | 亚洲91网 |