我有以下情况:必须根据每个对象的特定属性(该属性定义为枚举)将对象集合发送给不同的第三方。我打算使用下面的工厂模式来实现这一点。
可以将其重构为使用依赖注入吗?
public class ServiceA: IThirdParty
{
public void Send(Ticket objectToBeSent)
{
// a call to the third party is made to send the ticket
}
}
public class ServiceB: IThirdParty
{
public void Send(Ticket objectToBeSent)
{
// a call to the third party is made to send the ticket
}
}
public interface IThirdParty
{
void Send(Ticket objectToBeSent);
}
public static class ThirdPartyFactory
{
public static void SendIncident(Ticket objectToBeSent)
{
IThirdParty thirdPartyService = GetThirdPartyService(objectToBeSent.ThirdPartyId);
thirdPartyService.Send(objectToBeSent);
}
private static IThirdParty GetThirdPartyService(ThirdParty thirdParty)
{
switch (thirdParty)
{
case ThirdParty.AAA:
return new ServiceA();
default:
return new ServiceB();
}
}
}