.NET 抽象类

发布于 2024-07-05 15:13:56 字数 1100 浏览 7 评论 0原文

我正在设计一个网站导航层次结构。 它是一棵节点树。

大多数节点是页面。 有些节点是链接(想想 Windows 中的快捷方式)。

大多数页面都包含 HTML 内容。 有些执行代码。

我想将它们表示为类和抽象(MustInherit)类的集合...

类图

这是数据库表我将在其中存储所有这些...

数据库表http://img178.imageshack。 us/img178/8573/nodetablefm8.gif

这就是我被难住的地方。 PageNode 可能是也可能不是根。

我应该如何处理根类?

类图

我不想拥有全部四个...

  • HtmlPageNode
  • CodePageNode
  • HtmlRoot PageNode
  • CodeRootPageNode

我希望 HtmlPageNode 和 CodePageNode 类从 PageNode 继承,或者从 RootPageNode 继承。 那可能吗?


澄清:有多个根节点,并且根可能有父节点。 每个都是具有不同样式的子树的根。 想想不同的、用颜色编码的部门。 (也许 root 是一个糟糕的名称选择。建议?)


更新:关于“Root”名称...
我问过: 是与子树对应的节点有一个特定的名称吗?

I'm designing a web site navigation hierarchy. It's a tree of nodes.

Most nodes are pages. Some nodes are links (think shortcuts in Windows).

Most pages hold HTML content. Some execute code.

I'd like to represent these as this collection of classes and abstract (MustInherit) classes…

class diagram

This is the database table where I'm going to store all this…

database table http://img178.imageshack.us/img178/8573/nodetablefm8.gif

Here's where I'm stumped. PageNodes may or may not be roots.

How should I handle the root class?

class diagram

I don't want to have to have all four of…

  • HtmlPageNode
  • CodePageNode
  • HtmlRootPageNode
  • CodeRootPageNode

I want the HtmlPageNode and CodePageNode classes to inherit either from PageNode or else from RootPageNode. Is that possible?


Clarification: There are multiple root nodes and roots may have parent nodes. Each is the root of only a sub-tree that has distinct styling. Think of different, color-coded departments. (Perhaps root is a poor name choice. Suggestions?)


Update: Regarding the "Root" name...
I've asked: Is there a specific name for the node that coresponds to a subtree?

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

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

发布评论

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

评论(6

好倦 2024-07-12 15:13:56

PageNode 类是否应该只具有 Root 类型的属性?

替代文本

这是否违背了 PageNode is-a 根的想法。 或者,它们不是“是根”吗,因为只有一些是根?

这是否意味着该属性可能会遍历树来寻找根祖先? 还是只有我这样?

Should the PageNode class simply have a property of type Root?

alt text

Is that counter to the idea that a PageNode is-a Root. Or, are they not "is-a Root" because only some of them are roots?

And does that imply that the property might traverse the tree looking for the root ancestor? Or is that just me?

少女的英雄梦 2024-07-12 15:13:56

澄清:有多个根节点,并且根可能有父节点。 每个都是具有不同样式的子树的根。 想想不同的、用颜色编码的部门。 (也许 root 是一个糟糕的名字选择。有建议吗?)

Root 是一个糟糕的名称选择,因为它(有点讽刺地)被明确地接受为树结构的顶层,因为树从 root 伸出地面的地方开始。 除此之外的任何节点都是分支或叶子,不直接连接到根。

更好的名称类似于 IsAuthoritativeStyleNode、IsCascadingStyleNode、IsStyleParentNode 或对其进行限定:例如 IsDepartmentRootNode。 给事物赋予清晰明确的名称是极大提高可读性/易于理解的事情之一。

仅通过抽象基类/继承并不能真正实现您想要的目标。 根据其他建议,请考虑接口。

我还会考虑考虑是否让数据库模式过多地驱动客户端类设计。 并不是说在这种情况下需要改变,但至少应该考虑一下。 考虑如何将属性分解到引用公共“节点”表的单独表中,并对它们进行规范化以最大限度地减少空值和/或重复的相同数据。

Clarification: There are multiple root nodes and roots may have parent nodes. Each is the root of only a sub-tree that has distinct styling. Think of different, color-coded departments. (Perhaps root is a poor name choice. Suggestions?)

Root is a poor name choice because it's (somewhat ironically) accepted as explicitly the top level of a tree structure, because the tree starts where root comes out of the ground. Any node beyond that is a branch or leaf and not directly attached to the root.

A better name would be something like IsAuthoritativeStyleNode, IsCascadingStyleNode, IsStyleParentNode or instead qualify it: e.g. IsDepartmentRootNode. Giving things clear unambiguous names is one of things that drastically improves readability / easy understanding.

You can't really achieve what you want just via abstract base classes/inheritence. As per other suggestion(s), consider interfaces instead.

I'd also consider thinking about whether you're letting the database schema drive your client side class design too much. Not saying it needs changing in this case, but it should at least be thought about. Think about how you could factor out properties into separate tables referencing the common 'Node' table, and normalize them to minimize nulls and/or duplicated identical data.

请叫√我孤独 2024-07-12 15:13:56

我希望 HtmlPageNode 和 CodePageNode 类继承自 PageNode 或 RootPageNode。 这可能吗?

是的,这是可能的。 您需要让 HtmlPageNode 和 codePageNode 有一个对象,该对象将是 PageNode 和 RootPageNode 继承的抽象类。 在 HtmlPageNode 和 codePageNode 的构造函数中,您接受新的抽象类,在您的情况下,该抽象类将是 PageNode 或 RootPageNode。 这样你就有了 2 个不同的类,它们具有相同的方法,但有两个不同的对象。 希望对您有帮助!

I want the HtmlPageNode and CodePageNode classes to inherit either from PageNode or else from RootPageNode. Is that possible?

Yeah it's possible. You need to have HtmlPageNode and codePageNode have an object that will be an Abstract class that PageNode will inherit and RootPageNode too. In the constructor of HtmlPageNode and codePageNode you accept your new Abstract class that will be in your case PageNode OR RootPageNode. This way you have 2 differents classes with same methods but two differents object. Hope that help you!

征﹌骨岁月お 2024-07-12 15:13:56

实际上,由于“根”节点是节点的特例,因此也许您需要 RootHtmlPageNode : HtmlPageNode。

另一个想法:由于您没有指定“根”节点和普通节点之间的区别,也许只是在节点中指定一个标志来指定它是否是根节点也将是一个很好的设计。

编辑:根据您的澄清,普通节点和根节点之间没有功能差异,因此一个简单的标志应该足够(或属性 IsRootNode)。 如果“根”节点仅提供样式数据(或自身及其子节点的任何其他数据),那么您可以将此样式数据放置在单独的结构/类中并递归地获取它(基于 IsRootNode):

class Node
{
   private bool isRootNode;
   public bool IsRootNode;

   private StylingData stylingData;
   public StylingData StylingData
   {
      set
      {
         if (this.IsRootNode)
            this.stylingData = value;
         else
            throw new ApplicationException("The node is not root.");
      }
      get
      {
         if (this.IsRootNode)
            return this.stylingData;
         else
            return this.parent.StylingData;
      }
   }
}

这假设每个节点有对其父节点的引用。

这已经超出了问题范围,因为我不知道确切的设计。

Actually, as the "root" node is a special case of node, maybe you need RootHtmlPageNode : HtmlPageNode.

Another idea: as you do not specify what is the difference between a "root" and normal node, maybe just a flag in node specifying if it is root or not also will be a good design.

EDIT: Per your clarification, there is no functional difference between normal and root node, so a simple flag should be enough (or property IsRootNode). If "root" node only supplies a styling data (or any other data for itself and it's children), then you can place this styling data in a separate structure/class and fetch it recursively (based on IsRootNode):

class Node
{
   private bool isRootNode;
   public bool IsRootNode;

   private StylingData stylingData;
   public StylingData StylingData
   {
      set
      {
         if (this.IsRootNode)
            this.stylingData = value;
         else
            throw new ApplicationException("The node is not root.");
      }
      get
      {
         if (this.IsRootNode)
            return this.stylingData;
         else
            return this.parent.StylingData;
      }
   }
}

This assumes, that each node has a reference to it's parent.

It become way beyond the question, as I do not know the exact design.

我纯我任性 2024-07-12 15:13:56

使用复合模式


关于您的根节点,功能上是否存在差异,还是完全是外观上的差异? 如果差异仅在于外观,我建议您与 PageNode 中的单独 Style 类关联。

如果功能存在差异并且您有多种类型的页面,请考虑使用装饰器模式

Use the the Composite Pattern.


With regard to your root nodes, are there differences in functionality or is the difference it entirely appearance? If the difference is appearance only I suggest you have an association with a separate Style class from your PageNode.

If there are differences in functionality AND you have lots of types of page then think about using the Decorator Pattern.

把梦留给海 2024-07-12 15:13:56

如前所述,复合模式可能是一个很好的解决方案。

如果这对您不起作用,如果合适的话,将根定义为接口并根据需要应用它可能会更简单。

当然,这并不能让您为 Root 提供任何实现...
如果 Root 必须有实现,则可以使用装饰器模式。

As noted, the Composite Pattern may be a good solution.

If that doesn't work for you, it may be simpler - if appropriate - to define Root as an Interface, and apply that as needed.

Of course, that doesnt let you provide any implementation for Root...
If Root must have implementation, you can use the Decorator Pattern.

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