【发布时间】:2021-09-08 18:11:05
【问题描述】:
我的服务器代码上的许多模块/服务/类都有以下模式:
class Sercice1PerClient
{
void method1(int i)
{
printf("%s", StateOfTheClient);
}
void method2(int i)
{
printf("foo %s bar", StateOfTheClient);
}
char *StateOfTheClient;
}
class Service1
{
void method1(int i, int clientID)
{
v[clientID].method1(i);
}
void method2(int i, int clientID)
{
v[clientID].method2(i);
}
std::vector<Sercice1PerClient> v
}
我面临的问题是每次我想更改method1我都必须这样做4次(2次在cpp中,2次在标题中)
有没有更好的方法来保持每个客户端的状态彼此分离,而不必重复“母类”中的所有功能?
这个模式有名字吗,你觉得合理吗?
【问题讨论】:
-
我不相信有办法在不深入元编程领域的情况下解决这个问题(这绝对不能解决任何问题);即便如此,这也不应该是可能的。但是,您可以使用参数包和
std::forward将上面的代码模板化。这至少应该消除一些冗余,
标签: c++ design-patterns server