美文网首页
Android设计模式系列(11)--SDK源码之策略模式

Android设计模式系列(11)--SDK源码之策略模式

作者: __帝华 | 来源:发表于2017-04-28 10:27 被阅读0次

1. 意图
定义一系列的算法,把它们一个个封装起来,并且使它们可互相替换。
策略模式使得算法可独立于使用它的客户而变化。

2. 结构图和代码
Animation不同动画的实现,主要是依靠Interpolator的不同实现而变。

定义接口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为例,实现具体的策略,代码如下:

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实现在此不列举了。
如何在Animation模块实现不同的动画呢?
在这里我想提一个应用很广的概念:依赖注入。
在Animation模块里实现不同的动画,就是需要我们把各个Interpolator以父类或者接口的形式注入进去。
注入的方法一般是构造函数,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);
       // ... ...
    }
 
     // 缺省实现,是个小技巧,顺便提下,这个不是重点
     protected void ensureInterpolator() {
         if (mInterpolator == null) {
             mInterpolator = new AccelerateDecelerateInterpolator();
         }
     }
}

策略模式其实就是多态的一个淋漓精致的体现。

**3. 效果
(1).行为型模式
(2).消除了一些if...else...的条件语句
(3).客户可以对实现进行选择,但是客户必须要了解这个不同策略的实现(这句话好像是废话,总而言之,客户需要学习成本)
(4).代码注释中提到了缺省实现,可以让客户不了解策略,也能实现默认的策略
(5).注入的方式有多种:构造函数,set方法,注释。配置解析等等

相关文章

  • Android设计模式系列(11)--SDK源码之策略模式

    1. 意图定义一系列的算法,把它们一个个封装起来,并且使它们可互相替换。策略模式使得算法可独立于使用它的客户而变化...

  • 策略模式

    Android设计模式源码解析之策略模式策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替...

  • 设计模式整理(7) 状态模式

    学习《Android 源码设计模式解析与实践》系列笔记 介绍 状态模式和策略模式都是行为型模式。状态模式中的行为是...

  • Android 设计模式之简单工厂模式

    设计模式系列文章 Android 设计模式之单例模式 Android 设计模式之Builder模式 Android...

  • 详解 - Builder模式

    “Android设计模式”这个系列主要是对Android项目中的设计模式进行分析总结,学习自《Android 源码...

  • 详解 - 单例模式

    “Android设计模式”这个系列主要是对Android项目中的设计模式进行分析总结,学习自《Android 源码...

  • 设计模式整理(11) 装饰模式

    学习《Android 源码设计模式解析与实践》系列笔记 介绍 装饰模式,也称包装模式,结构型设计模式之一。 定义 ...

  • Android设计模式源码解析之原型模式

    Android设计模式源码解析之原型模式 【备注】只用于个人收藏

  • Android 代理模式

    Android 设计模式系列文章 Android 23种设计模式 前言 代理模式可能是我们平时开发中最常见的模式之...

  • Android设计模式系列(7)--SDK源码之命令模式

    1.意图将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销...

网友评论

      本文标题:Android设计模式系列(11)--SDK源码之策略模式

      本文链接:https://www.haomeiwen.com/subject/gpyozttx.html