大型嵌套 switch 语句的设计模式 [英] Design pattern for a large nested switch statements
问题描述
我搜索了许多关于重构大型 switch
语句的文章.
I've searched for a number of articles on refactoring a large switch
statement.
但他们没有做我想做的事.我要去遇到的问题是有一个巨大的 switch 语句,它根据两个不同的值调用不同的方法,比如说一个 type
和一个 代码
.
But they don't do what I want to do. The problem I'm going to to run in to is having a gigantic switch statement which calls a different method depending on two different values, lets say a type
and a code
.
目前,我会这样处理:
switch (type)
{
case Types.Type1:
handleType1(code);
break;
case Types.Type2:
handleType2(code);
break;
}
void handleTypeN(code)
{
switch (code)
{
...
}
}
也许结合了工厂模式和命令模式的东西可以帮助我?我一定遗漏了一些明显的东西.
Maybe something which combines the factory and command pattern would help me out? I must be missing something obvious.
你会如何重构这段代码?
How would you refactor this code?
我可能需要更具体地了解我所面临的情况.
I might need to get a little more specific as to what scenario I'm facing.
我正在从服务器接收数据包.一个数据包包含一个类型和一个代码以及一些特定的信息.
I'm receiving packets from a server. A packet contains a type and a code and some specific information.
一旦数据到达,我就会检索数据包的 type
和 code
,然后它会进入 switch
语句的 type
,在搞清楚type
后,调用特定方法对包的code
进行切换.
As soon as data arrives I retrieve the type
and the code
of the packet and it goes in to the switch
statement for the type
, after figuring out the type
a specific method is called to perform a switch on the code
of the packet.
处理代码的方法现在进一步解码数据包并完成该过程.
The method that handles the code now decodes the packet further and the process is done.
+----------+ +----------+
| | Packet | |
| Server | -------------> | Client |
| | | |
+----------+ +----------+
|
|
(Switch on the type of the packet and call a specific method)
|
|
(Switch on the code of the packet and call a specific method)
|
|
(Respond to the server or not)
推荐答案
我认为这取决于你想要做什么样的代码改进.
I think it depends what kind of code improvement you're trying to do.
如果你有幸实际进行大的设计更改,那么我建议使用多态:
If you have the luxury of actually making big design changes, then I'd suggest polymorphism:
创建一个抽象的 Packet 类.
为每种数据包类型创建一个类.
创建一个工厂方法,它接收原始服务器数据包,并创建正确的数据包类对象.
每个数据包类类型都将有自己的实现它需要做的工作.
Create an abstract Packet class.
Create a class for each packet type.
Create a factory method, that receives a raw server packet, and creates the right packet class object.
Each packet class type will have its own implementation of the job it needs to do.
如果您没有进行大型设计更改的奢侈(通常是这种情况):
If you don't have the luxury of doing large design changes (which is often the case):
- 如果您想提高可读性:
保留开关,每个开关案例都会调用一个正确命名的函数来完成它需要做的事情.
Keep the switch, each switch case will call a properly named function that will do what it needs to.
- 如果您想提高性能:
创建一个矩阵,该矩阵对于每个单元格 [T,C] 都将包含对一个函数的引用,该函数将处理类型为 T 和代码 C 的数据包.
矩阵应该在程序或类启动时启动一次(硬编码,没有办法).
这会给你比长开关块更好的性能(直接访问代码,没有逻辑比较)
Create a matrix, that for each cell [T,C] will hold a reference to a function that will handle a Packet with Type T and Code C.
The matrix should be initiated once (hard-coded, no way around that) at startup of program or class.
This will give you better performance than a long switch block (direct access to code, no logical comparisons)
这篇关于大型嵌套 switch 语句的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!