创建一个空类纯粹是为了将它与另一个类的良好实践区分开来?

时间:2022-09-25 10:36:13

I have a class CardStack. I have several classes that inherit from CardStack e.g. Cascade, Deck, Foundation etc.

我有一个班级CardStack。我有几个继承自CardStack的类,例如级联,甲板,基金会等

Foundation doesn't need to add any functionality to CardStack, but for display purposes my app needs to know which of the CardStacks are actually Foundations.

Foundation不需要向CardStack添加任何功能,但出于显示目的,我的应用程序需要知道哪些CardStack实际上是基础。

Incidentally, I have no such function CardStack.Display() (I'm using a model-view-controller pattern where the View object simply queries the Model to find out what type of objects it's dealing with).

顺便说一句,我没有这样的功能CardStack.Display()(我使用的是模型 - 视图 - 控制器模式,其中View对象只是查询模型以找出它正在处理的对象类型)。

It seems OK to me, but is there any reason not to do this?

对我来说似乎没问题,但有什么理由不这样做吗?

class Foundation : public CardStack
{

};

class Model
{
    Cascade cascade[10];
    Foundation foundations[10];
    ...
};

7 个解决方案

#1


12  

Nothing wrong with this.

这没什么不对。

Do it all the time.

一直这样做。

In the future, there may be a difference in structure, behavior or implementation. For now, they happen to share a lot of common features.

将来,结构,行为或实施可能会有所不同。目前,它们碰巧共享许多共同特征。

#2


5  

I don't see any technical problem with it, so maybe you're doing this for semantic reasons. In that case, make sure you document the reason it VERY CLEARLY so maintenance programmers later on don't try and change things.

我没有看到任何技术问题,所以也许你出于语义原因这样做。在这种情况下,请确保记录它非常清楚的原因,以便稍后维护程序员不要尝试更改内容。

#3


2  

Yep, this is valid and useful. An empty class can act as placeholder for future functionality (as example). Of course, a bit of documentation is in order if the class in question is "connected" to the program in any way ;-)

是的,这是有效和有用的。空类可以充当未来功能的占位符(例如)。当然,如果有问题的课程以任何方式与课程“连接”,则需要提供一些文档;-)

In your case above, the C++ code generated won't be burdened... but readability of your code is increased.

在上面的例子中,生成的C ++代码不会负担......但代码的可读性会增加。

#4


2  

I do it all the time for lists

我一直都在为列表做这件事

public class MyObjects : List<MyObject> { }

#5


1  

It's good practice, since it is semantically clearer with nearly, with nearly no cost associated and allows for modifications, when the need arises for subclasses do behave differently.

这是一个很好的实践,因为它在语义上几乎是更清晰的,几乎没有成本关联并且允许修改,当需要子类时表现不同。

#6


0  

Nothing wrong with it, I do this often. I like it better than empty "marker" interfaces (in Java). As others have mentioned, you should probably comment on the fact that the implementation is supposed to be empty (or perhaps "reserved for future use"), but otherwise IMHO you're fine.

它没有错,我经常这样做。我比空的“marker”接口(在Java中)更喜欢它。正如其他人所提到的那样,你应该评论这个实现应该是空的(或者可能是“保留以备将来使用”)这一事实,但除此之外恕我直言你没事。

#7


0  

The way you did it the Model class it seems to me that typedef will suffice to distinguish names (and readability!): http://en.wikipedia.org/wiki/Typedef

你在Model类中的做法在我看来,typedef足以区分名称(和可读性!):http://en.wikipedia.org/wiki/Typedef

#1


12  

Nothing wrong with this.

这没什么不对。

Do it all the time.

一直这样做。

In the future, there may be a difference in structure, behavior or implementation. For now, they happen to share a lot of common features.

将来,结构,行为或实施可能会有所不同。目前,它们碰巧共享许多共同特征。

#2


5  

I don't see any technical problem with it, so maybe you're doing this for semantic reasons. In that case, make sure you document the reason it VERY CLEARLY so maintenance programmers later on don't try and change things.

我没有看到任何技术问题,所以也许你出于语义原因这样做。在这种情况下,请确保记录它非常清楚的原因,以便稍后维护程序员不要尝试更改内容。

#3


2  

Yep, this is valid and useful. An empty class can act as placeholder for future functionality (as example). Of course, a bit of documentation is in order if the class in question is "connected" to the program in any way ;-)

是的,这是有效和有用的。空类可以充当未来功能的占位符(例如)。当然,如果有问题的课程以任何方式与课程“连接”,则需要提供一些文档;-)

In your case above, the C++ code generated won't be burdened... but readability of your code is increased.

在上面的例子中,生成的C ++代码不会负担......但代码的可读性会增加。

#4


2  

I do it all the time for lists

我一直都在为列表做这件事

public class MyObjects : List<MyObject> { }

#5


1  

It's good practice, since it is semantically clearer with nearly, with nearly no cost associated and allows for modifications, when the need arises for subclasses do behave differently.

这是一个很好的实践,因为它在语义上几乎是更清晰的,几乎没有成本关联并且允许修改,当需要子类时表现不同。

#6


0  

Nothing wrong with it, I do this often. I like it better than empty "marker" interfaces (in Java). As others have mentioned, you should probably comment on the fact that the implementation is supposed to be empty (or perhaps "reserved for future use"), but otherwise IMHO you're fine.

它没有错,我经常这样做。我比空的“marker”接口(在Java中)更喜欢它。正如其他人所提到的那样,你应该评论这个实现应该是空的(或者可能是“保留以备将来使用”)这一事实,但除此之外恕我直言你没事。

#7


0  

The way you did it the Model class it seems to me that typedef will suffice to distinguish names (and readability!): http://en.wikipedia.org/wiki/Typedef

你在Model类中的做法在我看来,typedef足以区分名称(和可读性!):http://en.wikipedia.org/wiki/Typedef