类设计“困境”

发布于 2024-09-13 02:52:50 字数 299 浏览 2 评论 0原文

我从来没有做过适当的类设计,所以请耐心等待。

假设我们有一个 Project 类。然后,假设我们有一个 ProjectGroup 类,它具有与 Project 类相同的成员,以及一个 Project 列表。

ProjectGroup 应该是 Project 的超类,还是 ProjectGroupProject 的特化,前者应该是后者?

I never did proper class design, so bear with me.

Let's say we have a Project class. Then let's say we have a ProjectGroup class that has the same members as the Project class, plus a list of Projects.

Should ProjectGroup be the superclass of Project or is ProjectGroup a specialization of Project and the former should be a subclass of the latter?

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

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

发布评论

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

评论(3

赠我空喜 2024-09-20 02:52:50

我不会用理论来打扰您,因为您可能急于获得快速答案。所以事情是这样的:

如果您的两个类实际上暗示它们应该通过继承相关,那么 ProjectGroup 应该从 Project 类继承。这就是 C# 中的样子:

public class ProjectGroup: Project ...

如果不是,但它们使用一些公共类成员(定义它们的状态和该状态上的一些功能),那么我将编写一个接口并在两个类中实现它。再次 C# 代码:

public interface ICommonState ...

public class Project: ICommonState ...

public class ProjectGroup: ICommonState
{
    IEnumerable<ICommonState> projects
    ...
}

编辑

如果您的类实际上是 ProjectProjectGroup 并且它们都具有 IDName 等属性共同点(例如),它们仍然不应该被继承。它们只是碰巧具有相同名称的属性,但它们基本上是不同的实体。

它们都可以

  • 实现 ICommonEntity 接口 - 当它们具有相同的状态+功能但功能行为不同时使用它
  • CommonEntity 类继承 - 当功能时使用它完全相同;这样您将遵循DRY(不要重复自己)哲学,

因此您的组件可能是一个接口或一个类(当使用复合模式时)。

当实体暗示彼此相关时,两个类之间的直接继承更合适。就像UserPerson 类一样。它们可以通过任何一种方式继承。根据业务场景而定。

class User: Person  

如果您有一个包含联系人的应用程序,就会出现这种情况。他们中的一些人也是这个应用程序的用户。

class Person: User  

这将是一个您可以注册为用户的网站。如果您还填写了一些个人详细信息,您的用户数据将成为 Person 类型。

I won't bother you with theory, because you're probably in a hurry to get a quick answer. So here it goes:

If your two classes are actually implying they should be related by inheritance then ProjectGroup should inherit from Project class. This is how it would look like in C#:

public class ProjectGroup: Project ...

If they are not, but they use some common class members (that define their state and some functionality over that state), then I'd write an interface and implement it in both classes. C# code again:

public interface ICommonState ...

public class Project: ICommonState ...

public class ProjectGroup: ICommonState
{
    IEnumerable<ICommonState> projects
    ...
}

Edit

If your classes are actually Project and ProjectGroup and they both have properties like ID and Name in common (for instance), they still shouldn't be inherited. They just happen to have properties with the same name, but they are basically different entities.

They could both either

  • implement an ICommonEntity interface - use it when they have the same state+functionality but functionality behaves differently in each of them
  • inherit from CommonEntity class - use it when functionality is completely identical; this way you'll follow the DRY (don't repeat yourself) philosophy

So your component may be an interface or a class (when using composite pattern).

Direct inheritance between two classes is more suitable where entities imply on being in relation to each other. Like User and Person classes. They can be inherited either way. Depending on the business scenario.

class User: Person  

This would be the case where you have an application with contacts. Some of them are also users of this very same application.

class Person: User  

This would be a web site where you can register as a user. In case you fill up some personal details as well your user data becomes of type Person.

似梦非梦 2024-09-20 02:52:50

听起来您可能想要复合模式。 LeafProject和CompositeProject都实现了Project接口,并且CompositeProject还保存了Project实例的集合。

It sounds like you might want the Composite pattern. Both LeafProject and CompositeProject implement the Project interface, and CompositeProject also holds a collection of Project instances.

魂归处 2024-09-20 02:52:50

如果项目的成员列表对于项目组是唯一的并且不适用于所有类型的项目,则将项目设为您的超级/基类并从项目派生项目组。

if the member list of projects is unique to projectgroup and does not apply to all types of projects, then make project your super/base class and derive projectgroup from project.

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