重构为模板类

Refactor to template class

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

我是C++语言和学习模板的新手。 我需要将以下类重构为模板化类。以下类中存在多个重复的代码,但继承的基类除外。LineDerived 和 DotMatrixderived 类的功能是相同的,只是它们的基类和虚函数不同。RealLine 和 RealDotMatrix 类也是如此。

class LineDerived: public printer::printerType::Line
{
public:
LineDerived(const std::wstring& str1) : _str(str1) {}
virtual const std::wstring getData() override { return _str; }
private:
const std::wstring _str;
};
class DotMatrixderived: public printer::printerType::DotMatrix
{
public:
DotMatrixderived(const std::wstring& str1) : _str(str1) {}
virtual const std::wstring getData() override { return _str; }
private:
const std::wstring _str;
};
class RealLine : public printer::system::LineSystem
{
public:
RealLine (const std::wstring& str1) : 
LineSystem(boost::make_shared<LineDerived>(str1))
{
}
virtual method1() { .. };
virtual method2() { .. };
};
class RealDotMatrix : public printer::system::DotMatrixSystem
{
public:
RealDotMatrix (const std::wstring& str1) : 
DotMatrixSystem(boost::make_shared<DotMatrixderived>(str1))
{
}
virtual method1() { .. };
virtual method2() { .. };
};

// Calling Part:
RealLine line(strData);
line.method1();
line.method2();
RealDotMatrix matrix(strData);
matrix.method1();
matrix.method2()

我想做一些事情,其中只有一个派生类,类型作为打印机类型,即 Line 和 DotMatrix,类似于"派生<'Line'>"和"派生<<'DotMAtrix'>",同样适用于真正的类。所以最终的调用应该像"real<'Line'> line(strData("和"real<'DotMatrix'>dot(strData("。

我花了一段时间才让你的例子成为一个工作示例,但最后我想出了这个:

#include <string>
#include <memory>
// don't have boost now so I use stl to make the example work
namespace boost { using std::make_shared; using std::shared_ptr; }
namespace printer::printerType {
class PrinterTypeBase {};
class Line : public PrinterTypeBase {
public:
virtual const std::wstring getData() = 0;
virtual ~Line(){}
}; 
class DotMatrix : public PrinterTypeBase {
public:
virtual const std::wstring getData() = 0;
virtual ~DotMatrix(){}
};
}
namespace printer::system {
class LineSystem
{
public:
LineSystem(boost::shared_ptr<printer::printerType::PrinterTypeBase>);
};
class DotMatrixSystem
{
public:
DotMatrixSystem(boost::shared_ptr<printer::printerType::PrinterTypeBase>);
};
}
template <typename T>
class Derived: public T::printerType
{
public:
Derived(const std::wstring& str1) : _str(str1) {}
virtual const std::wstring getData() override { return _str; }
private:
const std::wstring _str;
};
template <typename T>
class Real : public T::systemType
{
public:
Real(const std::wstring& str1) : 
T::systemType(boost::make_shared<typename T::printerType>(str1))
{
}
virtual void method1() { /*..*/ }
virtual void method2() { /*..*/ }
virtual ~Real() {}
};

struct DotMatrix
{
using printerType = printer::printerType::DotMatrix;
using systemType = printer::system::DotMatrixSystem;
};
struct Line
{
using printerType = printer::printerType::Line;
using systemType = printer::system::LineSystem;
};

int main(int, char**)
{
const std::wstring strData;
// Calling Part:
Real<Line> line(strData);
line.method1();
line.method2();
Real<DotMatrix> matrix(strData);
matrix.method1();
matrix.method2();
}