需要 Coldfusion 对象继承健全性检查

发布于 2024-09-25 18:36:42 字数 467 浏览 1 评论 0原文

我需要知道我是否以正确的方式做某事。

对于给定页面,我正在为页面本身实例化一个对象。我们将该对象称为 myPage。在页面内我有容器(通常是 div 标签)。当我进入管理组件来处理特定的 div 时,我也会为其实例化一个对象。我们称之为 myDiv。

现在,我想要为给定的 div 处理的事情之一是该 div 的样式。所以通常我会认为我只是放入一些与样式相关的方法,例如 myDiv.getPadding() 或 myDiv.getBackgroundColor() 等。

但我突然想到我最终可能会有其他对象还需要做与风格相关的事情。

鉴于此,我应该创建一个单独的 style.cfc 吗?那么它会被 div 对象扩展吗?或者 style 对象会扩展 div 对象吗?我的理解是,更具体的对象扩展了不太具体的对象,但我不确定在这种情况下哪个更具体:是引用特定 div 的 div 对象,还是提供一组特定的 style 对象数据?

提前致谢!

I need to know if I am going about something the right way.

For a given page, I am instantiating an object for the page itself. Let's call that object myPage. Within the page I have containers (usually div tags). When I go to an admin component to work with a specific div, I instantiate an object for that as well. Let's call that myDiv.

Now, one of the things I want to work with for a given div is the styling of that div. So normally I would think that I'd just put in some style-related methods, such as myDiv.getPadding() or myDiv.getBackgroundColor(), etc.

But it occurs to me that I may eventually have other objects for which I may also need to do style-related stuff.

Given this, should I then create a separate style.cfc? Would that then be extended by the div object? Or would the style object extend the div object? My understanding is that the more specific object extends the less specific one, but I am not sure which is more specific in this case: is it the div object, which references a specific div, or the style object, which provides a specific set of data?

Thanks in advance!

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

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

发布评论

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

评论(2

烂人 2024-10-02 18:36:42

首先,除非您需要即时编写样式,否则我将创建一个或多个样式表并动态链接它们,而不是动态创建它们。

但是,假设您确实需要即时创建它们...

我不会让控件(div)扩展样式,反之亦然。样式并不是 div 的更具体的定义,反之亦然。我要做的是创建一个样式对象,该对象仅包含给定元素或元素集的显示元数据。它可以包含在您的 control/div 对象(不是扩展)中,也可以是页面对象的一部分。样式肯定与控件相关,但我不会将它们组合起来,因为这使得分离内容和呈现变得更加困难。

First, unless you need to write styles on-the-fly, I would create one or more stylesheets and link them dynamically, instead of creating them dynamically.

Assuming, however, that you do need to create them on-the-fly...

I would not have either the control (div) extend the style or vice-versa. A style is not a more specific definition of a div, nor is the reverse true. What I would do is create a style object that only contains the display meta-data for a given element or element set. This can be contained within your control/div object (not an extension), or can be part of the page object. The style is definitely related to the control, but I would not combine them, as that makes it harder to separate content and presentation.

远昼 2024-10-02 18:36:42

我绝不是说这是最好的方法,但如果您确实想使用 CFC 来设计页面样式,您可以使用 DivTag.cfc 扩展 HtmlTag.cfc,它将充当所有 HTML 标记的基类。然后,您可以将 StyleAttribute.cfc 组合到 HtmlTag.cfc 中,以使用任何样式属性,例如背景颜色和填充。所以你最终会调用像 myDiv.getStyle().getPadding() 这样的函数。

一般来说,您确实应该尝试支持组合(“has a”)而不是继承(“is a”),并且不要对组件层次结构过于疯狂。在这种情况下,我建议使用 CSS 文件来设置页面样式。

By no means am I saying this is the best approach, but if you really wanted to use CFCs to style your pages, you could have a DivTag.cfc extend an HtmlTag.cfc, which would act as your base class for all HTML tags. You could then compose a StyleAttribute.cfc into your HtmlTag.cfc to work with any style properties, such as background colors and padding. So then you would end up calling functions like myDiv.getStyle().getPadding().

In general, you should really try to favor composition ("has a") over inheritance ("is a") and not get too crazy with your component hierarchies. In this case, I'd recommend using CSS files to style your pages.

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