我相信你会发现很多弱点,请告诉我。我的主要问题是,这是否一种将施工阶段与施工阶段分开的明智方法。
基本上,我有一组需要转换为类似(但不同)对象的对象。一旦被转换,我希望以后能够查询这些内容。
因此,我有一个处理转换部分的PartitionedContainersBuilder,还有一个PartitionedContainer,它表示可转换的查询。
我喜欢这样做的是,在任何地方都没有无效的状态,对成员变量做错误的风险也很小。如果Java能够返回两个值,那么我可能会以不同的方式解决它。但是,尽管如此,我知道,例如,调用它和构建可能不是最好的事情(它不是构建器模式)。
我更愿意让PartitionedContainer中的构造函数来完成这项工作,而不是静态构建--这样会更好吗?我的直觉是肯定的,但有明显的优势吗?
public class PartitionedContainers {
static class PartitionedContainersBuilder
{
private LogicalExcelAreaInterface buildLogicalExcelArea(ContainerNodeInterface pContainer, PapyrusInterface pPapyrus)
{
// TODO: introduce LeafItemContainer to remove if cases
if (pContainer.getItem() != null)
{
return pContainer.getItem().getLogicalExcelArea(pPapyrus);
} else {
return new MutableLogicalExcelArea(1, 1)
.withCellWidth(0, 0, pPapyrus.getWidth())
.withCellHeight(0, 0, pPapyrus.getHeight());
}
}
public PartitionedContainers build(final ContainerNodeInterface pRootContainer, final Map<ContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
final Map<PartitionedContainerInterface, PapyrusInterface> lPapyrusFromContainer = new HashMap<PartitionedContainerInterface, PapyrusInterface>();
final Map<ContainerNodeInterface, MutablePartitionedContainer> lPartitionedContainerFromContainer = new HashMap<ContainerNodeInterface, MutablePartitionedContainer>();
for (final ContainerNodeInterface lNode : new NodeCollections<ContainerNodeInterface>().getAllSortedTopDown(pRootContainer))
{
lPartitionedContainerFromContainer.put(
lNode,
new MutablePartitionedContainer(lNode.getElement(), buildLogicalExcelArea(lNode, pPapyrusFromContainer.get(lNode)))
);
if (!lNode.isRoot()) {
lPartitionedContainerFromContainer.get(lNode).withParent(lPartitionedContainerFromContainer.get(lNode.getParent()));
lPartitionedContainerFromContainer.get(lNode.getParent()).withAdditionalChild(lPartitionedContainerFromContainer.get(lNode));
}
lPapyrusFromContainer.put(lPartitionedContainerFromContainer.get(lNode), pPapyrusFromContainer.get(lNode));
}
return new PartitionedContainers(lPartitionedContainerFromContainer.get(pRootContainer), lPapyrusFromContainer);
}
}
PartitionedContainerInterface mRootContainer;
Map<PartitionedContainerInterface, PapyrusInterface> mPapyrusFromContainer;
private PartitionedContainers(PartitionedContainerInterface pRootContainer, Map<PartitionedContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
mRootContainer = pRootContainer;
mPapyrusFromContainer = pPapyrusFromContainer;
}
public PartitionedContainerInterface getRoot()
{
return mRootContainer;
}
public static PartitionedContainers build(ContainerNodeInterface pRootContainer, final Map<ContainerInterface, PapyrusInterface> pPapyrusFromContainer)
{
return new PartitionedContainersBuilder().build(pRootContainer, pPapyrusFromContainer);
}
public PapyrusInterface getPapyrusFromContainer(PartitionedContainerInterface pContainer)
{
return mPapyrusFromContainer.get(pContainer);
}
}发布于 2011-11-28 07:46:53
首先,你是对的,你拥有的不是一个建筑类,而是更像一个工厂类。这大致相当于静态工厂方法。
现在,关于使用构造函数与静态工厂方法的问题已经有了很多的讨论,而且在大多数情况下都没有结论性的、普遍适用的答案。(只需搜索"c#构造函数与静态工厂方法“,您就会看到)。但是,特别是在构造类和构造类的代码在另一个类中都是私有的情况下,new关键字所具有的“可发现性”和“可识别性”的优点并不适用,所以我肯定会使用工厂方法或类。
至于您应该使用工厂类还是静态工厂方法,我认为工厂类是可以的。生成立即被丢弃的附加对象的开销可以忽略不计(特别是与对象将要完成的工作量相比),甚至可能由编译器优化。另一方面,如果将来您决定在构建器中引入某种状态,那么拥有一个工厂类而不是静态工厂方法可能会很有用。
https://codereview.stackexchange.com/questions/6346
复制相似问题