#策略设计模式,就是对同一个算法进行不同的封装,由客户端采用选取不同的策略
abstract class AbstractStrategy implements IStrategy{
public int[] split(String reg,String splot){
String[] strings=reg.split(splot);
int[] result=new int[2];
result[0]=Integer.parseInt(strings[0]);
result[1]=Integer.parseInt(strings[1]);
return result;
}
}
class Plus extends AbstractStrategy implements IStrategy{
@Override
public int split(String string){
int[] result=split(string,"\\+");
return result[0]+result[1];
}
}
class Mutipluy extends AbstractStrategy implements IStrategy{
@Override
public int split(String string){
int[] result=split(string,"\\*");
return result[0]*result[1];
}
}
本文介绍了一种称为策略设计模式的方法,该模式允许客户端在运行时选择不同的算法或策略。通过抽象基类AbstractStrategy定义了一个通用的算法框架,并通过具体子类如Pluse和Mutipluy实现了加法和乘法的不同策略。
493

被折叠的 条评论
为什么被折叠?



