【发布时间】:2009-12-28 23:22:14
【问题描述】:
我的 Controller 类中有一个名为 handlePathChange() 的虚函数。
它会检查当前 URL,并应为其分配正确的视图。
这是我到目前为止的代码:
void Controller::handlePathChange()
{
if ( app->internalPathMatches(basePath) )
{
string path = app->internalPathNextPart(basePath);
if ( path.empty() ) // If it's empty it is known that the index of the controller should show up
index();
// else if ( path == ?? ) each controller has it's own routes
// call_some_unknown_function();
}
}
如何概括这一点?
我在考虑两种选择:
- 调用一个名为 dispatch() 的纯虚函数,它将匹配派生类中正确函数的正确路径。这种解决方案违反了 DRY,因为基本上你会一遍又一遍地编写相同的代码。
- 创建 std::function 的哈希映射,但如果 URL 的一部分是参数,则无法找到视图。所以这个选项还不够好。
有什么想法吗?
【问题讨论】:
标签: c++ model-view-controller wt