我已经在C# .Net Core中以以下方式实现了工厂方法。我有几个具体产品,比如说Gateway1
和Gateway2
public interface IGateway
{
void Test();
}
具体创建者:
public class PaymentFactory
{
private readonly IPaymentTransactionRepository _paymentTransactionRepository;
private readonly IPaymentGatewayRepository _paymentGatewayRepository;
public PaymentFactory(IPaymentTransactionRepository paymentTransactionRepository,
IPaymentGatewayRepository paymentGatewayRepository)
{
_paymentTransactionRepository = paymentTransactionRepository;
_paymentGatewayRepository = paymentGatewayRepository;
}
public IGateway ExecuteCreation(string bank)
{
switch (bank)
{
case "Gateway1":
{
return new Gateway1(_paymentGatewayRepository);
}
case "Gateway2":
{
return new Gateway2(_paymentTransactionRepository, _paymentGatewayRepository);
}
default:
{
return null;
}
}
}
}
具体产品:
public class Gateway1 : IGateway
{
private readonly IPaymentTransactionRepository _paymentTransactionRepository;
public Gateway1(IPaymentTransactionRepository paymentGatewayRepository)
{
_paymentGatewayRepository = paymentGatewayRepository;
}
public void Test()
{
//code
}
}
public class Gateway2 : IGateway
{
private readonly IPaymentTransactionRepository _paymentTransactionRepository;
private readonly IPaymentGatewayRepository _paymentGatewayRepository;
public Gateway2(IPaymentTransactionRepository paymentGatewayRepository,
IPaymentGatewayRepository paymentGatewayRepository)
{
_paymentGatewayRepository = paymentGatewayRepository;
_paymentGatewayRepository = paymentGatewayRepository;
}
public void Test()
{
//code
}
}
这段代码正在运行,但我想对它进行两个更改。
1- 如何通过反射实现工厂方法?
2- 如何传入多个参数以创建ConcreteProducts
?
提前感谢。