重构为模板类
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();
}
相关文章:
- 如何重构类层次结构以避免菱形问题
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- C++-将具有引用的长参数列表重构为结构
- 重构使用动态强制转换的 std::set 的比较运算符
- C++中的命名空间重构
- 将具有相反操作数的两个函数重构为一个
- 重构模板类,该类将其嵌套类用作另一个类的模板参数
- 我当前实现的双向链表类是否需要重构迭代器 end() 功能?
- C++重构,扩展方法typedef
- 重构类:无法将派生类中成员函数的公共代码移回基类
- 重构MFC消息映射以包括完全限定的成员函数指针
- 重构建议?
- 包装器函数可以重构吗?
- 重构此代码以进行优化
- 将 Arduino 草图重构为 C++ 类
- 重构为模板类
- 重构 C++ 中的特定嵌套循环
- 重构嵌套绑定以使用 lambda 函数
- Xcode 9 重构 对C++的支持
- 重构开关或 if/else 语句?