重构建议?
Refactoring suggestions?
我需要一些关于如何重构以下代码的建议。 我有多个配置类,它们都是不同的,但正如您在下面的示例代码中看到的那样,发生了一种重复出现的模式。
我想知道简化视图代码的最佳方法是什么?
class IConfiguration1
{
public:
virtual bool Save(const std::string& Output) = 0;
virtual bool OutText(const std::string& BaseFileName) = 0;
virtual bool Open() = 0;
}
class IConfiguration2
{
public:
virtual bool Save(const std::string& Output) = 0;
virtual bool OutText(const std::string& BaseFileName) = 0;
virtual bool Update() = 0;
}
class MockConfiguration
{
MOCK_METHOD1(Save,bool(const std::string& Output));
MOCK_METHOD1(OutText, bool(const std::string& BaseFileName));
}
void View::SaveConfiguration1(std::string path)
{
m_Configuration1->Save(path);
m_Configuration1->OutText(wxFileName::StripExtension(path).ToStdString())
//Enable Reset Menu
wxMenuItem* item2 = GetMenuBar()->FindItem(wxID_RESET);
if (item2 != NULL) item2->Enable(true);
}
void View::SaveConfiguration2(std::string path)
{
m_Configuration2->Save(path);
m_Configuration2->OutText(wxFileName::StripExtension(path).ToStdString());
//Enable Reset Menu
wxMenuItem* item2 = GetMenuBar()->FindItem(wxID_RESET);
if (item2 != NULL) item2->Enable(true);
}
void View::SaveConfiguration3(std::string path)
{
m_Configuration3->Save(path);
m_Configuration3->OutText(wxFileName::StripExtension(path).ToStdString());
//Enable Reset Menu
wxMenuItem* item2 = GetMenuBar()->FindItem(wxID_RESET);
if (item2 != NULL) item2->Enable(true);
}
这是调用保存的函数
void Controller::SaveCurrentSettings()
{
switch (m_View->GetSelectedConfiguration())
{
case Options::Configuration1:
{
SaveConfiguration1();
}
break;
case Options::Configuration2:
{
SaveConfiguration2();
}
break;
case Options::Configuration3:
{
SaveConfiguration3();
}
break;
}
}
创建一个所有配置通用的(抽象(接口。 对于显示的代码,这将包括"保存"和"输出文本",但不包括"打开"或"更新"。
从该接口派生配置
将 SaveConfiguration1、SaveConfiguration2 替换为单个函数 SaveConfiguration 方法,该方法具有一个附加参数,允许您选择要保存的正确配置。正如 Jarod42 评论的那样,该参数可能是配置本身(作为对我提到的新通用接口的引用传递(。否则,该参数可以是用于在 SaveConfiguration 方法中选择配置的枚举。
无论哪种方式,当您到达重复的 Save 和 OutText 行时,您将通过新界面调用它们,并依靠虚拟函数的强大功能来处理不同配置不相同的事实,方法是为您调用特定版本的 Save 和 OutText。
。评论后编辑"我已经有一个枚举 1 级别">
所以代替:-
case Options::Configuration1:
{
// your example call doesn't pass a view, but your SaveConfiguration requires one
SaveConfiguration1(/* view? */);
}
你可以有
case Options::Configuration1:
{
// your example call doesn't pass a view, but your SaveConfiguration requires one
SaveConfiguration(/* view? */ , m_Configuration1);
}
SaveConfiguration 采用 IConfiguration 和
void View::SaveConfiguration(std::string path, IConfiguration& config)
{
config.Save(path);
config.OutText(wxFileName::StripExtension(path).ToStdString())
//Enable Reset Menu
wxMenuItem* item2 = GetMenuBar()->FindItem(wxID_RESET);
if (item2 != NULL) item2->Enable(true);
}
class IConfiguration
{
public:
virtual bool Save(const std::string& Output) = 0;
virtual bool OutText(const std::string& BaseFileName) = 0;
// no output or update
}
您还需要选择将交换机移动到拥有配置的视图中,或者使配置可供控制器使用。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 无法在 CLion 中构建 C++ 项目
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用ndk-build.cmd构建Android.so文件
- 如何重构类层次结构以避免菱形问题
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 构建一个由C和C++文件组成的库
- llvm构建器向基本块添加终止符
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- 如何跨平台将二进制资源构建到程序中?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 如何解决 Ninja c++ 构建和执行问题
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- CMake WxWidgets项目成功地在Linux上构建,但没有在Windows上构建
- 更改命令行 qt5 源代码构建配置的正确/快速方法