父子关系类的设计模式

编程入门 行业动态 更新时间:2024-10-26 20:29:18
本文介绍了父子关系类的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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?

推荐答案

尝试复合的设计模式:

的 www.dofactory/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.

更多推荐

父子关系类的设计模式

本文发布于:2023-10-25 00:15:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525394.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:父子   模式   关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!