设计要点:多态性是否适合我的情况
Design point : is polymorphism appropriate in my case
我的情况类似于以下情况:
interface IAbstractPaymentService { void ProcessPayment(); }
interface IPaymentGateway1Service : IAbstractPaymentService { } // Do not define extra methods but needed for IoC container configuration
interface IPaymentGateway2Service : IAbstractPaymentService { } // Do not define extra methods but needed for IoC container configuration
public abstract class PaymentProcessor
{
protected abstract void ThisMethodNeedsASpecializedService(IAbstractPaymentService abstractPaymentService);
}
public class PaymentGateway1Processor : PaymentProcessor
{
protected override void ThisMethodNeedsASpecializedService(IAbstractPaymentService abstractPaymentService)
{
return ThisMethodNeedsASpecializedService(abstractPaymentService as IPaymentGateway1Service) // Don't worry, I do security checks
}
public void ThisMethodNeedsASpecializedService(IPaymentGateway1Service paymentGateway1Service)
{
paymentGateway1Service.ProcessPayment();
}
}
public class PaymentGateway2Processor : PaymentProcessor
{
protected override void ThisMethodNeedsASpecializedService(IAbstractPaymentService abstractPaymentService)
{
return ThisMethodNeedsASpecializedService(abstractPaymentService as IPaymentGateway2Service) // Don't worry, I do security checks
}
public void ThisMethodNeedsASpecializedService(IPaymentGateway2Service paymentGateway2Service)
{
paymentGateway2Service.ProcessPayment();
}
}
我对这种抽象不是很满意,因为多态的思想是你不关心底层类型,你只希望应用某种行为。但是在这里,即使我创建了一个 PaymentProcessor 工厂,每次消费者需要调用 ThisMethodNeedsASpecializedService() 时,他都需要知道底层类型才能注入正确的服务。
我正在考虑将服务存储在内部 属性,这样我就可以创建一个在创建时注入服务的工厂,而消费者不需要知道所使用的服务 -因此,不会关心底层类型。但我一直认为将服务实例存储在 属性 中是一种不好的做法,我不确定是否应该这样做。
您对此有何看法,您会采取不同的做法吗?
实现结构的更好方法是将 IAbstractPaymentService
注入 PaymentProcessor
构造函数。例如:
public abstract class PaymentProcessor
{
protected abstract void ThisMethodNeedsASpecializedService();
}
public class PaymentGateway1Processor : PaymentProcessor
{
private IPaymentGateway1Service paymentGateway1Service;
public PaymentGateway1Processor(IPaymentGateway1Service paymentGateway1Service){
this.paymentGateway1Service = paymentGateway1Service;
}
public void ThisMethodNeedsASpecializedService()
{
this.paymentGateway1Service.ProcessPayment();
}
}
我的情况类似于以下情况:
interface IAbstractPaymentService { void ProcessPayment(); }
interface IPaymentGateway1Service : IAbstractPaymentService { } // Do not define extra methods but needed for IoC container configuration
interface IPaymentGateway2Service : IAbstractPaymentService { } // Do not define extra methods but needed for IoC container configuration
public abstract class PaymentProcessor
{
protected abstract void ThisMethodNeedsASpecializedService(IAbstractPaymentService abstractPaymentService);
}
public class PaymentGateway1Processor : PaymentProcessor
{
protected override void ThisMethodNeedsASpecializedService(IAbstractPaymentService abstractPaymentService)
{
return ThisMethodNeedsASpecializedService(abstractPaymentService as IPaymentGateway1Service) // Don't worry, I do security checks
}
public void ThisMethodNeedsASpecializedService(IPaymentGateway1Service paymentGateway1Service)
{
paymentGateway1Service.ProcessPayment();
}
}
public class PaymentGateway2Processor : PaymentProcessor
{
protected override void ThisMethodNeedsASpecializedService(IAbstractPaymentService abstractPaymentService)
{
return ThisMethodNeedsASpecializedService(abstractPaymentService as IPaymentGateway2Service) // Don't worry, I do security checks
}
public void ThisMethodNeedsASpecializedService(IPaymentGateway2Service paymentGateway2Service)
{
paymentGateway2Service.ProcessPayment();
}
}
我对这种抽象不是很满意,因为多态的思想是你不关心底层类型,你只希望应用某种行为。但是在这里,即使我创建了一个 PaymentProcessor 工厂,每次消费者需要调用 ThisMethodNeedsASpecializedService() 时,他都需要知道底层类型才能注入正确的服务。
我正在考虑将服务存储在内部 属性,这样我就可以创建一个在创建时注入服务的工厂,而消费者不需要知道所使用的服务 -因此,不会关心底层类型。但我一直认为将服务实例存储在 属性 中是一种不好的做法,我不确定是否应该这样做。
您对此有何看法,您会采取不同的做法吗?
实现结构的更好方法是将 IAbstractPaymentService
注入 PaymentProcessor
构造函数。例如:
public abstract class PaymentProcessor
{
protected abstract void ThisMethodNeedsASpecializedService();
}
public class PaymentGateway1Processor : PaymentProcessor
{
private IPaymentGateway1Service paymentGateway1Service;
public PaymentGateway1Processor(IPaymentGateway1Service paymentGateway1Service){
this.paymentGateway1Service = paymentGateway1Service;
}
public void ThisMethodNeedsASpecializedService()
{
this.paymentGateway1Service.ProcessPayment();
}
}