首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在自分层树结构中的每个节点上实现业务规则

如何在自分层树结构中的每个节点上实现业务规则
EN

Stack Overflow用户
提问于 2015-11-24 02:58:46
回答 2查看 162关注 0票数 0

在自分层树状结构中,我希望在每个节点添加/编辑/删除时检查每个节点级别的业务规则(基于节点类型)。我尝试实现复合设计模式,但没有成功。请提个建议。

示例类结构。

代码语言:javascript
复制
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值不应该为空。

对于每个新节点的添加/编辑/删除,我需要在每个节点上检查这些规则,以便我的整个树满足所有业务规则。

EN

回答 2

Stack Overflow用户

发布于 2015-11-24 03:06:53

你应该检查一下访问者模式。它的最佳点是使用树/数据集合,其中的子类是基类的实现。

鲍勃大叔的一篇好文章可以在这里找到:http://butunclebob.com/ArticleS.UncleBob.IuseVisitor

另一个参考资料可以在这里找到:http://www.dofactory.com/net/visitor-design-pattern

票数 1
EN

Stack Overflow用户

发布于 2015-11-24 06:58:49

我在考虑添加一个可以调用的验证方法,并覆盖它并为每个类型创建实现。

代码语言:javascript
复制
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()?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33878532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档