设计模式-工厂模式

2019-10-26 20:46:21 浏览数 (1)

1.目录结构如下

总体的目录结构如下

2.第一步是有一个基础的工厂类,这个工厂类可以定义一些基础的工厂方法,可以用于多实现
代码语言:javascript复制
    public interface BaseFactory {

    }
2.一个基本操作类实现该接口
代码语言:javascript复制
    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;
        }
    }
3.具体的操作类
代码语言:javascript复制
    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;
        }
    }
4.需要实现的操作服务>加法服务
代码语言:javascript复制
    public interface OperationAddService {
        /**
         * 两个数相加
         *
         * @return
         */
        Double getResult();
    }
5.除法相关服务
代码语言:javascript复制
    public interface OperationDivService {
        /**
         * 两数相除得到的结果
         *
         * @return
         */
        Double getResult();
    }
6.乘法相关服务
代码语言:javascript复制
    public interface OperationMulService {
        /**
         * 两个数相乘的结果
         *
         * @return
         */
        Double getResult();
    }
7.减法相关服务
代码语言:javascript复制
    public interface OperationSubService {
        /**
         * 两个数的减法
         *
         * @return
         */
        Double getResult();
    }
8.加法服务实现类
代码语言:javascript复制
    public class OperationAddServiceImplextends extends Operation implements OperationAddService {
        @Override
        public Double getResult() {
            Double result =0D;
            result =numberA  numberB ;
            return result;
        }
    }
9.除法相关服务实现类
代码语言:javascript复制
    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;
        }
    }
10.乘法相关服务实现类
代码语言:javascript复制
    public class OperationMulServiceimplextends extends Operation implements OperationMulService {
        @Override
        public Double getResult() {
            Double result =0D ;
            result =numberA *numberB ;
            return result;
        }
    }
11.减法相关服务实现类
代码语言:javascript复制
    public class OperationSubServiceImplextends extends Operation implements OperationSubService {

        @Override
        public Double getResult() {
            Double result =0D;
            result =numberA -numberB ;
            return result;
        }
    }
12.Controller层
代码语言:javascript复制
    @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";
        }
    }
13.使用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>

0 人点赞