单一使用的类与“静态类”

发布于 2024-10-05 13:04:13 字数 236 浏览 0 评论 0原文

我有一个程序可以处理不同的对象,这些对象有自己的指令集(比如它们自己的程序),并且我想从主类中运行这些程序。然后我有两个选择:

  • 创建一个类InstructionHandler,在我的主类中创建它的一个实例,并将请求传递给该对象[单例模式-感谢Mark Peters]
  • 创建一个仅包含静态方法的类InstructionHandler,并在我的主课

从软件设计的角度来看,什么是好方法?

I have a program that handles different objects which have their own sort of instructions set (say their own program), and from a main class I want to run these programs. Then I have two choices:

  • Make a class InstructionHandler, create an instance of it in my main class and pass requests to that one object [Singleton pattern - thanks to Mark Peters]
  • Make a class InstructionHandler with just static methods and make use of these in my main class

What would be the good way to go from a software - design point of view?

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

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

发布评论

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

评论(2

月朦胧 2024-10-12 13:04:14

您是否考虑过使用解释器模式?是否可以组合每个对象的指令集来创建某种语法?

Have you considered using the Interpreter Pattern? Can the instruction sets be combined for each of the objects to create some kind of grammar?

迷途知返 2024-10-12 13:04:14

使用 Singleton 会更好。如果您曾经(例如为了测试)想要更改系统的行为,静态方法会干扰它。使用单例,您可以简单地子类化并覆盖相关方法,然后使用子类的实例代替常规类实例。静态方法不可能进行此类替换。

You are much better off with a Singleton. If you ever (for testing, for instance) wanted to change the behavior of your system, static methods interfere with it. With a Singleton, you can simply subclass and override the method(s) of concern, then use an instance of the subclass in place of your regular class instance. No such substitution is possible with static methods.

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