虚函数与bind 实现设计模式的练习
相同模式使用虚函数与bind function进行实现对比
#include "stdafx.h"
#include <iostream>
#include <functional>
#include <windows.h> class Calculater {
public:
virtual int calculate(int x, int y) = ;
}; class Minuss :public Calculater {
public:
int calculate(int x, int y) {
return x - y;
}
}; class Pluss :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);
}
};
//=======================================================
class NewCalcuClient {
private:
std::function<int(int, int)> pm_function;
public:
NewCalcuClient(std::function<int(int, int)> function) :pm_function(function) {}
int calculate(int x, int y) {
return (pm_function)(x, y);
}
}; void test1() {
DWORD TimeStart = GetTickCount();
for (int j = ; j < ; j++) {
for (int i = ; i < ; i++) {
Minuss m;
Pluss p;
CalcuClient c(&m);
c.calculate(, );
//std::cout << c.calculate(3, 8) << std::endl;;
CalcuClient c1(&p);
c1.calculate(, );
//std::cout << c1.calculate(3, 8) << std::endl;;
}
}
auto TimeEnd = GetTickCount();
auto TimeUsed = TimeEnd - TimeStart;
std::cout << __FUNCTION__ << " " << TimeUsed << std::endl;
} void test2() {
DWORD TimeStart = GetTickCount();
for (int j = ; j < ; j++) {
for (int i = ; i < ; i++) {
Minuss m1;
Pluss p1;
NewCalcuClient newclient((std::bind(&Minuss::calculate, &m1, std::placeholders::_1, std::placeholders::_2)));
NewCalcuClient newclient2((std::bind(&Pluss::calculate, &p1, std::placeholders::_1, std::placeholders::_2)));
newclient.calculate(, );
newclient2.calculate(, );
/*std::cout << newclient.calculate(3, 8) << std::endl;;;
std::cout << newclient2.calculate(3, 8) << std::endl;;;*/
}
}
auto TimeEnd = GetTickCount();
auto TimeUsed = TimeEnd - TimeStart;
std::cout << __FUNCTION__ << " " << TimeUsed << std::endl;
} int main()
{
test1();
test2(); return ;
}
责任链模式
// 555.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <memory>
#include <functional> 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 == ) {
std::cout << "request handled in ConcreteHandler1" << std::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 == ) {
std::cout << "request handled in ConcreteHandler2" << std::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 == ) {
std::cout << "request handled in ConcreteHandler3" << std::endl;
}
else {
if (m_next != nullptr) {
m_next->HandleRequest(request);
}
}
}
};
//=========================================================
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);
} int main()
{
Test();
return ;
}
command
// 444.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional> using namespace std; class Command {
public:
virtual void execute() = ;
}; class Hello :public Command {
public:
void execute() { cout << "Hello "; }
}; class World :public Command {
public:
void execute() { cout << "world! "; }
}; class IAm :public Command {
public:
void execute() { cout << "I'm the command pattern!"; }
}; class Macro {
vector<Command*> commands;
public:
void add(Command* c) { commands.push_back(c); }
void run() {
vector<Command*>::iterator it = commands.begin();
while (it != commands.end())
(*it++)->execute();
}
};
//========================================================
class myCommand {
private:
std::function<void()> function_;
public:
void SetFunc(std::function<void()> function) {
function_ = function;
}
void calculate()const {
return function_();
}
}; class myMacro {
vector<myCommand> commands;
public:
void add(myCommand c) {
commands.push_back(c);
}
void run() {
for (const auto& it : commands) {
it.calculate();
}
}
}; int main()
{
Macro macro;
macro.add(new Hello);
macro.add(new World);
macro.add(new IAm);
macro.run();
std::cout << std::endl;
std::cout << std::endl;
//============================
myMacro mm;
myCommand a;
a.SetFunc([] {std::cout << "Hello "; });
myCommand b;
b.SetFunc([] {std::cout << "world!"; });
myCommand c;
c.SetFunc([] {std::cout << " I'm the command pattern!"; });
mm.add(a);
mm.add(b);
mm.add(c);
mm.run();
std::cout << std::endl;
return ;
}
command
proxy
// 666.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <functional> using namespace std; class ProxyBase {
public:
virtual void f() = ;
virtual void g() = ;
virtual void h() = ;
virtual ~ProxyBase() {}
}; class Implementation :public ProxyBase {
public:
void f() { cout << __FUNCTION__ << endl; }
void g() { cout << __FUNCTION__ << endl; }
void h() { cout << __FUNCTION__ << endl; }
}; class Proxy :public ProxyBase {
ProxyBase* implementation;
public:
Proxy() { implementation = new Implementation(); }
~Proxy() { delete implementation; }
void f() { implementation->f(); }
void g() { implementation->g(); }
void h() { implementation->h(); }
};
//=====================================================
void f() { cout << "function " << __FUNCTION__ << endl; }
void g() { cout << "function " << __FUNCTION__ << endl; }
void h() { cout << "function " << __FUNCTION__ << endl; }
class myProxy {
std::function<void()> func_f;
std::function<void()> func_g;
std::function<void()> func_h;
public:
void set(std::function<void()> f, std::function<void()> g, std::function<void()> h)
{
func_f=(f); func_g=(g); func_h=(h);
}
void run() {
func_f();
func_g();
func_h();
} }; int main()
{
Proxy p;
p.f();
p.g();
p.h();
std::cout << endl;
//====================================
myProxy pp;
pp.set(f,g,h);
pp.run(); return ;
}
proxy
虚函数与bind 实现设计模式的练习的更多相关文章
- 应该用bind+function取代虚函数吗?
用bind+function取代虚函数在好几年前就有人提出了,曾引起广泛的讨论,有支持的有反对的,可能赞成的人占大多数.这个话题挺有趣,本来是作为技术沙龙的开放性话题来讨论的,由于时间关系并没有讨论. ...
- 以boost::function和boost:bind取代虚函数
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...
- boost::function和boost:bind取代虚函数
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...
- c++ virturn function -- 虚函数
c++ virturn function -- 虚函数 pure irtual function -- 纯虚函数 先看例子 #include <iostream> using nam ...
- 读书笔记 effective c++ Item 35 考虑虚函数的替代者
1. 突破思维——不要将思维限定在面向对象方法上 你正在制作一个视频游戏,你正在为游戏中的人物设计一个类继承体系.你的游戏处在农耕时代,人类很容易受伤或者说健康度降低.因此你决定为其提供一个成员函数, ...
- c++ 虚函数和纯虚函数
在你设计一个基类的时候,如果发现一个函数需要在派生类里有不同的表现,那么它就应该是虚的.从设计的角度讲,出现在基类中的虚函数是接口,出现在派生类中的虚函数是接口的具体实现.通过这样的方法,就可以将对象 ...
- C++基础(纯虚函数与抽象类)
C++基础之纯虚函数与抽象类 引言 纯虚函数在C++编程中的地位很重要,其关联到了设计模式中"接口"的概念. 语法 纯虚函数的语法: 1. 将成员函数声明为virtual 2. ...
- C++ 虚函数与纯虚函数
#include<iostream> #include<string> using namespace std; class A{ public: virtual void f ...
- c++学习之多态(虚函数和纯虚函数)
c++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
随机推荐
- 微信小程序开发攻略
首先,需要明确的一点是,小程序开发就是前端开发的一个小分支. 其次,小程序开发框架是一个精简版的React ,并且开发比较简单 . 第一步 获取AppId 小程序注册入口http://https:// ...
- excel表格输入思想
1.创建工作簿 SXSSFWorkbook wb = new SXSSFWorkbook(); //#设置单元格的垂直居中,水平居中,字体颜色 2.创建sheet Sheet sheet = wb ...
- https方式下 git push 每次都要输入密码的解决办法
转载自:http://git.oschina.net/oschina/git-osc/issues/2586 作者:Zoker https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输 ...
- 判断python字典中key是否存在
- centos 7 下 Ceph 配置安装
一.环境介绍 系统: CentOS Linux release 7.3.1611 (Core) 硬盘: 系统盘:300GB*2-raid 1 OSD:600GB*4-raid 5 ceph ...
- hadoop深入简出(二)
1.上传文件 Hadoop fs -put hello.txt / 2.查看上传的文件 hadoop fs -ls / hadoop fs -text /hello.txt 两个命令都可以 3.创建文 ...
- vue-app项目,将px自动转化为rem
1. 安装lib-flexible: npm install --save lib-flexible 2.安装postcss-loader和postcss-px2rem: npm install -- ...
- Wannafly挑战赛13 D.applese的生日(贪心+思维)
题目描述 最可爱的applese生日啦,他准备了许多个质量不同的蛋糕,想请一些同学来参加他的派对为他庆生,为了不让一部分同学感到不爽,他决定把每个蛋糕都分割成几份(也可以不分割),使得最小的蛋糕的质量 ...
- cloud server ribbon 自定义策略配置
虽然ribbon默认为我们提供了多钟负载均衡策略,但有时候我们仍然需要自定义符合自身业务逻辑的规则 使用配置文件的方式:我们只需要在配置文件中添加配置 serviceId.ribbon.NFLoadB ...
- 小白鼠排队(map容器插入数据的四种方法)
题目描述 N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的重量,要求按照白鼠重量从大到小的顺序输出它们头上帽子的颜色.帽子的颜色用“red”,“ ...