总体的目录结构如下
public interface BaseFactory {
}
public class OperationFactory implements BaseFactory{
/**
* 工厂类的本质是帮助判断对应的实例,将这种判断放在工厂当中,那么就节省了让操作类自己来进行操作的性能判断,这是使用多态来解决父类和子类之间的差异
*
* @param operation
* @return
*/
public static Operation createOperation(String operation) {
Operation oper =null;
switch (operation) {
case "+":
oper =new OperationAddServiceImpl();
break;
case "-":
oper =new OperationSubServiceImpl();
break;
case "*":
oper =new OperationMulServiceimpl();
break;
case "/":
oper =new OperationDivServiceImpl();
break;
}
return oper;
}
}
public class Operation {
protected Double numberA;
protected Double numberB;
public Double getNumberA() {
return numberA;
}
public void setNumberA(Double numberA) {
this.numberA = numberA;
}
public Double getNumberB() {
return numberB;
}
public void setNumberB(Double numberB) {
this.numberB = numberB;
}
/**
* 子类当中复写了当前方法,所以会优先使用子类的方法,这就是一种多态的用法
*
* @return
*/
public Double getResult() {
Double result =0D;
return result;
}
}
public interface OperationAddService {
/**
* 两个数相加
*
* @return
*/
Double getResult();
}
public interface OperationDivService {
/**
* 两数相除得到的结果
*
* @return
*/
Double getResult();
}
public interface OperationMulService {
/**
* 两个数相乘的结果
*
* @return
*/
Double getResult();
}
public interface OperationSubService {
/**
* 两个数的减法
*
* @return
*/
Double getResult();
}
public class OperationAddServiceImplextends extends Operation implements OperationAddService {
@Override
public Double getResult() {
Double result =0D;
result =numberA +numberB ;
return result;
}
}
public class OperationDivServiceImplextends extends Operation implements OperationDivService {
@Override
public Double getResult() {
Double result =0D;
if (numberB ==0){
throw new LogicalException("除数不能为0");
}
result =numberA /numberB ;
return result;
}
}
public class OperationMulServiceimplextends extends Operation implements OperationMulService {
@Override
public Double getResult() {
Double result =0D ;
result =numberA *numberB ;
return result;
}
}
public class OperationSubServiceImplextends extends Operation implements OperationSubService {
@Override
public Double getResult() {
Double result =0D;
result =numberA -numberB ;
return result;
}
}
@Controller
public class OperationController {
@Autowired
private OperationServiceo perationService;
@RequestMapping("/index")
public String index(){
return "login";
}
@RequestMapping("/calculate")
public String operation(Model model,Double numberA , Double numberB , String operation){
Operation oper;
oper = OperationFactory.createOperation(operation);
oper.setNumberA(numberA);
oper.setNumberB(numberB);
model.addAttribute("result",oper.getResult());
return "login";
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<body>
<form action="/calculate" method="post" >
numberA:<input type="text" size="30" name="numberA"><br>
numberB:<input type="text" size="30" name="numberB"><br>
<select class="form-control" name="operation" id="state">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select><input type="submit" value="comfirm"><br>
结果:<input type="text" value="${(result)!""}" readonly>
</form>
</body>
</html>