我试图根据开闭原则重构一些代码,但在应用设计模式时,我似乎无法正确获得以下类。(对于下面列出的许多课程,我深表歉意——我已经尽可能地减少了它们,但其余的需要向您展示我的设计)。
该设置由以下类组成:
public interface IPortFactory
{
IPort CreatePort(int id, PortDetails details);
}
public class PtpPortFactory : IPortFactory
{
public IPort CreatePort(int id, PortDetails details)
{
var ptpPortDetails = details as PtpPortDetails;
if (ptpPortDetails == null)
{
throw new ArgumentException("Port details does not match ptp ports", "details");
}
return new PtpPort(id, FiberCapability.FromValue(ptpPortDetails.Capability));
}
}
public interface IPort
{
int Id { get; }
}
public interface IInternetPort : IPort
{
bool OfCapability(FiberCapability capability);
}
public class PtpPort : IInternetPort
{
private readonly FiberCapability _capability;
public PtpPort(int id, FiberCapability capability)
{
_capability = capability;
Id = id;
}
public int Id { get; private set; }
public bool OfCapability(FiberCapability capability)
{
return capability.Equals(_capability);
}
}
此外PtpPort
,我有PonPort
,它也实现IInternetPort
,而CatvPort
只是实现IPort
。
已经在这段代码中,我认为有代码气味的迹象。在CreatePort
in 中PtpPortFactory
,漂亮的事情是它接受PtpPortDetails
(继承自PortDetails
)而不是强制转换。但是,如果这样做,我将无法创建一个PonPortFactory
也实现的IPortFactory
,因为这些端口需要PonPortDetails
. 或CatvPortFactory
就此而言。
当我使用端口工厂时,会出现另一种代码异味:
PortType portType = command.PortType;
IPortFactory portFactory = portType.GetPortFactory();
var portsToSelectFrom = ports.Select(port => (IInternetPort) portFactory.CreatePort(port.Id, port.PortDetails)).ToList();
我真的很想不必从 to 做沮丧IPort
,IInternetPort
而只是有CreatePort
回报IInternetPort
。
理解上述内容所需的最后一点信息可能是以下类(基于Jimmy Bogards Enumeration
类):
public abstract class PortType : Enumeration<PortType, int>
{
public static readonly PortType Ptp = new PtpPortType();
public static readonly PortType Pon = new PonPortType();
public static readonly PortType Catv = new CatvPortType();
protected PortType(int value, string description)
: base(value, description) { }
public abstract IPortFactory GetPortFactory();
private class CatvPortType : PortType
{
public CatvPortType() : base(2, "catv") { }
public override IPortFactory GetPortFactory()
{
return new CatvPortFactory();
}
}
private class PonPortType : PortType
{
public PonPortType() : base(1, "pon") { }
public override IPortFactory GetPortFactory()
{
throw new NotImplementedException("Pon ports are not supported");
}
}
private class PtpPortType : PortType
{
public PtpPortType() : base(0, "ptp") { }
public override IPortFactory GetPortFactory()
{
return new PtpPortFactory();
}
}
}
我真的希望有人能一路帮助我(我尝试过引入泛型,但似乎总是遇到 C# 不支持返回类型协变的障碍)。
此外,任何其他帮助我编写更好代码的技巧将不胜感激。
更新
由于评论请求,我在下面添加了更多代码。
public Port Handle(TakeInternetPortCommand command)
{
var portLocatorService = new PortLocatorService();
IList<Port> availablePorts = portLocatorService.FindAvailablePorts(command.Pop, command.PortType);
PortType portType = command.PortType;
IPortFactory portFactory = portType.GetPortFactory();
var portsToSelectFrom = ports.Select(port => (IInternetPort) portFactory.CreatePort(port.Id, port.PortDetails)).ToList();
IPort port = _algorithm.RunOn(portsToSelectFrom);
Port chosenPort = availablePorts.First(p => p.Id == port.Id);
chosenPort.Take(command.Spir);
_portRepository.Add(chosenPort);
return chosenPort;
}
不要被突然之间也有Port
类型的事实所迷惑。这是另一个有界上下文中的聚合(在 DDD 的意义上)。该算法需要将 的列表作为输入IInternetPort
,因为它在内部使用该方法OfCapability
来选择一个端口。但是,当算法选择了正确的端口时,我们只对 感兴趣Id
,因此返回类型只是一个IPort
。