类设计“困境”
我从来没有做过适当的类设计,所以请耐心等待。
假设我们有一个 Project
类。然后,假设我们有一个 ProjectGroup
类,它具有与 Project
类相同的成员,以及一个 Project
列表。
ProjectGroup
应该是 Project
的超类,还是 ProjectGroup
是 Project
的特化,前者应该是后者?
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 Project
s.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会用理论来打扰您,因为您可能急于获得快速答案。所以事情是这样的:
如果您的两个类实际上暗示它们应该通过继承相关,那么
ProjectGroup
应该从Project
类继承。这就是 C# 中的样子:如果不是,但它们使用一些公共类成员(定义它们的状态和该状态上的一些功能),那么我将编写一个接口并在两个类中实现它。再次 C# 代码:
编辑
如果您的类实际上是
Project
和ProjectGroup
并且它们都具有ID
和Name
等属性共同点(例如),它们仍然不应该被继承。它们只是碰巧具有相同名称的属性,但它们基本上是不同的实体。它们都可以
CommonEntity 类继承 - 当功能时使用它完全相同;这样您将遵循DRY(不要重复自己)哲学,
因此您的组件可能是一个接口或一个类(当使用复合模式时)。
当实体暗示彼此相关时,两个类之间的直接继承更合适。就像
User
和Person
类一样。它们可以通过任何一种方式继承。根据业务场景而定。如果您有一个包含联系人的应用程序,就会出现这种情况。他们中的一些人也是这个应用程序的用户。
这将是一个您可以注册为用户的网站。如果您还填写了一些个人详细信息,您的用户数据将成为
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 fromProject
class. This is how it would look like in C#: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:
Edit
If your classes are actually
Project
andProjectGroup
and they both have properties likeID
andName
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
ICommonEntity
interface - use it when they have the same state+functionality but functionality behaves differently in each of themCommonEntity
class - use it when functionality is completely identical; this way you'll follow the DRY (don't repeat yourself) philosophySo 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
andPerson
classes. They can be inherited either way. Depending on the business scenario.This would be the case where you have an application with contacts. Some of them are also users of this very same application.
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
.听起来您可能想要复合模式。 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.
如果项目的成员列表对于项目组是唯一的并且不适用于所有类型的项目,则将项目设为您的超级/基类并从项目派生项目组。
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.