重构为模板类

时间:2018-06-02 08:42:59

标签: c++ templates refactoring

我是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,类似于“derived&lt;'Line'&gt;”和“衍生的&lt;&lt;''DotMAtrix'&gt;”同样适用于真实课程。所以最后的调用应该是“真正的&lt;'Line'&gt; line(strData)”和“real&lt;'DotMatrix'&gt; dot(strData)”。

1 个答案:

答案 0 :(得分:0)

我花了一段时间才使你的榜样成为一个有效的例子,但最后我想出了这个:

#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();
}