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

             工廠方法模式實(shí)現(xiàn),客戶端進(jìn)行工廠的實(shí)例化,產(chǎn)生哪一個工廠實(shí)例的邏輯判斷依然存在。工廠方法把簡單工廠內(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)站導(dǎo)航:
           
          主站蜘蛛池模板: 富阳市| 报价| 浮山县| 宝山区| 屏山县| 阿瓦提县| 马山县| 离岛区| 武安市| 抚宁县| 贵南县| 开江县| 寿光市| 镇江市| 新乡市| 洛宁县| 古交市| 玉田县| 普兰县| 太和县| 巨野县| 东丽区| 海原县| 海伦市| 汉源县| 哈巴河县| 义乌市| 杭锦旗| 望江县| 社会| 库尔勒市| 沂水县| 布尔津县| 青冈县| 菏泽市| 五河县| 普宁市| 丹寨县| 苍梧县| 泰安市| 凭祥市|