Android Design Pattern series (11)-policy pattern of SDK source code

Source: Internet
Author: User

The rule mode is actually very simple (Do you feel relaxed when you hear this sentence ?).
For example, the official team will tell you that there is an ISort sort () method for sorting, and then the folks will do their best to achieve this sort method: Bubble, fast, heap, and so on.
These methods are "different policies ".
Then, a sorting method is required for a module, but the specific sort method cannot be specified for the time being (for extension reasons). The ISort interface is required.
Finally, the specific sort method is introduced in specific scenarios to Achieve flexible sorting.
This is the rule mode!
Next, we will analyze how the Android animation uses the policy mode.

1. Intention
Define a series of algorithms, encapsulate them one by one, and make them replaceable.
The policy mode allows algorithms to change independently of customers who use it.

2. structure chart and code
Animation implementations vary depending on different implementations of Interpolator.

Define the interface 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);}

Take AccelerateInterpolator as an example to implement the specific policy. The Code is as follows:

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);        }    }}

Other Interpolator implementations are not listed here.
How to implement different animations in the Animation module?
Here I would like to introduce a very broad concept: dependency injection.
Different animations are implemented in the Animation module, that is, we need to inject each Interpolator in the form of a parent class or interface.
Injection methods are usually constructor, set method, annotation, and so on.
Let's take a look at how the animation class works:

Public abstract class Animation implements Cloneable {Interpolator mInterpolator; // inject public void setInterpolator (Interpolator I) {mInterpolator = I;} public boolean getTransformation (long currentTime, Transformation outtranstransformation) through the set Method) {//...... // call final float interpolatedTime = mInterpolator. getInterpolation (normalizedTime); applyTransformation (interpolatedTime, outTransformation );//......} // The default implementation is a trick. By the way, this is not the focus of protected void ensureInterpolator () {if (mInterpolator = null) {mInterpolator = new AccelerateDecelerateInterpolator ();}}}

The rule pattern is actually an exquisite embodiment of polymorphism.

3. Results
(1). Behavior Model
(2). Some conditional statements of if... else... are eliminated.
(3) The customer can select the implementation, but the customer must understand the implementation of this different strategy (this sentence seems nonsense, in short, the customer needs to learn the cost)
(4). the default implementation is mentioned in the code comment, so that the customer can not understand the policy, but also implement the Default policy.
(5) There are multiple injection methods: constructor, set method, and annotation. Configuration parsing, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.