总体的目录结构如下
1.基础工厂类
代码语言:javascript
复制
public interface BaseFactory {
}
2.拓展工厂类
代码语言:javascript
复制
public class OperationFactoryimplements 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; }
}
3.基础操作类
代码语言:javascript
复制
public class Operation {
protected DoublenumberA; protected DoublenumberB; 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; }
}
4.加法Service
代码语言:javascript
复制
public interface OperationAddService {
/** * 两个数相加 * * @return */ Double getResult();
}
5.除法Service
代码语言:javascript
复制
public interface OperationDivService {
/** * 两数相除得到的结果 * * @return */ Double getResult();
}
6.乘法Service
代码语言:javascript
复制
public interface OperationMulService {
/** * 两个数相乘的结果 * * @return */ Double getResult();
}
7.减法Service
代码语言:javascript
复制
public interface OperationSubService {
/** * 两个数的减法 * * @return */ Double getResult();
}
8.加法ServiceImpl
代码语言:javascript
复制
public class OperationAddServiceImplextends Operationimplements OperationAddService {
@Override public Double getResult() { Double result =0D; result =numberA +numberB ; return result; }
}
9.除法ServiceImpl
代码语言:javascript
复制
public class OperationDivServiceImplextends Operationimplements OperationDivService {
@Override public Double getResult() { Double result =0D; if (numberB ==0){ throw new LogicalException("除数不能为0"); } result =numberA /numberB ; return result; }
}
10.减法ServiceImpl
代码语言:javascript
复制
public class OperationSubServiceImplextends Operationimplements OperationSubService {
@Override public Double getResult() { Double result =0D; result =numberA -numberB ; return result; }
}
11.Controller层
代码语言:javascript
复制
@Controller
public class OperationController {@Autowired OperationServiceoperationService; @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"; }
}
12.使用freemarker来作为前端展示
代码语言:javascript
复制
<!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>