虚函数与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++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
随机推荐
- @Scope 注解
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)这个是说在每次注入的时候回自动创建一个新的bean实例 @Scope(value=Config ...
- Flux architecture
[Flux architecture] Flux is a pattern for managing data flow in your application. The most important ...
- 物料没加DUMMY
会加入DUMMY的表 IN_ITEM,IN_ITEM_SITE,IN_SALES_ORDER 加入DUMMY的存储过程名为SAP_MATERIAL_SO. FP_CHANGE_MO_ROUTING的第 ...
- oracle 简单输出语句与赋值
输出: declare stuName varchar2(30); stuAge number; begin stuName:='jack'; stuAge:=30; dbms_output.put_ ...
- as3.0复制影片简介(自我复制的三种形式)
//mc是被复制影片简介的实例名,(===在库中找到mc影片简介,右击“属性”,点击“为actionscript导出”,选中确定即可===这个是重点) var newSprite:Sprite=mc; ...
- 跳台阶(python)
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). # -*- coding:utf-8 -*- class Soluti ...
- 英文谚语:Take that with a grain of salt
take sth. with a grain of salt 这个习语的字面意思是“和一撮盐一起吃下去”,为什么要与盐一起吃呢? 据说这个习语要追溯到罗马时代,罗马将军庞培曾发现一种解毒剂,必须和着一 ...
- 从零开始写一个npm包及上传
最近刚好自己需要写公有npm包及上传,虽然百度上资料都能找到,但是都是比较零零碎碎的,个人就来整理下,如何从零开始写一个npm包及上传. 该篇文件只记录一个大概的流程,一些细节没有记录. tips: ...
- label 和input/textarea居中对齐
设置label和input/textarea的vertical-align: middle;即可实现垂直方向居中对齐.有时候可能会有偏差,设置input的margin-top使看上去居中对齐
- 牛客网 Wannafly挑战赛12 删除子串(线性dp)
题目描述 给你一个长度为n且由a和b组成的字符串,你可以删除其中任意的部分(可以不删),使得删除后的子串“变化”次数小于等于m次且最长. 变化:如果a[i]!=a[i+1]则为一次变化.(且新的字符串 ...