开发者

Single used class vs. 'static class'

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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜