大型嵌套式开关语句的设计模式 [英] Design pattern for a large nested switch statements

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

问题描述

我搜索了一些关于重构一个大的开关语句的文章。



但是他们不要做我想做的事情问题我要去来运行是有一个巨大的切换语句,根据两个不同的值调用不同的方法,让我们说一个类型代码



目前,我将处理这样的情况:

  switch(type)
{
case Types.Type1:
handleType1(code);
break;

case Types.Type2:
handleType2(code);
break;
}

void handleTypeN(code)
{
开关(代码)
{
...
}
}

可能结合工厂和命令模式的东西可以帮助我吗?



你如何重构这段代码?






我可能需要更多地了解我正在面对的情况。



我正在从服务器接收数据包。数据包包含类型和代码以及一些特定信息。



一旦数据到达,我检索类型代码,它进入开关语句类型,在找出类型之后,调用一个特定的方法来执行代码的数据包。



处理代码的方法现在可以进一步解码数据包,并完成流程。

  + ---------- + + ---------- + 
| |数据包| |
|服务器| -------------> |客户端|
| | | |
+ ---------- + + ---------- +
|
|
(打开数据包的类型并调用特定方法)
|
|
(打开数据包的代码并调用一个特定的方法)
|
|
(回复服务器)


解决方案

我认为这取决于你想要做什么样的代码改进。



如果你真的有很大的设计变化,那么我建议多态:



创建一个抽象的Packet类。

为每个数据包类型创建一个类。

创建一个工厂方法,接收原始服务器数据包,并创建正确的数据包类对象。

每个数据包类类型都将自己实现需要执行的任务。



如果您没有做大型设计更改的奢侈品(通常是这样):




  • 如果您想提高可读性:



保持交换机,每个交换机的情况都会调用一个正确命名的函数,以便执行所需的操作。 >


  • 如果要提高性能,请执行以下操作:



创建一个矩阵,每个单元格[T,C]将持有一个函数的引用,该函数将处理一个具有类型T和代码C的数据包。

矩阵应该在程序或类的启动时,启动一次(硬编码,没有办法)。

这将给你比一个长的切换块更好的性能(直接访问代码,没有逻辑比较) p>

I've searched for a number of articles on refactoring a large switch statement.

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.

Currently, I would handle the situation like this:

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.

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:

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):

  • If you want to improve readability :

Keep the switch, each switch case will call a properly named function that will do what it needs to.

  • If you want to increase performance:

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)

这篇关于大型嵌套式开关语句的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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