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

相同模式使用虚函数与bind function进行实现对比

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <functional>
 4 #include <windows.h>
 5 
 6 class Calculater {
 7 public:
 8     virtual int calculate(int x, int y) = 0;
 9 };
10 
11 class Minuss :public Calculater {
12 public:
13     int calculate(int x, int y) {
14         return x - y;
15     }
16 };
17 
18 class Pluss :public Calculater {
19 public:
20     int calculate(int x, int y) {
21         return x + y;
22     }
23 };
24 
25 class CalcuClient {
26 private:
27     Calculater * m_caculater;
28 public:
29     CalcuClient(Calculater* caculater):m_caculater(caculater){}
30     int calculate(int x, int y) {
31         return m_caculater->calculate(x, y);
32     }
33 };
34 //=======================================================
35 class NewCalcuClient {
36 private:
37     std::function<int(int, int)> pm_function;
38 public:
39     NewCalcuClient(std::function<int(int, int)> function) :pm_function(function) {}
40     int calculate(int x, int y) {
41         return (pm_function)(x, y);
42     }
43 };
44 
45 
46 void test1() {
47     DWORD TimeStart = GetTickCount();
48     for (int j = 0; j < 100; j++) {
49         for (int i = 0; i < 100000; i++) {
50             Minuss m;
51             Pluss p;
52             CalcuClient c(&m);
53             c.calculate(3, 8);
54             //std::cout << c.calculate(3, 8) << std::endl;;
55             CalcuClient c1(&p);
56             c1.calculate(3, 8);
57             //std::cout << c1.calculate(3, 8) << std::endl;;
58         }
59     }
60     auto TimeEnd = GetTickCount();
61     auto TimeUsed = TimeEnd - TimeStart;
62     std::cout << __FUNCTION__ << "  " << TimeUsed << std::endl;
63 }
64 
65 void test2() {
66     DWORD TimeStart = GetTickCount();
67     for (int j = 0; j < 100; j++) {
68         for (int i = 0; i < 100000; i++) {
69             Minuss m1;
70             Pluss p1;
71             NewCalcuClient newclient((std::bind(&Minuss::calculate, &m1, std::placeholders::_1, std::placeholders::_2)));
72             NewCalcuClient newclient2((std::bind(&Pluss::calculate, &p1, std::placeholders::_1, std::placeholders::_2)));
73             newclient.calculate(3, 8);
74             newclient2.calculate(3, 8);
75             /*std::cout << newclient.calculate(3, 8) << std::endl;;;
76             std::cout << newclient2.calculate(3, 8) << std::endl;;;*/
77         }
78     }
79     auto TimeEnd = GetTickCount();
80     auto TimeUsed = TimeEnd - TimeStart;
81     std::cout << __FUNCTION__ << "  " << TimeUsed << std::endl;
82 }
83 
84 
85 int main()
86 {
87     test1();
88     test2();
89     
90     return 0;
91 }
View Code

 

责任链模式

  1 // 555.cpp: 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <iostream>
  6 #include <memory>
  7 #include <functional>
  8 
  9 struct Request {
 10     int RequestType;
 11 };
 12 
 13 class Handler {
 14 protected:
 15     std::shared_ptr<Handler> m_next;
 16 public:
 17     Handler(std::shared_ptr<Handler> next) :m_next(next) {}
 18     virtual void HandleRequest(Request) = 0;
 19 };
 20 
 21 class ConcreteHandler1 :public Handler {
 22 public:
 23     ConcreteHandler1(std::shared_ptr<Handler> next) :Handler(next) {}
 24     void HandleRequest(Request request) {
 25         if (request.RequestType == 1) {
 26             std::cout << "request handled in ConcreteHandler1" << std::endl;
 27         }
 28         else {
 29             if (m_next != nullptr) {
 30                 m_next->HandleRequest(request);
 31             }
 32         }
 33     }
 34 };
 35 
 36 class ConcreteHandler2 :public Handler {
 37 public:
 38     ConcreteHandler2(std::shared_ptr<Handler> next) :Handler(next) {}
 39     void HandleRequest(Request request) {
 40         if (request.RequestType == 2) {
 41             std::cout << "request handled in ConcreteHandler2" << std::endl;
 42         }
 43         else {
 44             if (m_next != nullptr) {
 45                 m_next->HandleRequest(request);
 46             }
 47         }
 48     }
 49 };
 50 
 51 class ConcreteHandler3 :public Handler {
 52 public:
 53     ConcreteHandler3(std::shared_ptr<Handler> next) :Handler(next) {}
 54     void HandleRequest(Request request) {
 55         if (request.RequestType == 3) {
 56             std::cout << "request handled in ConcreteHandler3" << std::endl;
 57         }
 58         else {
 59             if (m_next != nullptr) {
 60                 m_next->HandleRequest(request);
 61             }
 62         }
 63     }
 64 };
 65 //=========================================================
 66 class ChainHandler {
 67 public:
 68     std::function<void(Request)> function;
 69     void HandleRequest(Request request) {
 70         function(request);
 71     }
 72     std::function<void(Request)>& getfunction() {
 73         return function;
 74     }
 75 };
 76 
 77 void assemble(std::function<void(Request)> call, std::function<void(Request)> next, Request request) {
 78     if (next != nullptr) {
 79         next(request);
 80     }
 81     else {
 82         call(request);
 83     }
 84 }
 85 
 86 //============================================================
 87 
 88 void Test() {
 89     auto thirdHandler = std::make_shared<ConcreteHandler3>(nullptr);
 90     auto secondHandler = std::make_shared<ConcreteHandler2>(thirdHandler);
 91     auto firstHandler = std::make_shared<ConcreteHandler1>(secondHandler);
 92 
 93     Request request = { 3 };
 94     firstHandler->HandleRequest(request);
 95 
 96     ChainHandler chain;
 97 
 98     std::function<void(Request)> f1 = std::bind(&ConcreteHandler1::HandleRequest, firstHandler, std::placeholders::_1);
 99     std::function<void(Request)> f2 = std::bind(&ConcreteHandler2::HandleRequest, secondHandler, std::placeholders::_1);
100     std::function<void(Request)> f3 = std::bind(&ConcreteHandler3::HandleRequest, thirdHandler, std::placeholders::_1);
101 
102     chain.function = std::bind(&assemble, f1, chain.function, std::placeholders::_1);
103     chain.function = std::bind(&assemble, f2, chain.function, std::placeholders::_1);
104     chain.function = std::bind(&assemble, f3, chain.function, std::placeholders::_1);
105 
106     chain.HandleRequest(request);
107 }
108 
109 
110 int main()
111 {
112     Test();
113     return 0;
114 }
View Code

 

command

 1 // 444.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <vector>
 7 #include <functional>
 8 
 9 using namespace std;
10 
11 class Command {
12 public:
13     virtual void execute() = 0;
14 };
15 
16 class Hello :public Command {
17 public:
18     void execute() { cout << "Hello "; }
19 };
20 
21 class World :public Command {
22 public:
23     void execute() { cout << "world! "; }
24 };
25 
26 class IAm :public Command {
27 public:
28     void execute() { cout << "I'm the command pattern!"; }
29 };
30 
31 class Macro {
32     vector<Command*> commands;
33 public:
34     void add(Command* c) { commands.push_back(c); }
35     void run() {
36         vector<Command*>::iterator it = commands.begin();
37         while (it != commands.end())
38             (*it++)->execute();
39     }
40 };
41 //========================================================
42 class myCommand {
43 private:
44     std::function<void()> function_;
45 public:
46     void SetFunc(std::function<void()> function) {
47         function_ = function;
48     }
49     void calculate()const  {
50         return function_();
51     }
52 };
53 
54 class myMacro {
55     vector<myCommand> commands;
56 public:
57     void add(myCommand c) {
58         commands.push_back(c);
59     }
60     void run() {
61         for (const auto& it : commands) {
62             it.calculate();
63         }
64     }
65 };
66 
67 
68 
69 
70 
71 int main()
72 {
73     Macro macro;
74     macro.add(new Hello);
75     macro.add(new World);
76     macro.add(new IAm);
77     macro.run();
78     std::cout << std::endl;
79     std::cout << std::endl;
80     //============================
81     myMacro mm;
82     myCommand a;
83     a.SetFunc([] {std::cout << "Hello "; });
84     myCommand b;
85     b.SetFunc([] {std::cout << "world!"; });
86     myCommand c;
87     c.SetFunc([] {std::cout << " I'm the command pattern!"; });
88     mm.add(a);
89     mm.add(b);
90     mm.add(c);
91     mm.run();
92     std::cout << std::endl;
93     return 0;
94 }
command

 

proxy

 1 // 666.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #include <functional>
 7 
 8 
 9 using namespace std;
10 
11 class ProxyBase {
12 public:
13     virtual void f() = 0;
14     virtual void g() = 0;
15     virtual void h() = 0;
16     virtual ~ProxyBase() {}
17 };
18 
19 class Implementation :public ProxyBase {
20 public:
21     void f() { cout << __FUNCTION__ << endl; }
22     void g() { cout << __FUNCTION__ << endl; }
23     void h() { cout << __FUNCTION__ << endl; }
24 };
25 
26 class Proxy :public ProxyBase {
27     ProxyBase* implementation;
28 public:
29     Proxy() { implementation = new Implementation(); }
30     ~Proxy() { delete implementation; }
31     void f() { implementation->f(); }
32     void g() { implementation->g(); }
33     void h() { implementation->h(); }
34 };
35 //=====================================================
36 void f() { cout << "function " << __FUNCTION__ << endl; }
37 void g() { cout << "function " << __FUNCTION__ << endl; }
38 void h() { cout << "function " << __FUNCTION__ << endl; }
39 class myProxy {
40     std::function<void()> func_f;
41     std::function<void()> func_g;
42     std::function<void()> func_h;
43 public:
44     void set(std::function<void()> f, std::function<void()> g, std::function<void()> h)
45     {
46         func_f=(f); func_g=(g); func_h=(h);
47     }
48     void run() {
49         func_f();
50         func_g();
51         func_h();
52     }
53 
54 };
55 
56 int main()
57 {
58     Proxy p;
59     p.f();
60     p.g();
61     p.h();
62     std::cout << endl;
63     //====================================
64     myProxy pp;
65     pp.set(f,g,h);
66     pp.run();
67 
68     return 0;
69 }
proxy

 

posted on 2018-03-09 00:48  itdef  阅读(259)  评论(0编辑  收藏  举报

导航