解释为什么双重目的课程不好

发布于 2024-09-28 02:34:13 字数 263 浏览 10 评论 0原文

我在系统中有一个类,将其用途列为“这可以是从午夜开始的几秒。或者是带有日期的时间。”我试图解释这有多糟糕,但我无法理解我的观点。有谁对如何解决这个问题有任何想法。

http://code-slim-jim.blogspot.com /2010/10/object-anti-patterns.html

I have a class in a system that lists its purpose as "This can either be seconds-from midnight. Or a time with a date." I have tried to explian how bad this is but I cant get my point accross. Do anyone have any ideas on how to tackle this.

http://code-slim-jim.blogspot.com/2010/10/object-anti-patterns.html

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

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

发布评论

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

评论(2

掐死时间 2024-10-05 02:34:13

如前所述,这听起来像是变量的问题。如果一个变量(例如,float)可以表示自午夜以来的秒数或时间和日期,那么如何在代码中使用它?如果我想使用它的值,我必须确保我知道它最后是如何设置的,如果我想设置它,我必须确保我知道接下来如何使用它。

从更大的意义上来说,我认为你的意思是,让一个班级执行两项独立的任务有什么问题吗?问题在于它违反了封装性,因为不必要地将一个任务的实现暴露给另一个任务的实现,因此其中一个任务的错误可能会干扰这两个任务。

As stated this sounds like a problem with a variable. If a variable (say, float) can represent either seconds since midnight or time-and-date, then how can it be used in the code? If I want to use its value, I have to make sure I know how it was last set, and if I want to set it I must make sure I know how it well next be used.

In the larger sense I think you mean, what's wrong with having one class performing two independent tasks? The problem is that it violates encapsulation by unnecessarily exposing the implementation of one task to the implementation of another, so that a bug in one can disturb both.

奈何桥上唱咆哮 2024-10-05 02:34:13

我不确定这本质上是错误的。我确信直接使用该类会导致错误。它可能是一个“实现”类,可以在内部用作两个类的基础,一个表示时间+日期,另一个表示自午夜以来的秒数。

然而,大多数可能都是错误的。

人们需要更详细的描述,或者实际的代码,加上有关如何使用它的信息,才能做出决定。

干杯&呵呵,,

I'm not sure that it's inherently wrong. I'm sure that using that class directly is an open invitation to bugs. Possibly it's an "implementation" class that can be used internally as basis for two classes, one which represents time+date, and the other representing seconds since midnight.

However, most probably it's just Wrong.

One would need a more detailed description, or the actual code, plus info about how it's being used, to decide.

Cheers & hth.,

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