代码之家  ›  专栏  ›  技术社区  ›  nanachimi

当实现的构造函数具有要注入的依赖项时的工厂模式

  •  0
  • nanachimi  · 技术社区  · 7 年前

    我正在做一件私人事情,我面临一个需要其他意见的问题。我有下面的代码,我想在其中创建一个工厂模式的新实例 PaymentStrategy :

    界面

    public interface PaymentStrategy {
    
      Optional<Payment> pay(String payerAccountNumber,
                            String sellerAccountNumber,
                            ProductOrder[] productOrder
      );
    }
    

    EmployeePaymentStrategy

    public class EmployeePaymentStrategy implements PaymentStrategy {
    
      private final ProfileRemoteProvider profileRemoteProvider;
      private final PaymentValidator      paymentValidator;
    
      @Autowired
      public EmployeePaymentStrategy(ProfileRemoteProvider profileRemoteProvider,
                                     PaymentValidator paymentValidator) {
        this.profileRemoteProvider = profileRemoteProvider;
        this.paymentValidator = paymentValidator;
      }
    
      @Override
      public Optional<Payment> pay(String payerAccountNumber,
                                   String sellerAccountNumber,
                                   ProductOrder[] productOrder) {
        ...
      }
    }
    

    我想知道如何处理工厂类中的依赖项。 是 员工支付策略 工厂模式是解决问题的最佳方式吗

    PaymentStrategyFactory 问题出在哪里

    public class PaymentStrategyFactory {
    
      private PaymentStrategyFactory() {
      }
    
      public static PaymentStrategy getPaymentStrategy(AccountType payerAccountType,
                                                       AccountType sellerAccountType) {
        if (sellerAccountType == AccountType.COMPANY) {
          switch (payerAccountType) {
            case EMPLOYEE:
              return new EmployeePaymentStrategy(...); //TODO 
            case BASIC_USER:
              return ...
            default:
              //this exception is throw when a payer account type is unknown
              throw new RuntimeException("exception type will be more specific");
          }
        }
        //This exception is throw when a seller account type is not a seller
        throw new RuntimeException("exception type will be more specific");
      }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   nanachimi    7 年前

    PaymentStrategyFactory

    public class PaymentStrategyFactory {
    
      private static ApplicationContext context;
      private ApplicationContext applicationContext;
    
      @Autowired
      private PaymentStrategyFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
      }
    
      @PostConstruct
      private void initializeApplicationContext() {
        PaymentStrategyFactory.context = applicationContext;
        this.applicationContext = null;
      }
    
      @Override
      public void setApplicationContext(ApplicationContext context) throws BeansException {
        PaymentStrategyFactory.context = context;
      }
    
      public static PaymentStrategy getPaymentStrategy(AccountType payerAccountType,
                                                       AccountType sellerAccountType) {
        if (sellerAccountType == AccountType.COMPANY) {
          switch (payerAccountType) {
            case EMPLOYEE:
              return context.getBean(EmployeePaymentStrategy.class);
              // return new EmployeePaymentStrategy();
            case BASIC_USER:
               ...
            }
         } 
         throw ...
       }
    }
    

    this post 在使用Spring进行DI(依赖项注入)时,请帮助我处理静态成员。现在我觉得一切都很好。