/**
            *簡單工廠模式其實不是23種設(shè)計模式之一,但是它是創(chuàng)建型模式的最基礎(chǔ)的內(nèi)容,
            *工廠方法和抽象工廠都是在它的基礎(chǔ)上的改良版。
            *關(guān)于簡單工廠模式的優(yōu)缺點,請看設(shè)計模式(1)------工廠類設(shè)計模式。
            *
            *
            *它是對對象實例化的過程和需要實例化的對象進行細(xì)節(jié)上的封裝。
            *
            *  優(yōu)點:工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動態(tài)實例化相關(guān)類。對于客戶端來說,去除了與具體產(chǎn)品的依賴。

            *缺點:當(dāng)需要增加產(chǎn)生一個新對象實例的方法時,需要修改工廠類,這樣,工廠類就違背了“開-閉原則”。
            *
            */


            1package simpleFactroy;
            2
            3public class Operation  {
            4    private double dNumberA;
            5    private double dNumberB;
            6    
            7    public double getResults(){
            8        return 0l ;
            9    }

           10
           11    public double getdNumberA() {
           12        return dNumberA;
           13    }

           14
           15    public void setdNumberA(double dNumberA) {
           16        this.dNumberA = dNumberA;
           17    }

           18
           19    public double getdNumberB() {
           20        return dNumberB;
           21    }

           22
           23    public void setdNumberB(double dNumberB) {
           24        this.dNumberB = dNumberB;
           25    }

           26
           27    public Operation(double dNumberA, double dNumberB) {
           28        this.dNumberA = dNumberA;
           29        this.dNumberB = dNumberB;
           30    }

           31
           32    public Operation(double dNumberA) {
           33        this.dNumberA = dNumberA;
           34    }

           35
           36    public Operation() {
           37        
           38    }

           39    
           40
           41}

           42
           43
           44package simpleFactroy;
           45
           46public class OperationAdd extends Operation {
           47    public double getResults() {
           48        return  getdNumberA() + getdNumberB() ;
           49    }

           50}

           51
           52
           53package simpleFactroy;
           54
           55public class OperationSub extends Operation {
           56
           57    public double getResults() {    
           58        return  getdNumberA() - getdNumberB() ;
           59    }

           60
           61}

           62
           63
           64
           65package simpleFactroy;
           66
           67public class OperationSqrt extends Operation {
           68
           69    @Override
           70    public double getResults() {
           71        return Math.sqrt(getdNumberA());
           72    }

           73    
           74
           75}

           76
           77
           78
           79package simpleFactroy;
           80
           81public class OperationFactroy {
           82    public static Operation getOperation(String opr){
           83        Operation operation = null ;
           84        if(opr.equals("+")){
           85            operation = new OperationAdd();
           86        }
          else if(opr.equals("-")){
           87            operation = new OperationSub();
           88        }
          else{
           89            operation = new OperationSqrt();
           90        }

           91        return operation ;
           92    }

           93
           94}

           95
           96
           97
           98package simpleFactroy;
           99
          100import java.util.Scanner;
          101
          102
          103public class TestCalculate {
          104    public static void main(String[] args)throws Exception{
          105        while (true{
          106            Scanner src = new Scanner(System.in);
          107            double a = src.nextDouble();
          108            String operation = src.next();
          109            double b = src.nextDouble();
          110            System.out.println(calculate(a, operation, b));
          111            System.out.println(calculate(a));
          112        }

          113    }

          114    
          115    public static double calculate(double a,String operation, double b ){
          116        Operation opr = OperationFactroy.getOperation(operation);
          117        opr.setdNumberA(a);
          118        opr.setdNumberB(b);
          119        return opr.getResults();
          120    }

          121    public static double calculate(double a){
          122        Operation opr = OperationFactroy.getOperation("");
          123        opr.setdNumberA(a);
          124        return opr.getResults();
          125    }

          126
          127}

          128

          Feedback

          # re: 設(shè)計模式(2)-------------簡單工廠模式(代碼實現(xiàn))  回復(fù)  更多評論   

          2010-05-20 22:27 by wjmgyn
          大話設(shè)計 模式看多了。。。。

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 贞丰县| 敖汉旗| 固原市| 沁阳市| 扬州市| 成安县| 太仓市| 都匀市| 宽城| 穆棱市| 涟水县| 枣强县| 靖宇县| 台州市| 沅江市| 竹溪县| 福州市| 塘沽区| 宣汉县| 泸溪县| 葫芦岛市| 新绛县| 麦盖提县| 平安县| 揭东县| 那坡县| 林甸县| 封开县| 锡林浩特市| 分宜县| 申扎县| 万安县| 乳源| 龙江县| 枝江市| 改则县| 永登县| 三江| 陕西省| 澄江县| 徐闻县|