`
yanghaoli
  • 浏览: 284147 次
社区版块
存档分类
最新评论

Android设计模式系列(11)

 
阅读更多

策略模式其实特别简单(听到这句话,大家是不是心里一下子放松了?)。
比如排序,官方告诉大家我这里有一个排序的接口ISort的sort()方法,然后民间各尽其能,实现这个排序的方法:冒泡,快速,堆等等。
这些方法就是“不同的策略”。
然后,某个模块下,需要一个排序方法,但是暂时不能指定具体的sort方法(出于扩展的考虑),就需要使用ISort接口了。
最后,具体什么场景下,传入什么具体的sort方法,实现灵活的排序。
这就是策略模式!
下面,我们分析Android中的动画是如何使用策略模式的。

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方法,注释。配置解析等等

0
5
分享到:
评论

相关推荐

    Android设计模式系列(2)--SDK源码之观察者模式.pdf

    Android设计模式系列(2)--SDK源码之观察者模式.pdf

    Android设计模式系列之工厂方法模式

    工厂方法模式,往往是设计模式初学者入门的模式,的确,有人称之为最为典型最具启发效果的模式。接下来通过本文给大家介绍Android设计模式系列之工厂方法模式,感兴趣的朋友一起学习吧

    设计模式系列之适配器模式

    设计模式系列之适配器模式,分别以Java,Android,IOS进行讨论

    Android设计模式系列之组合模式

    在android UI设计,几乎所有的widget和布局类都依靠这两个类。 组合模式,Composite Pattern,是一个非常巧妙的模式。几乎所有的面向对象系统都应用到了组合模式。 1.意图 将对象View和ViewGroup组合成树形结构以...

    Android设计模式系列之单例模式

    单例模式,可以说是GOF的23种设计模式中最简单的一个。接下来通过本文给大家实例讲解android设计模式系列之单例模式的相关知识,感兴趣的朋友一起看看吧

    设计模式在android中的应用之1,简单工厂模式

    简单工厂模式在android中的应用。简单工厂模式相信大家都懂了。但是我觉得理论知识缺乏应用永远显得苍白无力。于是贴出了这一系列代码。都是关于设计模式在android中的简单应用。

    DesignPattern:23种设计模式,Android原始设计模式分析-android

    设计模式系列23种设计模式设计模式是一套被反复使用,多数人知道的,经过分类编目的,代码设计经验的总结。使用设计模式是为了可重用代码,让代码更容易被他人理解,保证代码可靠性。本文将介绍23种设计模式。创建型...

    Android开发-设计模式博客演示代码

    Android开发-设计模式系列博客的演示代码。 https://blog.csdn.net/xiaocheng0404/category_10196977.html

    Android源码设计模式分析项目.zip

    这部分内容包含了多个精心设计的Android项目案例,从需求分析、设计思路到实现过程,都有详细的讲解和代码示例。学习者可以通过实际操作,深入了解Android开发的整个流程,提升自己的实战能力。 此外,我们还提供了...

    Android编程设计模式之命令模式详解

    本文实例讲述了Android编程设计模式之命令模式。分享给大家供大家参考,具体如下: 一、介绍 命令模式(Command Pattern),是行为型设计模式之一。命令模式相对于其他的设计模式来说并没有那么多的条条框框,其实它...

    Android代码-android_design_patterns_analysis

    Android源码设计模式分析开源项目 该系列文章已经重新深度整理并出版 该系列文章已经根据技术发展、实战需求以及读者您的反馈重写所有章节,并且加入更加深入的核心机制分析以及模式在Android开发中的实战,以便...

    高焕堂Android系列资源

    Android应用框架原理与程序开发_高...高焕堂设计招式之美(6个pdf文档) 高焕堂(33个word文档) 总之,涉及内容挺多的,包括框架、底层、模式。和android有关系。 想跟高焕堂学习的别吝啬分数了,资源多,分数当然贵喽

    android开发资料大全

    Android核心分析28篇,强烈推荐android初学者,android进阶者看看这个系列教程 Android应用开发者指南:性能优化 android开发教程合集(推荐新手看下这一季教程) 新手入门 会员贡献电子图书整理(内含PDF下载) ...

    新版Android开发教程.rar

    Android Android Android Android 盈利模式 Android 的 App Market 模式,软件开发者获得 7 成收入, 3 成用于系统维护。难点在于位置营销。 设备商通过卖设备、内置特色应用来获得盈利。也可以兼职专业软件开发者...

    design-patterns-sample:我们在VandyHacks 2015上的演示的代码示例。此存储库演示了如何在iOS,Android和Rails中应用和使用少量设计模式

    该存储库将演示如何将设计模式应用于iOS,Android和Rails框架,并使您对何时以及如何使用这些模式有更深入的了解。 设计模式 以下列表是设计模式的汇总,我们提供了相应的解释和代码示例。 辛格尔顿 参考: 意图 ...

    基于Android设计模式之–SDK源码之策略模式的详解

    策略模式其实特别简单(听到这句话,...下面,我们分析Android中的动画是如何使用策略模式的。 1. 意图定义一系列的算法,把它们一个个封装起来,并且使它们可互相替换。策略模式使得算法可独立于使用它的客户而变化

    android开发秘籍

    第11 章 android 高级开发技术 232 11.1 android 的自定义视图 232 11.2 android 的原生组件 238 11.3 android 的安全机制 241 11.4 android 的进程间通信 242 11.5 android 的备份管理器 247 11.5.1 秘诀95:...

Global site tag (gtag.js) - Google Analytics