虚函数与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++是面向对象语言,面向对象有个重要特点,就是继承和多态.继承之前学过了,就是一种重用类的设计方式.原有的类叫父类,或者基类,继承父类的类叫子类.在设计模式中,我们总是要避免继承,推荐用组合.因为继 ...
随机推荐
- MySql数据库常用语句汇总
第一天1.登陆数据库 mysql -uroot -proot; //-u用户名 -p密码2.启动数据库 net start mysql;3.创建表空间(数据库)create database qy97 ...
- 外购半成品报SHORT问题(验货客户)
描述:下图中可以看到外购半成品层物料报SHORT 2.开始检查数据 --针对外购半成品(外购半成品的成品层有BOM数据,外购半成品没有BOM数据) '; --select * from TB_ADDB ...
- Could not load conf for core new_core 解決方法
new_core: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load ...
- 【Django】关于scss 的安装
今天看视频教程的时候发现老师的样式文件改用了scss(然鹅我买的1块钱特价课程其实是节选出来的,所以前面没有看到过关于scss的介绍) 然后我本以为把原来的css改名字为scss就行,然鹅没有效果. ...
- 22. Generate Parentheses (backTracking)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- iOS - iphoneX系列 - 全局配置的基本信息
/// 获得当前窗口 var JY_WINDOW: UIWindow? { get{ if let app = UIApplication.shared.delegate as? AppDeleg ...
- Head First Servlets & JSP 学习笔记 第一章 —— 前言和体系结构
URL,Uniform Resource Locatiors,统一资源定位符. http:// www.wickedlysmart.com :80 /beeradivice/select /beer1 ...
- jsplumb流程器使用3--connector
jsPlumb.getInstance内可以放一个对象 对象内可选地提供默认值: connector: 连接器(直线--a straight line, 贝塞尔曲线--a Bezier curv ...
- Vue 数据的双向绑定
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- python tcp 粘包问题解决、文件下载等
from socket import * #以下是关于tcp:服务端 和 客户端的小例子#服务端socket_server = socket(AF_INET, SOCK_STREAM) socket_ ...