子类化或不子类化

发布于 2024-10-09 11:20:01 字数 226 浏览 0 评论 0原文

我有三个对象;行动、问题和风险。这些都包含许多常见变量/属性(例如:描述、标题、截止日期、提出者等)和一些特定字段(风险具有概率)。问题是:

  1. 我应该创建 3 个单独的 行动、风险和问题各类别 包含重复字段。

  2. 创建父类“Abstract_Item” 包含这些字段和 对它们进行操作,然后有 行动、风险和问题子类 摘要_项目。这将坚持 DRY 主体。

I have three objects; Action, Issue and Risk. These all contain a nunber of common variables/attributes (for example: Description, title, Due date, Raised by etc.) and some specific fields (risk has probability). The question is:

  1. Should I create 3 separate
    classes Action, Risk and Issue each
    containing the repeat fields.

  2. Create a parent class "Abstract_Item"
    containing these fields and
    operations on them and then have
    Action, Risk and Issue subclass
    Abstract_Item. This would adhere to
    DRY principal.

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

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

发布评论

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

评论(7

烟燃烟灭 2024-10-16 11:20:01

我的观点

假设您使用了继承。经过一段时间后,您将拥有仅操作问题所共有的新属性,而风险则不会。你将如何处理这个问题?如果你把它们放在父项下,那么风险就是继承不相关的东西(里氏替换原则敲门?)。如果你将 then 分别放在 Action 和 Risk 中,那么你就破坏了 DRY,这是你开始继承的最初原因。要点是重用的继承性不好。如果没有“is-a”,那么最好不要使用它,当你有疑问时,那么就不存在真正的“is-a”。

我的偏好

还有其他实现 DRY 的方法,如下面的示例代码所示。添加新属性后,可能会成为另一个 Common2,如果新行为不适用于所有 3 个类,那么它们就是新的 CommonBehavior2;如果它们是,那么只需更改现有的CommonCommonBehavior

public class Common implements CommonBehavior
{
    String Description;
    String title;

    public void f() {}
}

public interface CommonBehavior
{
    void f();
}

public class Action implements CommonBehavior
{
    private Common delegate;

    public void f()
    {
        delegate.f();
    }
}

另请参阅我对类似问题的回答以及另一个实际示例用于添加新类的设计模式

My Perspective

Let's say, if you used inheritance. Over a period you have new attributes that are common to only Action and Issue but not Risk. How will you handle this? If you put them under parent then Risk is inheriting stuff that is irrelevant (Liskov Substituon Principle knocking?). If you put then in Action and Risk separately then you are breaking DRY, the initial reason why you started inheritance. Point is Inhertence for re-use is bad. If there is no "is-a" then better not use it and when you are in doubt then there is no real "is-a".

My Preference

There are other ways of achieving DRY as shown in below example code. With this addition of new properties my be another Common2, addition of new behavior is new CommonBehavior2 if they are not applicable to all 3 classes; if they are then just change existing Common and CommonBehavior

public class Common implements CommonBehavior
{
    String Description;
    String title;

    public void f() {}
}

public interface CommonBehavior
{
    void f();
}

public class Action implements CommonBehavior
{
    private Common delegate;

    public void f()
    {
        delegate.f();
    }
}

Also look at my answer to a similar question with another practical example Design pattern to add a new class

忆悲凉 2024-10-16 11:20:01

是的,坚持 DRY 通常是一个非常好的主意除非如果这些类有非常非常不同的用途(即苹果和汽车都可能是红色的,但我仍然不会从基础派生它们)名为 ProbouslyRed 的类)。但是,就您而言,我肯定会选择基类,因为您描述的实现(ActionIssueRisk)似乎都是某种具有非常相似语义的业务规则。

Yes, adhering to DRY is usually a very good idea except if the classes have very, very different uses (i.e. both apples and cars may be red, still I wouldn't derive both of them from a base class called ProbablyRed). In your case, however, I'd definitely go for a base class since the implementations you describe (Action, Issue, Risk) all seem to be some kind of business rule with very similar semantics.

懒的傷心 2024-10-16 11:20:01

看来你自己也在回答这个问题。正如你所说,干。

You seem to be answering this yourself. As you say, DRY.

香草可樂 2024-10-16 11:20:01

抽象父类听起来像是一个可行的方法。它还将使实现和使用作用于这三个项目中任何一个的函数成为可能或更容易(取决于您的语言)。例如,您可以有一个“获取 {user} 提出的所有项目的列表”功能。

The abstract parent class sounds like the way to go. It will also make it possible or easier (depending on your language) to implement and use functions which act on any of the three items. For example, you could have a "Get a list of all items raised by {user}" function.

策马西风 2024-10-16 11:20:01

如果您查看用例,另一个方面可能会变得可见,可能涉及属性的不同子组。

例如,如果在类似待办事项的应用程序中使用“标签”、“截止日期”和“提出”,并且在处理该任务时“操作”和“风险”的其他属性将很普遍,那么您可能会考虑聚合任务(标签、截止日期……)和主题,它是对诸如操作、问题或将来会出现的任何内容的多态引用

Another facet may become visible if you look at the use cases, probably dealing with different subgroups of the properties.

If for example "label", "due date" and "raised" are used in a todo like application and other properties of "Action" and "Risk" will be prevalent when working on that task, you might consider an aggregation of lets say Task (label, due date,...) and Topic which is a polymorphic reference to things like Action, Issue or whatever will come up someday

蹲在坟头点根烟 2024-10-16 11:20:01

我想我在这里回答了同样的问题

何时创建类与设置布尔标志?

I guess I have answered the same question here

When to create a class vs setting a boolean flag?

困倦 2024-10-16 11:20:01

不要仅仅因为对象共享某些数据或操作而进行子类化。将组合视为遵循 DRY 的默认方式。

在您的特定情况下,仅当您的对象实际上相关时才创建父类,即与父类存在语义“是”关系。例如,如果 Action、Issue 和 Risk 都是 Ticket 对象。

Don't subclass just because objects share some data or operations. Consider composition as the default way to follow DRY.

In your particular case, only create a parent class if your objects are actually related, i.e. there's a semantic "is a" relationship to the parent class. For example, if Action, Issue, and Risk are all Ticket objects.

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