策略模式

作者: 噬魂Miss | 来源:发表于2017-08-25 11:37 被阅读47次
smile

大家好:我叫石头!

为什么出现策略模式:

完成一项任务,往往可以有多种不同的方式,每一种方式称为一个策略,我们可以根据环境或者条件的不同选择不同的策略来完成该项任务。
---对象行为型模式。

优点:

  • 策略模式提供了对“开闭原则”的完美支持,用户可以在不修改原有系统的基础上选择算法或行为,也可以灵活地增加新的算法或行为。
  • 策略模式提供了管理相关的算法族的办法。策略模式提供了可以替换继承关系的办法。
  • 使用策略模式可以避免使用多重条件转移语句。

结构图:

策略模式包含如下角色:

Context: 环境类
Strategy: 抽象策略类
ConcreteStrategy: 具体策略类

UML
策略模式代码实现
# Strategy: 抽象策略类
interface IStrategy {
        public void doSomething();
    }
    class ConcreteStrategy1 implements IStrategy {
        public void doSomething() {
            System.out.println("具体策略1");
        }
    }
    class ConcreteStrategy2 implements IStrategy {
        public void doSomething() {
            System.out.println("具体策略2");
        }
    }
    class Context {
        private IStrategy strategy;

        public Context(IStrategy strategy){
            this.strategy = strategy;
        }

        public void execute(){
            strategy.doSomething();
        }
    }

    public class Client {
        public static void main(String[] args){
            Context context;
            System.out.println("-----执行策略1-----");
            context = new Context(new ConcreteStrategy1());
            context.execute();

            System.out.println("-----执行策略2-----");
            context = new Context(new ConcreteStrategy2());
            context.execute();
        }
    }

背后的本质原理:
在面向对象设计(Object-Oriented DesignOOD),其实体现的是继承跟多态,

  • 几个类的主要逻辑相同,只在部分逻辑的算法和行为上稍有区别的情况。
  • 有几种相似的行为,或者说算法,客户端需要动态地决定使用哪一种,那么可以使用策略模式,将这些算法封装起来供客户端调用。

一般来说,策略模式不会单独使用,跟模版方法模式、工厂模式等混合使用的情况比较多。

在android中的应用:

1.adapter anim.setInterpolator(new AccelerateInterpolator(2f));
2.属性动画
3.volley

在属性动画中,有一个东西叫做插值器,它的作用就是根据时间流逝的百分比来来计算出当前属性值改变的百分比.

Context: 环境类

private static final TimeInterpolator sDefaultInterpolator =
        new AccelerateDecelerateInterpolator();  
private TimeInterpolator mInterpolator = sDefaultInterpolator; 
@Override
public void setInterpolator(TimeInterpolator value) {
    if (value != null) {
        mInterpolator = value;
    } else {
        mInterpolator = new LinearInterpolator();
    }
}

@Override
public TimeInterpolator getInterpolator() {
    return mInterpolator;
}
Strategy: 抽象策略类
public interface Interpolator extends TimeInterpolator {
    // A new interface, TimeInterpolator, was introduced for the new android.animation
    // package. This older Interpolator interface extends TimeInterpolator so that users of
    // the new Animator-based animations can use either the old Interpolator implementations or
    // new classes that implement TimeInterpolator directly.
}
BaseInterpolator插值器实现了Interpolator接口,并且是一个抽象类
abstract public class BaseInterpolator implements Interpolator {
    private int mChangingConfiguration;
    /**
     * @hide
     */
    public int getChangingConfiguration() {
        return mChangingConfiguration;
    }

    /**
     * @hide
     */
    void setChangingConfiguration(int changingConfiguration) {
        mChangingConfiguration = changingConfiguration;
    }
}  

为什么还要封装成一层抽象类呢?
方便抽取一些公共的实现跟必须的一些实现.
ConcreteStrategy: 具体策略类
线性插值器
public class LinearInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

    public LinearInterpolator() {
    }

    public LinearInterpolator(Context context, AttributeSet attrs) {
    }

    public float getInterpolation(float input) {
        return input;
    }

    /** @hide */
    @Override
    public long createNativeInterpolator() {
        return NativeInterpolatorFactoryHelper.createLinearInterpolator();
    }
}
加减速插值器
public class AccelerateDecelerateInterpolator extends BaseInterpolator
        implements NativeInterpolatorFactory {
    public AccelerateDecelerateInterpolator() {
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public AccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {
    }

    public float getInterpolation(float input) {
        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
    }

    /** @hide */
    @Override
    public long createNativeInterpolator() {
        return NativeInterpolatorFactoryHelper.createAccelerateDecelerateInterpolator();
    }
}

随便说一句:
各种设计模式的出现都是为了提高代码的健壮性等.

Volley实现
public interface RetryPolicy {


    public int getCurrentTimeout();//获取当前请求用时(用于 Log)


    public int getCurrentRetryCount();//获取已经重试的次数(用于 Log)


    public void retry(VolleyError error) throws VolleyError;//确定是否重试,参数为这次异常的具体信息。在请求异常时此接口会被调用,可在此函数实现中抛出传入的异常表示停止重试。
}
默认实现
public class DefaultRetryPolicy implements RetryPolicy {
    ...
}
public abstract class Request<T> implements Comparable<Request<T>> {
    private RetryPolicy mRetryPolicy;
    public Request<?> setRetryPolicy(RetryPolicy retryPolicy) {
        mRetryPolicy = retryPolicy;
        return this;
    }
    public RetryPolicy getRetryPolicy() {
        return mRetryPolicy;
    }
}

各大网络请求框架,或多或少都会使用到缓存,缓存一般会定义一个Cache接口,然后实现不同的缓存策略,如内存缓存,磁盘缓存等等,这个缓存的实现,其实也可以使用策略模式。直接看Volley,里面也有缓存。

策略模式在MVP中的实现:

  • 提示:Context类不一定只有一个algorithm(),只要是持有interface接口的类都能当做Context

我们常使用的MVP模式中的Presenter层就是对策略模式的实现,一个View在不同的条件下,可以对应不同的Presenter层。

MVP中的策略模式

iOS设计模式-策略模式

相关文章

网友评论

    本文标题:策略模式

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