父子关系类的设计模式 [英] Parent child class relationship design pattern

查看:176
本文介绍了父子关系类的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有子项的列表的类。有没有一种设计模式,我可以复制,我可以申请到这些类,这样我可以从子访问父实例,并强制执行的规则,如不能够给孩子添加到多个家长,等?

I have a class which has a list of child items. Is there a design pattern I can copy that I can apply to these classes so that I can access the parent instance from the child, and it enforces rules such as not being able to add the child to multiple parents, etc?

推荐答案

尝试复合的设计模式:

http://www.dofactory.com/Patterns/PatternComposite.aspx

要使用它,你就必须在添加一些代码来移回树,它看起来像父,但除此之外,它应该工作。

To use this, you'll have to add some code in to move back up the tree to the parent it looks like, but other than that it should work.

只需添加时,它被添加到树保存到父元素的引用的属性。如果父变化更新,并设置它,如果节点被移除空。

Just add a property that holds a reference to the parent element when it is added to the tree. Update it if the parent changes, and set it to null if the node is removed.

这篇关于父子关系类的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆