使用访问者模式重构代码

时间:2019-03-18 10:23:50

标签: c++ design-patterns refactoring

尝试重构代码时遇到问题。它在类似编译器的结构中。 代码结构如下。 我有几个目标平台可以生成我的代码,例如x86。

class Op
{
public:
  virtual doA(vector<int>& vec) = 0;
  virtual doB(unordered_map<int>& map) = 0;
};

---Directory x86----
class X86_Op_A: public Op 
{
public:
  virtual doA(vector<int>& vec){
      // genereate A type operator that do something under x86
  }
  virtual doB(unordered_map<int>& map){
     // genereate A type operator that do something under x86
  }
};

class X86_Op_B: public Op
public:
  virtual doA(vector<int>& vec){
      // genereate B type operator that do something under x86
  }
  virtual doB(unordered_map<int>& map){
     // genereate B type operator that do something under x86
  }
}; 

---Director arm---
class Arm_Op_A: public Op 
class Arm_Op_B: public Op
like the same code described above.

如何重构代码,使它们看起来像访客模式,我可以将其提供给我的目标平台和我想做的操作。

我尝试过考虑编写一个抽象平台界面并以不同的目标计划形式实现。但是,我仍然很难正确编写代码。如果在这种情况下无法使用访问者模式,是否有任何方法可以重构代码?

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助!

在以下情况下,访问者模式会更好: 1-由于某种原因,您想将doA和doB操作分开在不同的类中; 2-您的X86_Op_A,X86_Op_B,Arm_Op_A,Arm_Op_B类具有其他方法,并且您不希望doA和doB操作与这些方法一起使用;

class X86_Op_A;
class X86_Op_B;
class Arm_Op_A;
class Arm_Op_B;

class OpVisitor
{
public:
    OpVisitor();
    virtual ~OpVisitor();
public:
    virtual void visit(X86_Op_A*) = 0;
    virtual void visit(X86_Op_B*) = 0;
    virtual void visit(Arm_Op_A*) = 0;
    virtual void visit(Arm_Op_B*) = 0;
};
#include "OpVisitor.h"
#include "vector"
#include "iostream"

class DoAVisitor :
    public OpVisitor
{
public:
    DoAVisitor(std::vector<int>& vec): m_vec(vec)
    {
    }
    virtual ~DoAVisitor();
public:
    virtual void visit(X86_Op_A*)
    {
        std::cout << "genereate A type operator that do something under x86" << "\n";
    }
    virtual void visit(X86_Op_B*)
    {
        std::cout << "genereate B type operator that do something under x86" << "\n";
    }
    virtual void visit(Arm_Op_A*)
    {
        std::cout << "genereate A type operator that do something under Arm" << "\n";
    }
    virtual void visit(Arm_Op_B*)
    {
        std::cout << "genereate B type operator that do something under x86" << "\n";
    }
protected:
    std::vector<int>& m_vec;
};

#include "OpVisitor.h"
#include "iostream"
#include "unordered_map"

class DoBVisitor :
    public OpVisitor
{
public:
    DoBVisitor(std::unordered_map<int, int>& map): m_map(map)
    {
    }
    virtual ~DoBVisitor();
public:
    virtual void visit(X86_Op_A*)
    {
        std::cout << "genereate A type operator that do something under x86" << "\n";
    }
    virtual void visit(X86_Op_B*)
    {
        std::cout << "genereate B type operator that do something under x86" << "\n";
    }
    virtual void visit(Arm_Op_A*)
    {
        std::cout << "genereate A type operator that do something under Arm" << "\n";
    }
    virtual void visit(Arm_Op_B*)
    {
        std::cout << "genereate B type operator that do something under x86" << "\n";
    }
protected:
    std::unordered_map<int, int>& m_map;
};

#include "Op.h"
#include "OpVisitor.h"

class X86_Op_A :
    public Op
{
public:
    X86_Op_A();
    virtual ~X86_Op_A();
public:
    virtual void Accept(OpVisitor& v)
    {
        v.visit(this);
    }
};
#include "Op.h"
#include "OpVisitor.h"

class X86_Op_B :
    public Op
{
public:
    X86_Op_B();
    virtual ~X86_Op_B();
public:
    virtual void Accept(OpVisitor& v)
    {
        v.visit(this);
    }
};
#include <iostream>
#include "X86_Op_A.h"
#include "DoAVisitor.h"
#include "vector"

int main()
{
    X86_Op_A var;

    std::vector<int> vec;

    DoAVisitor visitor(vec);

    var.Accept(visitor);

    std::cout << "Hello World!\n"; 
}