A detailed explanation of the strategy pattern of--SDK source code based on the Android design mode _android

Source: Internet
Author: User

The strategy model is actually very simple (to hear this sentence, people are not in the heart suddenly relaxed? )。
For example, the official tells everyone I have here a sort of interface isort the sort () method, and then the folk do their best to achieve this sort of method: bubbling, fast, heap and so on.
These methods are "different strategies".
Then, under a module, you need a sort method, but you can't specify a specific sort method (for extended consideration), you need to use the Isort interface.
Finally, exactly what scene, pass into what specific sort method, realize flexible sort.
This is the Strategy mode!
Below, we analyze how the animation in Android uses the policy mode.

1. Intent
Define a series of algorithms, encapsulate them all, and make them interchangeable.
The policy pattern allows the algorithm to vary independently from the customer who uses it.

2. Structure diagram and code
Animation The realization of different animation, mainly rely on the different implementation of interpolator and change.

Define Interface Interpolator:

Copy Code code as follows:

Package android.animation;

/**
* A Time Interpolator defines the rate's change of A 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 a animation to a value that represents
* The interpolated fraction. This interpolated value are then multiplied by the
* 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
* In the animation where 0 represents the start and 1.0 represents
* The end
* @return the interpolation value. This value can is more than 1.0 for
* Interpolators which overshoot their targets, or less than 0 for
* Interpolators that undershoot their targets.
*/
float getinterpolation (float input);

We take accelerateinterpolator as an example to implement a specific strategy, the code is as follows:
Copy Code code 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 is 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);
}
}
}

The other interpolator implementations are not listed here.
How do you implement different animations in the animation module?
Here I want to mention a very broad concept: dependency injection.
To implement different animations in the animation module, we need to inject each interpolator in the form of a parent class or interface.
The injection method is typically a constructor, set method, annotation, and so on.
Let's see what the animation class does:
Copy Code code as follows:

Public abstract class Animation implements Cloneable {
Interpolator Minterpolator;
Injected through the Set method
public void Setinterpolator (Interpolator i) {
Minterpolator = i;
}

public boolean gettransformation (long currenttime, transformation outtransformation) {
// ... ...
Specific call
Final float interpolatedtime = minterpolator.getinterpolation (normalizedtime);
Applytransformation (Interpolatedtime, outtransformation);
// ... ...
}

The default implementation, is a trick, by the way, this is not the point
protected void Ensureinterpolator () {
if (Minterpolator = = null) {
Minterpolator = new Acceleratedecelerateinterpolator ();
}
}

}

The strategy model is actually a manifestation of the polymorphism of a dripping delicate.

3. Effect
(1). Behavioral pattern
(2). Eliminate some if...else ... The conditional statement
(3). Customers can choose the implementation, but the customer must understand the implementation of this different strategy (this sentence seems to be nonsense, in short, customers need to learn costs)
(4). The default implementation is mentioned in the code comment, which enables the customer not to understand the policy or to implement the default policy
(5). There are several ways to inject: constructors, set methods, comments. Configuration resolution, and so on

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.