工厂方法模式

    • /**
      *
      */
      public abstract class OperationBean {
          private double _numberA = 0;//字段,即属性
          private double _numberB = 0;
      
      
      
          public double get_numberB() {
              return _numberB;
          }
      
          public void set_numberB(double _numberB) {
              this._numberB = _numberB;
          }
      
          public double get_numberA() {
              return _numberA;
          }
      
          public void set_numberA(double _numberA) {
              this._numberA = _numberA;
          }
          public  abstract  double GetResult();
      }
      
      
      public class OperationAdd extends OperationBean {
          @Override
          public double GetResult() {
              double result = 0;
              result = get_numberA() + get_numberB();
              return result;
          }
      }
      
      
      /**
       * 
       */
      public class OperationSub extends OperationBean {
          @Override
          public double GetResult() {
              double result = 0;
              result = get_numberA() - get_numberB();
              return result;
          }
      }
      
      
      /**
       * 
       */
      public class OperationMul extends OperationBean {
          @Override
          public double GetResult() {
              double result = 0;
              result = get_numberA() * get_numberB();
              return result;
          }
      }
      
      
      /**
       * 
       */
      public class OperationDiv extends OperationBean {
          @Override
          public double GetResult() {
              double result = 0;
              result = get_numberA() / get_numberB();
              return result;
          }
      }
      
      /**
       * 工厂类
       */
      public class OperationFactory{
      
          public static OperationBean createOperation(String operate){
              OperationBean operation = null;
              switch (operate){
                  case "+":
                      operation =  new OperationAdd();
                      break;
                  case "-":
                      operation =  new OperationSub();
                      break;
                  case "*":
                      operation =  new OperationMul();
                      break;
                  case "/":
                      operation =  new OperationSub();
                      break;
              }
              return operation;
          }
      
      
      }
      
      
      
      /**
       * 客户端代码
       */
      public class TestUtil {
      
          public static void main(String[] args) {
              
              OperationBean operation = OperationFactory.createOperation("+");
              operation.set_numberA(1);
              operation.set_numberB(2);
              System.out.println(operation.GetResult()+"");;
      
          }
      }
  • 工厂方法模式定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例话延迟到了其子类。
    • /**
       * 工厂类
       */
      public abstract class BossOperationFactory {
      
          public abstract OperationBean createOperation();
      }
      
      
      /**
       * 加法工厂
       */
      public class AddFactory extends BossOperationFactory {
          @Override
          public OperationBean createOperation() {
              return new OperationAdd();
          }
      }
      
      /**
       * 减法工厂
       */
      public class SubFactory extends BossOperationFactory {
          @Override
          public OperationBean createOperation() {
              return new OperationSub();
          }
      }
      
      /**
       * 乘法工厂
       */
      public class MulFactory extends BossOperationFactory {
          @Override
          public OperationBean createOperation() {
              return new OperationMul();
          }
      }
      
      
      /**
       * 除法工厂
       */
      public class DivFactory extends BossOperationFactory {
          @Override
          public OperationBean createOperation() {
              return new OperationDiv();
          }
      }
      
      
      /**
       * 客户端代码
       */
      public class TestUtil {
      
          public static void main(String[] args) {
           
              BossOperationFactory factory = new AddFactory();
              OperationBean operationBean = factory.createOperation();
              operationBean.set_numberA(1);
              operationBean.set_numberB(2);
              System.out.println(operationBean.GetResult()+"");
      
          }
      }
  • 抽象工厂模式(Abstract Factory)
    • 提供一个创建一系列相关或互相依赖对象的接口,而无需指定它们具体的类。
    • 利用反射,通过配置文件设置参数和获取配置文件参数。
posted @ 2018-12-28 21:55  好胖的兔子  阅读(129)  评论(0编辑  收藏  举报