大話設計模式一:簡單工廠模式(計算器)

jopen 10年前發布 | 25K 次閱讀 設計模式

        定義Operation抽象類,將各種操作解耦為各個類并實現Operation抽象類,這樣可以降低了各種具體操作代碼耦合性。總體來說,定義一個抽象類,然后若干類繼承抽象類,實現抽象方法,工廠會根據需要生成各種子類對象(多態)。

    package simple_factory;  

    public abstract class Operation {  
        private double numberA = 0;  
        private double numberB = 0;  
        public double getNumberA() {  
            return numberA;  
        }  
        public double getNumberB() {  
            return numberB;  
        }  
        public void setNumberA(double numberA) {  
            this.numberA = numberA;  
        }  
        public void setNumberB(double numberB) {  
            this.numberB = numberB;  
        }  
        public abstract double GetResult() throws Exception;  

        public static void main(String[] args) throws Exception {  
            // TODO Auto-generated method stub  
            Operation oper;  
            oper = OperationFactory.createOperation("+");  
            oper.setNumberA(1);  
            oper.setNumberB(2);  
            double result = oper.GetResult();  
            System.out.println(result);  
        }  
    }  

    class OperationAdd extends Operation {  
        @Override  
        public double GetResult() {  
            double result = 0;  
            result = this.getNumberA() + this.getNumberB();  
            return result;  
        }  
    }  

    class OperationSub extends Operation {  
        @Override  
        public double GetResult() {  
            double result = 0;  
            result = this.getNumberA() - this.getNumberB();  
            return result;  
        }  
    }  

    class OperationMul extends Operation {  
        @Override  
        public double GetResult() {  
            double result = 0;  
            result = this.getNumberA() * this.getNumberB();  
            return result;  
        }  
    }  

    class OperationDiv extends Operation {  
        @Override  
        public double GetResult() throws Exception {  
            double result = 0;  
            if (this.getNumberB() == 0)  
                throw new Exception("除數不能為0");  
            result = this.getNumberA() / this.getNumberB();  
            return result;  
        }  
    }  

    class OperationFactory {  
        public static Operation createOperation(String operate) {  
            Operation oper = null;  
            switch (operate)  
            {  
                case "+":  
                    oper = new OperationAdd();  
                    break;  
                case "-":  
                    oper = new OperationSub();  
                    break;  
                case "*":  
                    oper = new OperationMul();  
                    break;  
                case "/":  
                    oper = new OperationDiv();  
                    break;  
            }  
            return oper;  
        }  
    }  
     用一個單獨的類來做創建實例的過程,這就是工廠。如果現在還需要增加各種復雜運算,比如平方根,只需要增加運算子類繼承Operation抽象類,同時修改運算類工廠。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!