gpt4 book ai didi

c++ - 我怎样才能改进这个 C++ 代码

转载 作者:可可西里 更新时间:2023-11-01 16:54:43 27 4
gpt4 key购买 nike

我需要您对以下伪代码的建议。请建议我如何改进它,是否可以使用一些设计模式。


// i'm receiving a string containing : id operation arguments
data = read(socket);
tokens = tokenize(data," "); // tokenize the string based on spaces
if(tokens[0] == "A") {
if(tokens[1] == "some_operation") {
// here goes code for some_operation , will use the remaining tokens as arguments for function calls
}
else if(tokens[1] == "some_other_operation") {
// here goes code for some_other_operation , will use the remaining tokens
}
...
else {
// unknown operation
}
}
else if(tokens[0] == "B") {
if(tokens[1] == "some_operation_for_B") {
// do some operation for B
}
else if(tokens[1] == "yet_another_operation") {
// do yet_another_operation for B
}
...
else {
// unknown operation
}
}

我希望你明白我的意思。问题是我有大量的 id's 并且每个都有自己的 operations ,我认为有 10 屏代码包含很多 有点难看如果是else 如果是

最佳答案

每个 ID 都有一个实现公共(public)接口(interface)的类。基本上是 Strategy pattern IIRC.

所以你会像这样调用(伪)代码:

StrategyFactory.GetStrategy(tokens[0]).parse(tokens[1..n])

关于c++ - 我怎样才能改进这个 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/239344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com