这应该是命名空间还是类?

发布于 2024-12-11 14:02:42 字数 282 浏览 0 评论 0原文

当您有一组彼此之间没有交互的函数时,您可以将它们放置在命名空间中。 (例如,math 命名空间。)

当您有一些公共属性和一组可选的作用于这些属性的函数时,它应该成为一个类。

但是,当您有一组相关函数但没有公共属性时该怎么办?一个例子是事件管理器:您可能只有 subscribe()post()dispatch() 而没有公共属性;但是,您确实具有隐藏的属性,例如订阅者列表和三个函数所作用的事件队列。这应该是一个类还是一个命名空间?

When you have a set of functions that have no interaction between them, you place them in a namespace. (Example, a math namespace.)

When you have some public attributes and optionally a set of functions that act on those attributes, that should become a class.

But what about when you have a set of related functions but no public attributes? An example would be an event manager: you might only have subscribe(), post(), and dispatch() and no public attributes; however you do have hidden attributes like a list of subscribers and an event queue that the three functions act upon. Should this be a class or a namespace?

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

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

发布评论

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

评论(4

娇纵 2024-12-18 14:02:42

任何时候你有行为并声明它都应该是一个类,即使该状态不可公开访问。这样做的一个实际原因是它可以更轻松地对与相关模块交互的其他模块进行单元测试。

Any time you have behavior and state it should be a class, even if the state isn't publicly accessible. One practical reason for this is it makes it easier to unit test other modules that interact with the module in question.

撞了怀 2024-12-18 14:02:42

一个班。您有一个状态,尽管是内部状态,因此您可能需要创建两个事件管理器(两个队列)。

A class. You have a state, albeit internal, so you may want to create two event managers (two queues).

梦明 2024-12-18 14:02:42

这绝对应该是类,因为它有内部状态。如果您需要多个实例怎么办?在这种情况下,命名空间无法帮助你,而类可以。

This should definitely be class, because it has internal state. What if you need more than one instance? In that case namespace can't help you, and class can.

站稳脚跟 2024-12-18 14:02:42

如果您想创建一个或多个具有生命周期的实例,那么它应该是一个类。

事件管理器听起来像是您想要创建、使用然后销毁的东西。所以,应该是一个类。

命名空间实际上只是为了帮助处理非常大的程序而引入的。在大型程序中,您可能有多个开发团队,每个团队都编写代码。对于每个团队来说,确保没有其他人使用与其他人相同的函数或类名称可能是不可行的。或者,事物的名称可能与可能想要使用的第三方库发生冲突。命名空间有助于避免这些问题。如果您没有从事大型项目,那么您可能真的不需要为自己的代码使用命名空间,除非您觉得需要将代码组织到几个命名空间中以保持整洁。使用不必要的小名称空间会使代码难以使用。过分关注代码是否应该位于这个名称空间或那个名称空间中没有多大意义,最好专注于确保代码确实有效。

If you want to create one of more instances of the thing that each have a lifetime, then it should be a class.

An event manager sounds like something you want to create, use and then destroy. So, it should be a class.

namespaces were only really introduced to help with very large programs. In a large program you may have multiple teams of developers each writing code. It may not be feasible for each team to make sure that nobody else uses happens to give a function or class the same name as somebody else. Or, names of things may clash with 3rd party libraries that might want to be used. Namespaces help avoid these problems. If you're not working on a huge project, you probably don't really need to bother ever using namespaces for your own code, unless you feel the need to organize your code into a few namespaces just to keep it neat. Using unnecessarily small namespaces can make code painful to work with. There's not much point in over obsessing about whether code should be in this namespace or that one, it's better to focus on making sure the code actually works.

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