NotOnlyJava

http://www.ibm.com/developerworks/cn/java/j-lo-serial/
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JAVA计算器的良好实现

Posted on 2013-06-28 17:14  NotOnlyJava  阅读(267)  评论(0编辑  收藏  举报
  1 //接口
  2 package com.czp;
  3 
  4 import java.math.BigDecimal;
  5 
  6 public interface IOperator {
  7 
  8     BigDecimal operator(BigDecimal n1, BigDecimal n2);
  9     
 10     BigDecimal operator(int n1, int n2);
 11     
 12     BigDecimal operator(double n1, double n2);
 13 }
 14 //工厂类
 15 
 16 package com.czp;
 17 
 18 import java.math.BigDecimal;
 19 
 20 
 21 /**
 22  * 获取操作的工厂类
 23  * 
 24  * @author Czp
 25  * @version 1.0 (2013-6-28)
 26  */
 27 public class OperatorFatory {
 28 
 29     private static final OperatorFatory FATORY = new OperatorFatory();
 30 
 31     private IOperator add;
 32 
 33     private IOperator sub;
 34 
 35     private IOperator mul;
 36 
 37     private IOperator div;
 38 
 39     private OperatorFatory() {
 40         add = new AddOperator();
 41         sub = new SubOperator();
 42         mul = new MulOperator();
 43         div = new DivOperator();
 44     }
 45 
 46     /**
 47      * 操作的类型
 48      * 
 49      * @author Czp
 50      * @version 1.0 (2013-6-28)
 51      */
 52     public enum TYPE {
 53         ADD("+"), SUB("-"), MUL("*"), DIV("/");
 54         
 55         String type;
 56 
 57         private TYPE(String type) {
 58             this.type = type;
 59         }
 60         
 61         static TYPE fromString(String str)
 62         {
 63             TYPE[] tps = TYPE.values();
 64             for (TYPE type : tps) {
 65                 if(type.type.equals(str.trim()))
 66                 {
 67                     return type;
 68                 }
 69             }
 70             return null;
 71         }
 72         
 73     }
 74 
 75     /**
 76      * 获取工厂的实例
 77      * 
 78      * @return
 79      */
 80     public static OperatorFatory getInstance() {
 81         return FATORY;
 82     }
 83 
 84     /**
 85      * operator的工厂方法
 86      * 
 87      * @param type
 88      * @return
 89      */
 90     public IOperator getOperator(TYPE type) {
 91         switch (type) {
 92         case ADD:
 93             return add;
 94         case SUB:
 95             return sub;
 96         case MUL:
 97             return mul;
 98         case DIV:
 99             return div;
100         }
101         return null;
102     }
103     /**
104      * 通过字符串获取操作类
105      * 
106      * @param type
107      * @return
108      */
109     public IOperator getOperator(String type) {
110         return getOperator(TYPE.fromString(type));
111     }
112     
113     /************** 以下是+-x/的具体的实现 ***********************/
114 
115     private static abstract class AbstractOperator implements IOperator {
116 
117         @Override
118         public abstract BigDecimal operator(BigDecimal n1, BigDecimal n2);
119 
120         @Override
121         public BigDecimal operator(int n1, int n2) {
122             return operator(BigDecimal.valueOf(n1), BigDecimal.valueOf(n2));
123         }
124 
125         @Override
126         public BigDecimal operator(double n1, double n2) {
127             return operator(BigDecimal.valueOf(n1), BigDecimal.valueOf(n2));
128         }
129 
130     }
131 
132     private static class AddOperator extends AbstractOperator {
133 
134         @Override
135         public BigDecimal operator(BigDecimal n1, BigDecimal n2) {
136             return n1.add(n2);
137         }
138 
139     }
140 
141     private static class DivOperator extends AbstractOperator {
142 
143         private static final BigDecimal ZERO = new BigDecimal(0);
144 
145         @Override
146         public BigDecimal operator(BigDecimal n1, BigDecimal n2) {
147             if (n2.compareTo(ZERO) == 0)
148                 throw new RuntimeException("second number is zero");
149             return n1.divide(n2);
150         }
151 
152     }
153 
154     private static class SubOperator extends AbstractOperator {
155 
156         @Override
157         public BigDecimal operator(BigDecimal n1, BigDecimal n2) {
158             return n1.subtract(n2);
159         }
160 
161     }
162 
163     private static class MulOperator extends AbstractOperator {
164 
165         @Override
166         public BigDecimal operator(BigDecimal n1, BigDecimal n2) {
167             return n1.multiply(n2);
168         }
169 
170     }
171 }