大的switch语句:坏OOP? [英] Large Switch statements: Bad OOP?

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

问题描述

我一直认为大的switch语句是坏OOP设计一个症状。在过去,我读过,讨论这个话题的文章和他们提供altnerative OOP基础的方法,通常是根据多态性来实例化合适的对象来处理这种情况。

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.

我现在在具有根据数据从在该协议包括基本上换行符终止命令的TCP套接字,随后的数据线流的凶猛的switch语句,接着是结束标志的情况。该命令可以是100个不同的命令,所以我想找到一种方法来减少这种怪物switch语句的东西更易于管理。

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.

我已经做了一些使用Google找到我记得的解决方案,但遗憾的是,谷歌已经成为了多种查询这些天不相关的结果荒地。

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.

然而,这也有发生爆炸的问题。我现在需要100个新的类别,再加上我必须找到一个方法来清洁接口他们的数据模型。是一真switch语句真的要走的路?

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?

我倒是AP preciate你的想法,意见或评论。

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

推荐答案

您可能会得到一些好处了命令模式

You may get some benefit out of a Command Pattern.

有关OOP,您可能能够折叠每个几个类似的命令成一个类,如果行为变化足够小,以避免一个完整的类爆炸(是的,我能听到OOP大师们尖叫有关的话)。但是,如果系统已经OOP,每个100+的命令是真正独一无二的,然后只是让他们独特的类和利用继承,巩固共同的东西。

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.

如果系统没有面向对象的话,我就不会只是这个附加OOP ...你可以很容易地使用命令模式用一个简单的字典查找和函数指针,或基于命令的名字甚至动态生成的函数调用取决于语言。然后,你可以逻辑上相关的功能到该重新present类似的命令的集合,实现管理的分离库。我不知道是否有针对这种实现良好的长期...我一直认为它是一个调度员的风格,基于MVC的方法来处理的URL。

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.

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

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