设计模式之策略模式及源码中的应用

策略模式

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

使用场景:

  1. 针对同一类型问题的多种处理方式,仅仅是具体行为有差别时。
  2. 需要安全地封装多种同一类型的操作时。
  3. 出现同一抽象类有多个子类,而又需要使用if-else或者switch-case来选择具体子类时。

使用栗子:
计算图书价格,初级会员没有折扣,中级会员打9折,高级会员打8折。如果一般写法,应该是if-else判断他是什么级别的会员,在计算相应的折扣。下面使用策略模式来进行实现:
抽象折扣类

1
2
3
4
5
6
7
8
public interface MemberStrategy {
/**
* 计算图书的价格
* @param booksPrice 图书的原价
* @return 计算出打折后的价格
*/
public double calcPrice(double booksPrice);
}

初级会员折扣类

1
2
3
4
5
6
7
8
9
10
public class PrimaryMemberStrategy implements MemberStrategy{
/**
* 初级会员折扣
*/
@Override
public double calcPrice(double booksPrice) {
System.out.println("对于初级会员的没有折扣");
return booksPrice;
}
}

中级会员折扣类

1
2
3
4
5
6
7
8
9
10
public class IntermediateMemberStrategy implements MemberStrategy{
/**
* 中级会员折扣
*/
@Override
public double calcPrice(double booksPrice) {
System.out.println("对于中级会员的折扣为10%");
return booksPrice * 0.9;
}
}

高级会员折扣类

1
2
3
4
5
6
7
8
9
10
public class AdvancedMemberStrategy implements MemberStrategy{
/**
* 高级会员折扣
*/
@Override
public double calcPrice(double booksPrice) {
System.out.println("对于高级会员的折扣为20%");
return booksPrice * 0.8;
}
}

价格类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Price {
//持有一个具体的策略对象
private MemberStrategy strategy;
/**
* 构造函数,传入一个具体的策略对象
* @param strategy 具体的策略对象
*/
public Price(MemberStrategy strategy){
this.strategy = strategy;
}
/**
* 计算图书的价格
* @param booksPrice 图书的原价
* @return 计算出打折后的价格
*/
public double quote(double booksPrice){
return this.strategy.calcPrice(booksPrice);
}
}

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Client {
public static void main(String[] args) {
//选择并创建需要使用的策略对象
MemberStrategy strategy1 = new AdvancedMemberStrategy();
//创建环境
Price price = new Price(strategy1);
//计算价格
double quote = price.quote(300);
System.out.println("图书的最终价格为:" + quote);
}
}

结果

1
2
对于高级会员的折扣为20%
图书的最终价格为:240.0

策略模式和工厂模式的区别

工厂模式 策略模式
创建型的设计模式 行为型的设计模式
关注对象创建 关注行为的选择
黑盒子(无需知道具体的实现过程) 白盒子(知道具体的实现过程)

源码中的策略模式实现

ListAdapter

ListView 是一个很重要的组件,我们通常在布局里写个 ListView 组件,然后在代码中 setAdapter,把 View 与 Model 结合的任务交给了 Adapter。

当更换 Adapter 的具体实现时,仍然调用的是 ListView.setAdapter(…) 方法,传入的是ArrayAdapter或BaseAdapter等,查看 ListView 源码,发现 setAdapter 方法的参数是一个 ListAdapter,如下:

1
2
3
4
@Override
public void setAdapter(ListAdapter adapter) {
........
}

1
2
3
public interface ListAdapter extends Adapter{
.........
}

可以看到 ListAdapter 是一个接口,ArrayAdapter 和 BaseAdapter 是它的一个实现类。

可以发现 ListAdapter 就是 strategy 接口,ArrayAdpater 等就是具体的实现类,而在 ListView 中引用的是 接口 ListAdapter,可以证实这就是一个 策略模式 的使用。

TimeInterpolator

时间插值器,它是一个接口,定义了动画改变的速率,允许动画进行非匀速变化。
我们在使用属性动画时,可以根据需要选择合适的时间插值器:

1
2
3
ObjectAnimator animator = ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f);
animator.setInterpolator(new AccelerateInterpolator()); //加速
animator.setInterpolator(new OvershootInterpolator()); //跑过头又返回来

和 ListView 的 setAdapter 一样,ValueAnimator 的 setInterpolator 方法中也引用的是 接口 TimeInterpolator:

1
2
3
4
5
6
7
8
@Override
public void setInterpolator(TimeInterpolator value) {
if (value != null) {
mInterpolator = value;
} else {
mInterpolator = new LinearInterpolator();
}
}

TimeInterpolator 源码及类结构:

1
2
3
4
public interface TimeInterpolator {
float getInterpolation(float input);
}

因此这里也是应用了策略模式。。。