大开关语句:坏OOP? [英] Large Switch statements: Bad OOP?

查看:137
本文介绍了大开关语句:坏OOP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直以来认为大型的switch语句是不好的OOP设计的一个症状。过去,我已经阅读了讨论这个主题的文章,他们提供了基于OOP的高级方法,通常基于多态来实例化正确的对象来处理这个问题。



我现在处于一种基于来自TCP套接字的数据流的怪异的switch语句,其中协议由基本上是newline终止的命令组成,后跟数据行,后跟一个结束标记。该命令可以是100个不同的命令之一,所以我想找到一种方法来将这个怪物切换语句减少到更易于管理的位置。



我已经做了一些谷歌搜索找到我回忆的解决方案,但可悲的是,Google已经成为这些天的多种查询的无关结果的荒谬。



这种类型的问题?有关可能实现的任何建议?一个人以为我是使用字典查找,将命令文本与实例化的对象类型相匹配。这有一个很好的优点,只需创建一个新的对象,并在新的命令表中插入一个新的命令/类型。



然而,这也有类型爆炸的问题。我现在需要100个新的类,加上我必须找到一种方法来将它们干净地与数据模型进行接口。



感谢您的想法,意见或评论。

解决方案

您可能会从命令中获得一些好处格式



对于OOP,您可能可以将多个类似的命令折叠到单个类中,如果行为变化足够小,以避免完整的类爆炸(是的,我可以听到OOP大师对此已经发出了尖锐的声音)。然而,如果系统已经是OOP,并且100个命令中的每一个都是真正唯一的,那么只需使它们成为唯一的类,并利用继承来整合常见的东西。



如果系统不是OOP,那么我不会为此添加OOP ...您可以轻松地使用命令模式,使用简单的字典查找和函数指针,甚至基于命令名称动态生成函数调用,取决于语言。然后,您可以将逻辑关联函数分组到表示类似命令的集合的库中,以实现可管理的分离。我不知道这种实现是否有一个好的术语...我总是认为它是一种调度员风格,基于MVC处理URL的方式。


I've always been of the opinion that large switch statements are a symptom of bad OOP design. In the past, I've read articles that discuss this topic and they have provided altnerative OOP based approaches, typically based on polymorphism to instantiate the right object to handle the case.

I'm now in a situation that has a monsterous switch statement based on a stream of data from a TCP socket in which the protocol consists of basically newline terminated command, followed by lines of data, followed by an end marker. The command can be one of 100 different commands, so I'd like to find a way to reduce this monster switch statement to something more manageable.

I've done some googling to find the solutions I recall, but sadly, Google has become a wasteland of irrelevant results for many kinds of queries these days.

Are there any patterns for this sort of problem? Any suggestions on possible implementations?

One thought I had was to use a dictionary lookup, matching the command text to the object type to instantiate. This has the nice advantage of merely creating a new object and inserting a new command/type in the table for any new commands.

However, this also has the problem of type explosion. I now need 100 new classes, plus I have to find a way to interface them cleanly to the data model. Is the "one true switch statement" really the way to go?

I'd appreciate your thoughts, opinions, or comments.

解决方案

You may get some benefit out of a Command Pattern.

For OOP, you may be able to collapse several similar commands each into a single class, if the behavior variations are small enough, to avoid a complete class explosion (yeah, I can hear the OOP gurus shrieking about that already). However, if the system is already OOP, and each of the 100+ commands is truly unique, then just make them unique classes and take advantage of inheritance to consolidate the common stuff.

If the system is not OOP, then I wouldn't add OOP just for this... you can easily use the Command Pattern with a simple dictionary lookup and function pointers, or even dynamically generated function calls based on the command name, depending on the language. Then you can just group logically associated functions into libraries that represent a collection of similar commands to achieve manageable separation. I don't know if there's a good term for this kind of implementation... I always think of it as a "dispatcher" style, based on the MVC-approach to handling URLs.

这篇关于大开关语句:坏OOP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆