C ++使用一个不同的语句重构公共代码

我有两种方法
f(vector<int>& x, ....) and g(DBConn& x, ....)
其中(....)参数都是相同的。 除了一个语句之外,两个方法中的代码完全相同 我们根据x的类型执行不同的操作:
in f(): we do x.push_back(i)
in g(): we do x.DeleteRow(i)
将公共代码提取到一个方法中的最简单方法是什么呢? 有两个不同的陈述? 我想有一个模板运算符重载operator()(int a),但这似乎有点过分。     
已邀请:
您可以编写一个带有两个实现的简单适配器,每个实现调用另一个类的所需方法。
class MyInterface {
public:
  virtual doIt(int i) = 0;
}

class VectorImp : public MyInterface {
public:
  vector<int>& v;
  VectorImp(vector<int>& theVector) : v(theVector) {}
  doIt(int i) { x.push_back(i); }
}

class DbImp : public MyInterface {
public:
  DBConn& c;
  VectorImp(DBConn& conn) : c(conn) {}
  doIt(int i) { c.DeleteRow(i); }
}
    
common_function(....)
{
}

f(vector<int>x,... )
{
    x.push_back(i);
    common_f(...);
}
g(DBConn& x, ....)
{
    x.DeleteRow(i);
    common_f(...);
}
    
template<class T>
struct Adapter;

template<>
struct Adapter<vector<int> >
{
  static void execute(vector<int> &x, int i)
  {
    x.push_back(i);
  }
};

template<>
struct Adapter<DBConn>
{
  static void execute(DBConn &x, int i)
  {
    v.DeleteRow(i);
  }
};

template<class T>
void f(T &t, ...)
{
  ...
  Adapter<T>::execute(t, i);
  ...
}
要么:
template<class T>
struct adapter_traits;

template<>
struct adapter_traits<vector<int> >
{
  typedef void (vector<int>::*PMF)(int);
  static const PMF pmf = &vector<int>::push_back;
}

template<>
struct adapter_traits<DBConn>
{
  typedef void (DBConn::*PMF)(int);
  static const PMF pmf = &DBConn::DeleteRow;
}

template<class T>
void f(T &t, ...)
{
  ...
  (t.*adapter_traits<T>::pmf)(i);
  ...
}
注意:我可能有一些语法错误,但你明白了。     
另一个想法:
template<class T>
void f(T &t, void (T::*p)(int), ...)
{
  ...
  (t.*p)(i);
}

void g()
{
  DBConn x;
  vector<int> y;
  f(x, &DBConn::DeleteRow, ...);
  f(y, &vector<int>::push_back, ...);
}
    
仿函数的经典案例:
#include <vector>
#include <DBConn.h>

// T:    The type of the object that is to be manipulated.
// A:    The type of the object that will do the manipulating
//       This may be a functor object or a function pointer.
//
// As this is a template function the template parameters will
// be deduced by the compiler at compile time.
template<typename T,typename A>
void action(T& obj,A const& action/*,....*/)
{
    // Do Stuff
    action(obj,5);
    // Do more Stuff
}

// Functor object
struct MyVectorAction
{
    // Just defines the operator()
    // Make sure it is a const method.
    // This does the unique bit of code. The parameters should be what you pass into action
    void operator()(std::vector<int>& data,int val) const   {data.push_back(val);}
};
void f(std::vector<int>& x)
{
    action(x,MyVectorAction()/*.... Params ....*/);
}


struct MyDBConnAction
{   void operator()(DBConn& data,int val) const   {data.DeleteRow(val);} };
void g(DBConn& x)
{
    action(x, MyDBConnAction());
}

int main()
{
    std::vector<int>    x;

    f(x);
}
    
你可以创建一个具有你所调用的参数的函数(...),这个函数可以实现f()和g()中相同的逻辑。然后,您可以更改f()和g()的实现来调用此新函数,而不是复制逻辑。如果你在你的独特线之前和之后做了重复的事情,要小心。在这种情况下,您可能需要两个功能。无论如何,我认为这比拥有重复的代码块更可取。     

要回复问题请先登录注册