Android进阶之路设计模式架构算法设计模式和编程理论

策略设计模式

2019-08-17  本文已影响8人  OneXzgj

1.定义:

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

2.使用场景

3.UML建模图

image.png

Context:用来操作策略的上下文环境。
Stragety:策略的抽象。
ConcreteStragetyA、ConcreteStragetyB:具体的策略实现。

4.策略模式的简单实现

以选择旅游出行方式为例,假如是2公里之内,则选择自行车,如果是10公里之内,则选择公交汽车,如果是10公里以外,则最好自驾车,比较的方便。


public interface TravelWays {

    /**
     * 选择交通工具
     */
    void selectTransportation();

}
public class BicycleWay implements TravelWays {
    @Override
    public void selectTransportation() {
        System.out.println("选择了自行车出行方式");
    }
}
public class BusWay implements TravelWays {
    @Override
    public void selectTransportation() {
        System.out.println("选择了公交出行");
    }
}
public class CarWay implements TravelWays {
    @Override
    public void selectTransportation() {
        System.out.println("选择了自驾出行方式");
    }
}
public class Context {

    private TravelWays travelWays;

    public Context(TravelWays travelWays) {
        this.travelWays = travelWays;
    }

    public void selectTransportation(){
        travelWays.selectTransportation();
    }
}
public class Client {

    public static void main(String[] args) {

        Context context;

        //短途选择自行车
        TravelWays bicycle=new BicycleWay();
        context=new Context(bicycle);
        context.selectTransportation();
        
        //中短途选择公交
        TravelWays bus=new BusWay();
        context=new Context(bus);
        context.selectTransportation();
        
        //远程选择小汽车
        TravelWays car=new CarWay();
        context=new Context(car);
        context.selectTransportation();
        
    }
}

5.优点缺点比较

优点

缺点

6.在Android中的使用

在Android中,插值器的实现是通过策略设计模式实现,

TimeInterpolator为策略接口,LinearInterpolator、AccelerateInterpolator为具体的实现类。

public interface TimeInterpolator {

    /**
    *
     * @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);
}

public class LinearInterpolator extends TimeInterpolator  {

    public LinearInterpolator() {
    }

    //...

    public float getInterpolation(float input) {
        return input;
    }
    //...
}
@HasNativeInterpolator
public class AccelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
    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) {
        this(context.getResources(), context.getTheme(), attrs);
    }

    /** @hide */
    public AccelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;
        if (theme != null) {
            a = theme.obtainStyledAttributes(attrs, R.styleable.AccelerateInterpolator, 0, 0);
        } else {
            a = res.obtainAttributes(attrs, R.styleable.AccelerateInterpolator);
        }

        mFactor = a.getFloat(R.styleable.AccelerateInterpolator_factor, 1.0f);
        mDoubleFactor = 2 * mFactor;
        setChangingConfiguration(a.getChangingConfigurations());
        a.recycle();
    }

    public float getInterpolation(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float)Math.pow(input, mDoubleFactor);
        }
    }

 
}

上一篇下一篇

猜你喜欢

热点阅读