用bind+function取代虚函数在好几年前就有人提出了,曾引起广泛的讨论,有支持的有反对的,可能赞成的人占大多数。这个话题挺有趣,本来是作为技术沙龙的开放性话题来讨论的,由于时间关系并没有讨论。今天就来具体探讨一下这个问题,我将做两个实验来验证一下这两种做法,具体是实现两个模式:策略模式和责任链模式。我将分别用经典的虚函数和bind+function来实现这两个模式。通过这两个实验来得出我的结论。

实验一:策略模式的实现

1.虚函数方式实现策略模式

class Calculater
{
public:
virtual int calculate(int x, int y) = ;
}; class Minus : public Calculater
{
public:
int calculate(int x, int y)
{
return x - y;
}
}; class Plus : public Calculater
{
public:
int calculate(int x, int y)
{
return x + y;
}
}; class CalcuClient
{
private:
Calculater* m_caculater;
public:
CalcuClient(Calculater* caculater) : m_caculater(caculater){} int calculate(int x, int y)
{
return m_caculater->calculate(x, y);
}
};

2.bind+function方式实现策略模式

class NewCalcuClient
{
private:
std::function<int(int, int)> m_function; public:
NewCalcuClient(std::function<int(int, int)> function) : m_function(function){} int calculate(int x, int y)
{
return m_function(x, y);
}
};

测试代码:

    Minus minus;
CalcuClient client(&minus); Plus plus;
CalcuClient client2(&plus); int r = client.calculate(, );
int r2 = client2.calculate(, );

   //bind+function
NewCalcuClient newclient(boost::bind(&Minus::calculate, &minus, _1, _2));
NewCalcuClient newclient2(boost::bind(&Plus::calculate, &plus, _1, _2)); int r3 = newclient.calculate(, );
int r4 = newclient2.calculate(, );

  bind+function取代虚函数的一个重要理由是虚函数带来了效率损失,bind+function效率更高,我做了一个性能测试, 分别调用10000000次来看耗时,发现虚函数比bind+function方式要快一些,无论是用标准库的bind还是boost的bind,都比虚函数方式要慢,所以说bind+function比虚函数性能更好是想当然,站不住脚的。接下来看第二个实验。

实验二:责任链模式的实现

1.虚函数方式实现责任链模式

struct Request
{
int RequestType;
}; class Handler
{
protected:
std::shared_ptr<Handler> m_next;
public:
Handler(std::shared_ptr<Handler> next) : m_next(next){} virtual void HandleRequest(Request) = ;
}; class ConcreteHandler1 : public Handler
{
public:
ConcreteHandler1(std::shared_ptr<Handler> next) : Handler(next){} void HandleRequest(Request request)
{
if (request.RequestType == )
{
cout << "request handled in ConcreteHandler1" << endl;
}
else
{
if (m_next != nullptr)
m_next->HandleRequest(request);
}
}
}; class ConcreteHandler2 : public Handler
{
public:
ConcreteHandler2(std::shared_ptr<Handler> next) : Handler(next){} void HandleRequest(Request request)
{
if (request.RequestType == )
{
cout << "request handled in ConcreteHandler2" << endl;
}
else
{
if (m_next != nullptr)
m_next->HandleRequest(request);
}
}
}; class ConcreteHandler3 : public Handler
{
public:
ConcreteHandler3(std::shared_ptr<Handler> next) : Handler(next){} void HandleRequest(Request request)
{
if (request.RequestType == )
{
cout << "request handled in ConcreteHandler3" << endl;
}
else
{
if (m_next != nullptr)
m_next->HandleRequest(request);
}
}
};

2.bind+function方式实现责任链模式

class ChainHandler
{ public:
std::function<void(Request)> function; void HandleRequest(Request request)
{
function(request);
} std::function<void(Request)>& getfunction()
{
return function;
}
}; void assemble(std::function<void(Request)> call, std::function<void(Request)> next, Request request)
{
if (next != nullptr)
next(request);
else
call(request);
}

测试代码:

void Test()
{
auto thirdHandler = std::make_shared<ConcreteHandler3>(nullptr);
auto secondHandler = std::make_shared<ConcreteHandler2>(thirdHandler);
auto firstHandler = std::make_shared<ConcreteHandler1>(secondHandler); Request request = { };
firstHandler->HandleRequest(request); ChainHandler chain; std::function<void(Request)> f1 = std::bind(&ConcreteHandler1::HandleRequest, firstHandler, std::placeholders::_1);
std::function<void(Request)> f2 = std::bind(&ConcreteHandler2::HandleRequest, secondHandler, std::placeholders::_1);
std::function<void(Request)> f3 = std::bind(&ConcreteHandler3::HandleRequest, thirdHandler, std::placeholders::_1); chain.function = std::bind(&assemble, f1, chain.function, std::placeholders::_1);
chain.function = std::bind(&assemble, f2, chain.function, std::placeholders::_1);
chain.function = std::bind(&assemble, f3, chain.function, std::placeholders::_1); chain.HandleRequest(request);
}

bind+function实现责任链模式的关键代码在这里:

chain.function = std::bind(&assemble, f1, chain.function, std::placeholders::_1);
chain.function = std::bind(&assemble, f2, chain.function, std::placeholders::_1);
chain.function = std::bind(&assemble, f3, chain.function, std::placeholders::_1); chain.HandleRequest(request);

  这几行代码通过assemble不断地往function链条中加function,最后调用的时候会从链条的第一个function开始调用。

  bind+function取代虚函数的另外一个理由是松耦合,去除了继承的限制,方法的实现更加灵活,确实,低耦合确实是bind+function最大的优点,然而这个最大的优点也成了它最大的缺点,当需要替代的虚函数增多时,组装function的复杂度也在增加,太松散了导致代码也不够直观,代码的内聚性也变低了。比如上面责任链模式的实现,虚函数的实现明显比bind+function的实现要优雅。

结论

  bind+function相比虚函数的实现在性能上并不占优,最大的优点是大大降低类之间的耦合度,缺点是太过于松散导致代码的内聚性和可读性降低。

  bind+function适用的场景:

  1.迫切需要接口和实现解耦;

  2.需要解耦的接口很少。

  满足这两种情况适合用bind+function,否则还是用虚函数更好。

应该用bind+function取代虚函数吗?的更多相关文章

  1. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  2. boost::function和boost:bind取代虚函数

    以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...

  3. Virtual Function(虚函数)in c++

    Virtual Function(虚函数)in c++ 用法: virtual void log() { std::cout << "hello world!" < ...

  4. C++ polymorphism Virtual Function 多态 虚函数

    Polymorphism in C++ https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm https://github.com ...

  5. c++ virturn function -- 虚函数

    c++ virturn function -- 虚函数 pure irtual function  -- 纯虚函数   先看例子 #include <iostream> using nam ...

  6. 虚函数与bind 实现设计模式的练习

    相同模式使用虚函数与bind function进行实现对比 #include "stdafx.h" #include <iostream> #include <f ...

  7. 【深度探索c++对象模型】Function语义学之虚函数

    虚函数的一般实现模型:每一个class有一个virtual table,内含该class中的virtual function的地址,然后每个object有一个vptr,指向virtual table. ...

  8. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

  9. 纯虚函数(pure virtual function )和抽象类(abstract base class)

    函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...

随机推荐

  1. 移植到windows下的iconv

    This is a short memo about installing iconv on Windows host (specifically: Windows 7 SP1 x64). Iconv ...

  2. Android 蓝牙通信——AndroidBluetoothManager

    转载请说明出处! 作者:kqw攻城狮 出处:个人站 | CSDN To get a Git project into your build: Step 1. Add the JitPack repos ...

  3. (原)tensorflow中提示CUDA_ERROR_LAUNCH_FAILED

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6606092.html 参考网址: https://github.com/tensorflow/tens ...

  4. Notes of O_DIRECT flag

    What is O_DIRECT Starting with kernel 2.4, Linux allows an application to bypass the buffer cache wh ...

  5. 【RS】Using graded implicit feedback for bayesian personalized ranking - 使用分级隐式反馈来进行贝叶斯个性化排序

    [论文标题]Using graded implicit feedback for bayesian personalized ranking (RecSys '14  recsys.ACM ) [论文 ...

  6. 【DeepLearning】UFLDL tutorial错误记录

    (一)Autoencoders and Sparsity章节公式错误: s2 应为 s3. 意为从第2层(隐藏层)i节点到输出层j节点的误差加权和. (二)Support functions for ...

  7. 重整ADO.NET连接池相关资料

    https://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.connectionstring(VS.80) ...

  8. Docker stop停止/remove删除所有容器

    原文地址:https://blog.csdn.net/superdangbo/article/details/78688904 docker ps // 查看所有正在运行容器$ docker stop ...

  9. Docker学习笔记 ---存储与管理

    存储管理 为了适应不同平台不同场景的存储需求,Docker提供了各种基于不同文件系统实现的存储驱动来管理实际的镜像文件 元数据管理 镜像在设计上将元数据和文件存储完全隔离.Docker管理元数据采用的 ...

  10. golang 学习 ---- channel

    把一个loop放在一个goroutine里跑,我们可以使用关键字go来定义并启动一个goroutine: package main import "fmt" func loop() ...