[英]How to return different Response Types using Factory / Strategy pattern
目標:我想實現多個付款處理器。 (語言:C#)
設計選擇:我選擇了戰略模式。 它有助於致電適當的付款處理者。
問題:不同的付款處理者返回不同的響應。 我必須為每個處理器創建單獨的Process()方法。
我試圖統一一個接口中的所有響應,但是該接口具有如此多的屬性,並且不確定是否有更好的方法。
public class PaymentStrategy
{
public static IPaymentProcessor GetPaymentType(IPaymentDetails paymentDetails)
{
if (paymentDetails.PaymentType == PaymentTypes.PaymentProcessorA)
return new PaymentProcessorA();
else if (paymentDetails.PaymentType == PaymentTypes.PaymentProcessorA)
return new PaymentProcessorA();
else if (paymentDetails.PaymentType == PaymentTypes.PayPal)
return new PayPal();
else
throw new NotImplementedException();
}
}
public class PaymentService : IPaymentService
{
public IPaymentDetails PaymentDetails { get; set; }
public IPaymentProcessor PaymentProcessor { get; set; }
public PaymentService(IPaymentDetails paymentDetails)
{
PaymentDetails = paymentDetails;
PaymentProcessor = PaymentStrategy.GetPaymentType(paymentDetails);
}
public PaymentA_Response Process_A()
{
return PaymentProcessor.Process_A(PaymentDetails);
}
public PaymentB_Response Process_B()
{
return PaymentProcessor.Process_B(PaymentDetails);
}
}
internal class PaymentProcessorA : IPaymentProcessor
{
public string GetPaymentGatewayName()
{
return this.GetType().Name;
}
public PaymentA_Response Process(IPaymentDetails paymentDetails)
{
return new PaymentA_Response() { TxId = 1, ResponseFromA = $"Success from {GetType().Name}" };
}
}
internal class PaymentProcessorB : IPaymentProcessor
{
public string GetPaymentGatewayName()
{
return this.GetType().Name;
}
public PaymentB_Response Process(IPaymentDetails paymentDetails)
{
return new PaymentB_Response() { TxId = 1, ResponseFromB = $"Success from {GetType().Name}" };
}
}
class PaymentA_Response
{
public int TransactionId { get; set; }
public string Status { get; set; }
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
}
class PaymentB_Response
{
public int Tx { get; set; }
public string Status { get; set; }
public string F { get; set; }
public string G { get; set; }
public string H { get; set; }
public string I { get; set; }
public string J { get; set; }
}
class Program
{
static void Main(string[] args)
{
TestPaymentGateway();
Console.ReadLine();
}
private static void TestPaymentGateway()
{
// Calling code for PaymentGateway A
IPaymentDetails paymentDetails = new PaymentDetails()
{
Amount = 10,
PaymentType = PaymentTypes.PaymentProcessorA
};
PaymentService paymentService = new PaymentService(paymentDetails);
Process_A_Response resopnse = paymentService.Process_A();
Console.WriteLine(resopnse.A);
// Calling code for PaymentGateway B
IPaymentDetails paymentDetails = new PaymentDetails()
{
Amount = 10,
PaymentType = PaymentTypes.PaymentProcessorB
};
PaymentService paymentService = new PaymentService(paymentDetails);
Process_B_Response resopnse = paymentService.Process_B();
Console.WriteLine(resopnse.F);
}
}
需要的解決方案:我希望客戶端代碼使用PaymentService類調用名為Process()的方法,該方法返回單個響應對象,而不是調用process_A或Process_B。
您可以使用通用接口來處理它,以便一個Process
方法可以處理它:
public class PaymentService<TResponse> : IPaymentService<TResponse>
{
public IPaymentDetails PaymentDetails { get; set; }
public IPaymentProcessor PaymentProcessor { get; set; }
public PaymentService(IPaymentDetails paymentDetails)
{
PaymentDetails = paymentDetails;
PaymentProcessor = PaymentStrategy.GetPaymentType(paymentDetails);
}
public TResponse Process()
{
return PaymentProcessor.Process(PaymentDetails);
}
}
在主叫端,您將需要指定響應類型:
var service = new PaymentService<PaymentA_Response>();
var response = service.Process();
上面的代碼片段只是用來說明如何使用通用類型參數在服務中使用一種方法將其合並。 您可能還需要在其他地方進行更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.