重构建议?

Refactoring suggestions?

本文关键字:构建 重构      更新时间:2023-10-16

我需要一些关于如何重构以下代码的建议。 我有多个配置类,它们都是不同的,但正如您在下面的示例代码中看到的那样,发生了一种重复出现的模式。

我想知道简化视图代码的最佳方法是什么?

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
}

您还需要选择将交换机移动到拥有配置的视图中,或者使配置可供控制器使用。