家长小孩班关系设计模式 [英] Parent child class relationship design pattern

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

问题描述

我有一个有一个子项列表的类。有没有可以复制的设计模式,我可以应用到这些类,以便我可以从孩子访问父实例,并且执行诸如无法将孩子添加到多个父母等的规则?

解决方案

尝试复合设计模式:



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



为了使用这个,你必须添加一些代码来将树移回到它看起来像这样的父类,但是除此之外它应该可以工作。



只需添加一个属性,当属性添加到树中时,该属性将保存对父元素的引用。如果父更改,则更新它,如果节点被删除,则将其设置为null。


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?

解决方案

Try the composite design pattern:

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天全站免登陆