1、枚举 2、工厂模式 3、策略模式
public class TestOne {
public static void main(String[] args) {
Double a = 2d;
Double b = 1d;
//ADD:加法。SUB:减法。MUL:乘法。DIV:除法 ...
String flag = "ADD";
if("ADD".equals(flag)){
System.out.println("加法计算结果:"+(double)(a+b));
}else if("SUB".equals(flag)){
System.out.println("减法计算结果:"+(double)(a-b));
}
}
}
第一步,首先定义一个公用接口 CalculateOperation,表示不同计算方法所能做的操作:
public interface CalculateOperation {
public void doCalculate(Double a, Double b);
}
第二步,接下来我们将不同计算方法全部交由枚举类来做,定义一个枚举类
public enum CalculateEnum implements CalculateOperation{
ADD{
@Override
public void doCalculate(Double a, Double b) {
System.out.println("加法的计算结果:"+(double)(a+b));
}
},
SUB{
@Override
public void doCalculate(Double a, Double b) {
System.out.println("加法的计算结果:"+(double)(a-b));
}
}
}
第三步,创建测试类
public class TestTwo {
public static void main(String[] args) {
Double a = 2d;
Double b = 1d;
String flag = "ADD";
CalculateEnum.valueOf(flag).doCalculate(a,b);
}
}
第一步,创建加法实现类
public class Add implements CalculateOperation{
@Override
public void doCalculate(Double a, Double b) {
System.out.println("加法计算结果:"+(Double)(a+b));
}
}
第二步,创建加法实现类
public class Sub implements CalculateOperation{
@Override
public void doCalculate(Double a, Double b) {
System.out.println("减法计算结果:"+(Double)(a-b));
}
}
第三步,创建工厂类
public class CalculateFactory {
static Map<String,CalculateOperation> calculateOperationMap = new HashMap<>();
static {
calculateOperationMap.put("ADD",new Add());
calculateOperationMap.put("SUB",new Sub());
}
public static CalculateOperation getObj(String calculateName){
return calculateOperationMap.get(calculateName);
}
}
第四步,创建测试类
public class TestThree {
public static void main(String[] args) {
Double a = 2d;
Double b = 1d;
String flag = "ADD";
CalculateFactory.getObj(flag).doCalculate(a,b);
}
}
这样的话以后想扩展条件也很容易,只需要增加新代码,而不需要动以前的业务代码,非常符合“开闭原则”。
第一步,公用接口 CalculateOperation添加support方法。
public interface CalculateOperation {
public default boolean support(String name) {
return false;
}
public void doCalculate(Double a, Double b);
}
第二步,add 和 sub实现类添加support方法。
public class Add implements CalculateOperation{
@Override
public boolean support(String name) {
return "ADD".equals(name);
}
@Override
public void doCalculate(Double a, Double b) {
System.out.println("加法计算结果:"+(Double)(a+b));
}
}
public class Sub implements CalculateOperation{
@Override
public boolean support(String name) {
return "SUB".equals(name);
}
@Override
public void doCalculate(Double a, Double b) {
System.out.println("减法计算结果:"+(Double)(a-b));
}
}
第三步,添加calculation上下文类
public class CalculateContext2 {
private CalculateOperation calculateOperation;
public CalculateContext2(CalculateOperation calculateOperation){
this.calculateOperation=calculateOperation;
}
public void doExec(Double a ,Double b){
System.out.println("计算前");
calculateOperation.doCalculate(a,b);
System.out.println("计算后");
}
}
第四步,添加测试类
public class TestSix {
public static void main(String[] args) {
Double a = 2d;
Double b = 1d;
String flag = "ADD";
List<CalculateOperation> calculateOperations = new ArrayList<>();
calculateOperations.add(new Add());
calculateOperations.add(new Sub());
for(CalculateOperation item:calculateOperations){
if(item.support(flag)){
new CalculateContext2(item).doExec(a,b);
}
}
}
}