[color=red]【策略模式】定义一系列算法,把他们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化[/color]
package com.demo.strategy;
import java.util.Arrays;
public class StrategyThree implements ComputableStrategy {
@Override
public double computableScore(double[] a) {
// TODO Auto-generated method stub
if(a.length<=2)
return 0;
double sum=0;
Arrays.sort(a);
for(int i=1;i<a.length-1;i++){
sum=sum+a[i];
}
return sum/(a.length-2);
}
}
package com.demo.strategy;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
double a[]={9.12,9.15,8.87,9.99,7.96,7.38};
double b[]={8.72,8.95,9.87,8.99,8.26,8.38};
//上下文对象
GymnasticsGame game=new GymnasticsGame();
//上下文对象使用统一策略
game.setStrategy(new StrategyOne());
System.out.println(game.getPersonScore(a));
System.out.println(game.getPersonScore(b));
System.out.println("-----------------");
game.setStrategy(new StrategyTwo());
System.out.println(game.getPersonScore(a));
System.out.println(game.getPersonScore(b));
System.out.println("-----------------");
game.setStrategy(new StrategyThree());
System.out.println(game.getPersonScore(a));
System.out.println(game.getPersonScore(b));
}
}
8.745000000000001
8.861666666666666
-----------------
8.702534326503933
8.84653917642559
-----------------
8.774999999999999
8.76