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

时间:2023-03-09 02:37:21
虚函数与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

相关文章