如何使用工厂创建使用策略模式的对象? [英] How to use a factory to create objects which use Strategy pattern?

查看:176
本文介绍了如何使用工厂创建使用策略模式的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在网上商店有一个简单的付款功能。我们希望通过不同的交易处理器管理不同的交易:




  • 交易可以是付款或退款。

  • 交易处理器可以是PayPal或Payplug。



所以我们有以下类:

 类PaymentTransaction实现Transaction {

}

class RefundTransaction实现事务{

}

class PaypalProcessor实现处理器{

}

class PayplugProcessor实现处理器{

}

这个答案,我们可以使用以下使用策略和多态的类。

  class PaymentProcessor {
private Processor processor;
私人交易事务;

public PaymentProcessor(处理器处理器,事务处理){
this.processor = processor;
this.transaction = transaction;
}

public void processPayment(){
processor.process(transaction);
}
}

我们假设处理器和使用的交易被给予从数据库。我想知道如何创建 PaymentProcessor 对象。



似乎只有一个方法的抽象工厂类仍然是有效的抽象工厂模式。所以在这种情况下,我想知道是否使用抽象工厂是相关的。




  • 如果是,如何实现它?

  • 如果没有,我们应该使用 PaymentProcessorFactory Factory Method 模式创建 PaymentProcessor 根据数据库中提供的详细信息,他的两个属性



什么是最佳做法在这种情况下使用工厂?

解决方案


我们假设处理器和使用的事务是从数据库中给出。我想知道如何创建PaymentProcessor对象。


我将定义一个可以适应数据库结果或任何其他来源的界面可以提供创建 PaymentProcessor 所需的数据。这对于unittests也是有用的。

  public interface PaymentProcessorFactoryArgs {
String getProcessorType();
String getTransactionType();
}

然后实现这样的工厂。

  public class PaymentProcessorFactory {

private Map< String,Processor> processorMap = new HashMap<>();
private Map< String,Transaction> transactionMap = new HashMap<>();

public PaymentProcessorFactory(){
processorMap.put(paypal,new PaypalProcessor());
processorMap.put(payplug,新的PayplugProcessor());

transactionMap.put(退款,新的退款交易());
transactionMap.put(payment,新的PaymentTransaction());
}

public PaymentProcessor create(PaymentProcessorFactoryArgs factoryArgs){
String processorType = factoryArgs.getProcessorType();
处理器处理器= processorMap.get(processorType);
if(processor == null){
throw new IllegalArgumentException(未知处理器类型+ processorType);
}

String transactionType = factoryArgs.getTransactionType();
事务事务= transactionMap.get(transactionType);
if(transaction == null){
throw new IllegalArgumentException(未知事务类型+ processorType);
}

返回新的PaymentProcessor(处理器,事务);
}
}

这只是一个简单的例子。如果您可以注册处理器交易,则会更好。 public void register(String processorType,Processor processor){
$ $ $ $ $ $ $ $ $ $

$ $ b public void register(String transactionType,Transaction transaction){
...
}

您还可能需要使用其他类型,然后使用 String 作为密钥,也可能是枚举。



在这个例子中,每次 PaymentProcessor 处理器 Transaction code>被创建。如果要为每个 PaymentProcessor 创建新对象,可以替换 Map s类型

  private Map< String,Factory< Processor>> processorMap = new HashMap<>(); 
private Map< String,Factory< Transaction>> transactionMap = new HashMap<>();

与其他工厂界面。公开接口工厂< T>< T>< T> {
public T newInstance();
}


Let's assume we have a simple payment feature on an online shop. We want to manage different transactions with different processors of transactions:

  • A transaction can be a payment or a refund.
  • A processor of transactions can be Paypal or Payplug.

So we have the following classes:

class PaymentTransaction implements Transaction {

}

class RefundTransaction implements Transaction {

}

class PaypalProcessor implements Processor {

}

class PayplugProcessor implements Processor {

}

As suggested in this answer, we could use the following class which uses Strategy and polymorphism.

class PaymentProcessor {
     private Processor processor;
     private Transaction transaction;

     public PaymentProcessor(Processor processor, Transaction transaction) {
          this.processor = processor;
          this.transaction = transaction;
     }

     public void processPayment() {
         processor.process(transaction);
     }
}

We assume the processor and the transaction to use are given from the database. I wonder how to create the PaymentProcessor object.

It seems that an abstract factory class with only one method is still a valid Abstract Factory pattern. So, in this case I wonder if using Abstract Factory would be relevant.

  • If yes, how to implement it?
  • If no, should we use Factory Method pattern with a PaymentProcessorFactory class to create PaymentProcessor with his two attributes according the details given from the database?

What is a best practice to use a factory in this case?

解决方案

We assume the processor and the transaction to use are given from the database. I wonder how to create the PaymentProcessor object.

I would define an interface that I can adapt to the database result or any other source that can provide the data needed to create a PaymentProcessor. This is also useful for unittests.

public interface PaymentProcessorFactoryArgs {
  String getProcessorType();
  String getTransactionType();
}

and then implement a factory like this.

public class PaymentProcessorFactory {

  private Map<String, Processor> processorMap = new HashMap<>();
  private Map<String, Transaction> transactionMap = new HashMap<>();

  public PaymentProcessorFactory() {
    processorMap.put("paypal", new PaypalProcessor());
    processorMap.put("payplug", new PayplugProcessor());

    transactionMap.put("refund", new RefundTransaction());
    transactionMap.put("payment", new PaymentTransaction());
  }

  public PaymentProcessor create(PaymentProcessorFactoryArgs factoryArgs) {
    String processorType = factoryArgs.getProcessorType();
    Processor processor = processorMap.get(processorType);
    if(processor == null){
      throw new IllegalArgumentException("Unknown processor type " + processorType);
    }

    String transactionType = factoryArgs.getTransactionType();
    Transaction transaction = transactionMap.get(transactionType);
    if(transaction == null){
      throw new IllegalArgumentException("Unknown transaction type " + processorType);
    }

    return new PaymentProcessor(processor, transaction);
  }
}

This is just a quick example. It would be better if you can register Processors and Transactions. E.g.

public void register(String processorType, Processor processor){
   ...
}
public void register(String transactionType, Transaction transaction){
   ...
}

You also might want to use anther type then String for the keys, maybe an enum.

In this example the Processor and Transaction objects are re-used every time a PaymentProcessor is created. If you want to create new objects for each PaymentProcessor, you can replace the Maps type

private Map<String, Factory<Processor>> processorMap = new HashMap<>();
private Map<String, Factory<Transaction>> transactionMap = new HashMap<>();

with anther factory interface. E.g.

public interface Factory<T> {
  public T newInstance();
}

这篇关于如何使用工厂创建使用策略模式的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆