(原创)C++11改进我们的程序之简化我们的程序(八)

本次要讲的是如何通过泛型函数来简化我们的程序。

泛型函数除了之前介绍的一些优点外还有两个重要的优点

1.消除重复逻辑,提高程序的内聚性和健壮性

泛型函数在某种程度上用来弥补泛型类型的不足。通过泛型类型T可以消除一些重复代码,当不同的类型具有相同的行为时,通过泛型擦除类型,达到消除重复简化代码的目的。例如:

template<typename T>
auto Add(T a, T b)->decltype(a+b)
{
    return a + b;
}

void TestAdd()
{
    auto r1 = Add(1, 2); //3
    auto r2 = Add(1.2, 3.5); //4.7
    auto r3 = Add(string("aa"), string("bb")); //aabb
}

我们可以通过一个泛型Add函数对不同的类型:int、double和string做加运算,而不用针对每种类型写一个Add函数,复用了Add函数,消除了重复。本质 上通过T擦出了类型,使得我们可以复用这些不同类型的共同行为。也就是说T可以消除类型不同而行为相同导致的重复,这在很多时候可以有效的消除重复代码,但是有时候却不能消 除某些函数的重复逻辑。比如,两个函数中大部分逻辑都是相同的,只是某个处理步骤略有不同时就无能为力了。也就是说大部分逻辑相同,只是某个行为有差异,只是通过泛型类型T就帮不上忙了,因为泛型类型T只能擦除类型,而不能屏蔽不同的行为。比如:

//遍历xml文件,将标签IDAttr的值和AlgorithmTypeAttr属性值组成键值对,存到map中
void ParseA(const string& confFile, const string& xmlPath,unordered_multimap<uint32_t, int>& map)
{
    property_tree::ptree ptree;
    property_tree::read_xml(confFile, ptree);
    auto kpichilds = ptree.get_child(xmlPath);
    std::for_each(kpichilds.begin(), kpichilds.end(), [this, &map](const ptree::value_type& pair)
    {
        string str = pair.second.get<string>(IDAttr);
        uint32_t key = strtol(str.data(), nullptr, 16);

        int val = pair.second.get<int>(AlgorithmTypeAttr);
        map.insert(make_pair(key, val));
    });
}

//遍历xml文件,将标签AlgorithmAttr属性值和其子节点的标签Attr属性值组成键值对,存入map中
void ParseB(const string& confFile, const string& xmlPath,unordered_multimap<uint32_t, string>& map)
{
    property_tree::ptree ptree;
    property_tree::read_xml(confFile, ptree);
    auto kpichilds = ptree.get_child(xmlPath);
    std::for_each(kpichilds.begin(), kpichilds.end(), [&map](const ptree::value_type& pair)
    {
        int type = pair.second.get<int>(AlgorithmAttr);
        auto childs = pair.second.get_child(Tag);
        for (auto item : childs)
    {
    bap.insert(make_pair(type, item.second.get<string>    (Attr)));
    }
});
}

这两个函数大部分逻辑都是相似的,只是在最后组成键值对时稍有差异,这两个函数中存在重复代码和重复逻辑(遍历生成键值对),重复代码比较好消除,但是重复逻辑该如何消除呢?这时我们就需要通过泛型的函数来屏蔽行为的差异,使得逻辑保持一致,各处仍然可以复用该逻辑代码。重构后代码是这样的:

//通过F屏蔽细小的行为差异,使得各个函数可以复用这段代码
template<typename F>
void Parse(const string& confFile, const string& xmlPath, const F& f)
{
    property_tree::ptree ptree;
    property_tree::read_xml(confFile, ptree);
    auto kpichilds = ptree.get_child(xmlPath);
    std::for_each(kpichilds.begin(), kpichilds.end(), f);
}

//内部复用Parse逻辑
void ParseA(unordered_multimap<uint32_t, int>& map)
{
    Parse(ConfFileName, KPIPath, [this, &map](const ptree::value_type& pair)
{
    string str = pair.second.get<string>(IDAttr);
    uint32_t key = strtol(str.data(), nullptr, 16);

    int val = pair.second.get<int>(AlgorithmTypeAttr);
    map.insert(make_pair(key, val));
};);
}

//内部复用Parse逻辑
void ParseA(unordered_multimap<uint32_t, string>& map)
{
    Parse(ConfFileName, KPIPath, [this, &map](const ptree::value_type& pair)
  {
      int type = pair.second.get<int>(AlgorithmAttr);
      auto childs = pair.second.get_child(Tag);
      for (auto item : childs)
      {
          bap.insert(make_pair(type, item.second.get<string>(Attr)));
      }
  };);
}

重构后,细微的行为差异通过泛型的函数F来屏蔽了,具体的有差异的行为在外面定义,从而获得了逻辑上的一致,并且增强了灵活性。也许有人觉得如此费心思去消除差异来重用一段逻辑代码未免有点过头了。其实不然,高质量的代码就是要消除重复逻辑,应该所有的逻辑只在一个地方制定,尽可能多的复用已有逻辑。消除重复逻辑也可以使得我们的代码具有较高的内聚性,可以做到一个bug只修改一个地方,也间接的让外面的调用接口都充当了测试函数,提高了程序的健壮性。关于消除重复逻辑的好处不多说了。让我们看看泛型函数如何实现非继承的多态调用吧。

2.非继承的多态调用

通过泛型函数可以不需继承就可以实现多态,因为F可以在外面定义,所以你可以调用相同的接口而实现不同的行为,达到多态调用的目的。比如:有一个聚合算法接口,通过派生分别实现加和减。

struct IAggregate
{
  virtual int Aggregate(int x, int y) = 0;
};

struct Add : public IAggregate
{
  int Aggregate(int x, int y) 
  {
    return x+y;
  }
};

struct Sub : public IAggregate
{
  int Aggregate(int x, int y) 
  {
    return x-y;
  }
};

void TestAggregate()
{
  //加法
  shared_ptr<IAggregate> ptrAdd(new Add);
  ptrAdd->Aggregate(3,4);

  //减法
  shared_ptr<IAggregate> ptrSub(new Sub);
  ptrSub->Aggregate(3,4);
}

通过泛型函数的写法是这样的:

template<typename T, typename F>
auto Aggregate(const T& x, const T& y, F& f)->decltype(f(x,y))
{
  return f(x,y);
}

void TestAggregate()
{
  //加法
  Aggregate(3,4, [](int x, int y){return x+y;});

  //减法
  Aggregate(3,4, [](int x, int y){return x-y;});
}

我们可以看到通过泛型函数实现多态调用更简洁,消除了继承的强耦合,也提高了性能。

至此,c++11改进我们的程序之简化我们的程序系列的介绍告一段落了,接下来,会介绍c++11如何优化我们的程序性能以及c++11工程实践系列,敬请关注。

c++11 boost技术交流群:296561497,欢迎大家来交流技术。
原文链接: https://www.cnblogs.com/qicosmos/p/3327217.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/104725

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午7:46
下一篇 2023年2月10日 上午7:53

相关推荐