定義一個用于創(chuàng)建對象的接口,讓子類決定實例化哪一個類。工廠方法使一個類的實例化延遲到了子類。

             工廠方法模式實現(xiàn),客戶端進行工廠的實例化,產(chǎn)生哪一個工廠實例的邏輯判斷依然存在。工廠方法把簡單工廠內(nèi)部邏輯判斷轉(zhuǎn)移到了客
          戶端。工廠方法模式就是利用了“依賴倒轉(zhuǎn)原則”解決了簡單工廠模式違背“開
          -閉原則”的問題。



          package factroyMethod;

          public class Operation  {
              
          private double dNumberA;
              
          private double dNumberB;
              
              
          public double getResults(){
                  
          double result = 0d;
                  
          return result ;
              }


              
          public double getdNumberA() {
                  
          return dNumberA;
              }


              
          public void setdNumberA(double dNumberA) {
                  
          this.dNumberA = dNumberA;
              }


              
          public double getdNumberB() {
                  
          return dNumberB;
              }


              
          public void setdNumberB(double dNumberB) {
                  
          this.dNumberB = dNumberB;
              }


              
          public Operation(double dNumberA, double dNumberB) {
                  
          this.dNumberA = dNumberA;
                  
          this.dNumberB = dNumberB;
              }


              
          public Operation(double dNumberA) {
                  
          this.dNumberA = dNumberA;
              }


              
          public Operation() {
              }

          }




          package factroyMethod;

          public interface IFactory {
              Operation getOperation();
          }



          package factroyMethod;


          public class FactroyAdd implements IFactory {

              
          public Operation getOperation() {
                  
          return new OperationAdd() ;
              }


          }



          package factroyMethod;


          public class FactroySub implements IFactory {

              
          public Operation getOperation() {
                  
          return new OperationSub() ;
              }


          }



          package factroyMethod;

          public class FactroySqrt implements IFactory {

              @Override
              
          public Operation getOperation() {
                  
                  
          return  new OperationSqrt();
              }


          }



          package factroyMethod;

          public class OperationAdd extends Operation {

              
          public double getResults() {
                  
          return  getdNumberA() + getdNumberB() ;
              }

              

          }



          package factroyMethod;

          public class OperationSub extends Operation {

              
          public double getResults() {    
                  
          return  getdNumberA() - getdNumberB() ;
              }


          }



          package factroyMethod;

          public class OperationSqrt extends Operation {

              @Override
              
          public double getResults() {
                  
          return Math.sqrt(getdNumberA());
              }

              

          }



          package factroyMethod;

          import java.util.Scanner;


          public class TestCalculate {
              
          public static void main(String[] args)throws Exception{        
                  
          while (true{
                      Scanner src 
          = new Scanner(System.in);
                      
          double a = src.nextDouble();
                      String operation 
          = src.next();
                      
          double b = src.nextDouble();
                      IFactory factory 
          = null ;
                      
          if(operation.equals("+"))factory = new FactroyAdd();
                      
          else if(operation.equals("-"))factory = new FactroySub();
                      
          else factory = new FactroySqrt();
                      
                      Operation opr 
          =factory.getOperation();
                      opr.setdNumberA(a);
                      opr.setdNumberB(b);
                      System.out.println(opr.getResults());
                  }

              }

              
              
          }


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 宁远县| 大同县| 抚宁县| 瑞安市| 涟源市| 民县| 泸定县| 秭归县| 禄丰县| 澎湖县| 文登市| 通海县| 澄江县| 曲麻莱县| 顺平县| 巴林右旗| 原阳县| 万安县| 苏州市| 高雄县| 商城县| 皋兰县| 聊城市| 滦南县| 台州市| 宽城| 德庆县| 通化市| 江安县| 牙克石市| 应城市| 石家庄市| 郸城县| 华亭县| 岳西县| 宜章县| 合川市| 剑川县| 江油市| 雷山县| 冕宁县|