Java工厂设计模式(简单计算器实现)

总体的目录结构如下

在这里插入图片描述
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>

&lt;meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=UTF-8&#34;&gt;

&lt;title&gt;页面&lt;/title&gt;

<body>

<form action="/calculate" method="post" >

numberA:&lt;input type=&#34;text&#34; size=&#34;30&#34; name=&#34;numberA&#34;&gt;&lt;br&gt;

numberB:&lt;input type=&#34;text&#34; size=&#34;30&#34; name=&#34;numberB&#34;&gt;&lt;br&gt;

&lt;select class=&#34;form-control&#34; name=&#34;operation&#34; id=&#34;state&#34;&gt;

    &lt;option value=&#34;+&#34;&gt;+&lt;/option&gt;

    &lt;option value=&#34;-&#34;&gt;-&lt;/option&gt;

    &lt;option value=&#34;*&#34;&gt;*&lt;/option&gt;

    &lt;option value=&#34;/&#34;&gt;/&lt;/option&gt;

&lt;/select&gt;&lt;input type=&#34;submit&#34; value=&#34;comfirm&#34;&gt;&lt;br&gt;

结果:&lt;input type=&#34;text&#34; value=&#34;${(result)!&#34;&#34;}&#34; readonly&gt;

</form>

</body>

</html>