在自分层树状结构中,我希望在每个节点添加/编辑/删除时检查每个节点级别的业务规则(基于节点类型)。我尝试实现复合设计模式,但没有成功。请提个建议。
示例类结构。
class Parent
{
int Id;
}
class ChildType1 : Parent
{
string propForType1;
List<Parent> ListOfChildren;
}
class ChildType2 : Parent
{
string propForType2;
List<Parent> ListOfChildren;
}

当我说业务规则时,它意味着这些规则是对节点类型的特定约束。对于ex:业务规则1- ChildType2只能有ChildType2类型的子项
业务规则2- ChildType1应该至少有2个子节点,并且propForType1值不应该为空。
对于每个新节点的添加/编辑/删除,我需要在每个节点上检查这些规则,以便我的整个树满足所有业务规则。
发布于 2015-11-24 03:06:53
你应该检查一下访问者模式。它的最佳点是使用树/数据集合,其中的子类是基类的实现。
鲍勃大叔的一篇好文章可以在这里找到:http://butunclebob.com/ArticleS.UncleBob.IuseVisitor
另一个参考资料可以在这里找到:http://www.dofactory.com/net/visitor-design-pattern
发布于 2015-11-24 06:58:49
我在考虑添加一个可以调用的验证方法,并覆盖它并为每个类型创建实现。
class Parent
{
int Id;
public abstract bool isValid();
}
class ChildType1 : Parent
{
string propForType1;
List<Parent> ListOfChildren;
public override bool isValid() {
return ListOfChildren.Count >= 2;
}
}
class ChildType2 : Parent
{
string propForType2;
List<Parent> ListOfChildren;
public override bool isValid() {
foreach (Parent p in ListOfChildren) {
if (!(p is ChildType2)) {
return false;
}
}
return true;
}
}并在添加/编辑/删除过程中调用isValid()?
https://stackoverflow.com/questions/33878532
复制相似问题