我应该使用什么设计,以便类可以查询它的祖先之一?

发布于 2024-08-13 06:17:58 字数 485 浏览 9 评论 0原文

我正在创建一个对象层次结构,它表示在控件上绘制的表格。我的层次结构如下所示:

Table has multiple pages
Page has multiple lines
Line has multiple cells
Cell has multiple glyph

我想在表格上有一个选项(参数)来过滤显示的列(单元格)。客户端代码可以执行如下操作:

myTable.ShowColumns(8,12) // Will display columns 8 to 12

在控件上显示和放置单元格是 Lines 对象的责任。如何将要显示哪些单元格的信息从 Table 对象传递到 Line 对象?

我应该为每一行提供对表对象的引用吗?每次调用 Table.ShowColumns() 时,我是否应该尝试通过层次结构将信息传递到每一行?

一定有一种优雅的方式吗?

I'm creating an object hierarchy that is representing a table that is draw on a control. My hierarchy looks like this :

Table has multiple pages
Page has multiple lines
Line has multiple cells
Cell has multiple glyph

I want to have an option (a parameter) on the table to filter the column (cells) displayed. The client code can do something like this:

myTable.ShowColumns(8,12) // Will display columns 8 to 12

Displaying and placing cells on the control is the responsibility of the Lines objects.How can I pass the informations of which cells are to be displayed from the Table object to the Line object?

Should I give each line a reference to the table object? Should I try to pass the informations to each lines through the hierarchy each time Table.ShowColumns() is called?

There must be an elegant way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-08-20 06:17:58

我认为不需要设计模式。 (或者我不会这样称呼它)
为什么不使用双重链接,让子级链接到父级,反之亦然?

I don't think that there is need for a design pattern. (Or I wouldn't call it that way)
Why don't you just use double linking where childs link to the parent and vice versa?

彼岸花ソ最美的依靠 2024-08-20 06:17:58

如果我理解正确的话,您所指的祖先实际上是您的层次结构中的父母 - 实际上,这只不过是。对于树来说,常见的做法是让子节点引用它们各自的父节点。

至于 ShowColumns 函数的设计,我认为它应该通过 Lines 类上的类似内部函数调用来修改 Lines 类(保存单元格)的内部状态变量。

// something like this..
Table.ShowCollumns -> Table.m_lines.SetVisibleColumns -> (modify visible columns)

显然,根据列到页以及页到表的逻辑映射,您可能必须在调用 Lines 类上的 SetVisibleColumns 之前插入另一个调用,以便找到正确的页面对象:Table.FindPageWithColumns(...),然后对该对象进行操作。

If I understand correctly, what you refer to as ancestors are actually parents in your hierarchy - which in effect if nothing more than a tree. And with trees it is common practice to have child nodes reference their respective parents.

As to the design of the ShowColumns function, I think it should modify an internal state variable of the Lines class (which holds the cells) by virtue of a similar internal function call on the Lines class.

// something like this..
Table.ShowCollumns -> Table.m_lines.SetVisibleColumns -> (modify visible columns)

Obviously, depending on the logical mapping of columns to pages, and pages to tables, you may have to interject yet another call before calling SetVisibleColumns on the Lines class, in order to find the correct page object: Table.FindPageWithColumns(...) and then operate on that one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文