• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            設計模式之Adapter模式和Template Method模式

            Adpater(適配器)模式接受一種類型并提供其他類型的接口。它用在具相同思想的的,只是表達方式不同的類或函數中。
            例如:斐波那契數列發生類
            #ifndef FIBONACCIGENERATOR_H
            #define FIBONACCIGENERATOR_H

            class FibonacciGenerator {
              int n;
              int val[2];
            public:
              FibonacciGenerator() : n(0) { val[0] = val[1] = 0; }
              int operator()() {
                int result = n > 2 ? val[0] + val[1] : n > 0 ? 1 : 0;
                ++n;
                val[0] = val[1];
                val[1] = result;
                return result;
              }
              int count() { return n; }
            };
            #endif // FIBONACCIGENERATOR_H ///:~
            調用operator()來使用它。下面是使用Adpater模式的實現代碼
            include <iostream>
            #include <numeric>
            #include "FibonacciGenerator.h"
            #include "../C06/PrintSequence.h"
            using namespace std;

            class FibonacciAdapter { // Produce an iterator
              FibonacciGenerator f;
              int length;
            public:
              FibonacciAdapter(int size) : length(size) {}//通過斐波那契數列的長度來初始化類。它對輸入迭代器的約束條件比較嚴格
              class iterator;
              friend class iterator;
              class iterator : public std::iterator<
                std::input_iterator_tag, FibonacciAdapter, ptrdiff_t> {
                FibonacciAdapter& ap;
              public:
                typedef int value_type;
                iterator(FibonacciAdapter& a) : ap(a) {}
                bool operator==(const iterator&) const {
                  return ap.f.count() == ap.length;
                }
                bool operator!=(const iterator& x) const {
                  return !(*this == x);
                }
                int operator*() const { return ap.f(); }
                iterator& operator++() { return *this; }
                iterator operator++(int) { return *this; }
              };
              iterator begin() { return iterator(*this); }
              iterator end() { return iterator(*this); }
            };
            int main() {
              const int SZ = 20;
              FibonacciAdapter a1(SZ);
              cout << "accumulate: "
                << accumulate(a1.begin(), a1.end(), 0) << endl;
              FibonacciAdapter a2(SZ), a3(SZ);
              cout << "inner product: "
                << inner_product(a2.begin(), a2.end(), a3.begin(), 0)
                << endl;
              FibonacciAdapter a4(SZ);
              int r1[SZ] = {0};
              int* end = partial_sum(a4.begin(), a4.end(), r1);
              print(r1, end, "partial_sum", " ");
              FibonacciAdapter a5(SZ);
              int r2[SZ] = {0};
              end = adjacent_difference(a5.begin(), a5.end(), r2);
              print(r2, end, "adjacent_difference", " ");
            } ///:~

            Template Method(模板模式)通過調用基類的不同函數來驅動程序運行。 它的一個重要特征:定義在基類中(有時作為一個私有成員函數)并且不能改動。例如:
            #include <iostraem>
            using namespace std;

            class ApplicationFrameword{
            protected:
            virtral void custmoize1()= 0;
            virtral void custmoize2()=0;
            public:
            void templateMethod()
            {
            for(int i =0;i <5; ++i)
            {
            custmoize1();
            custmoize2();
            }
            }
            };

            //create a new "application"
            class MyApp:public ApplicationFramework{
            protected:
            void custmoize1()
            {
            cout << "Hello";
            }
            void custmoize2()
            {
            cout << "World!"<<endl;
            }

            int main()
            {
            MyApp app;
            app.templateMethod();
            }
            驅動應用程序運行的“引擎”就是模板方法模式。在GUI中,這個引擎就是主要的時間環,客戶端只要提供customize1()和custmoize2()的定義就可以運行程序。
            }
            }

            posted on 2009-04-12 14:02 Benjamin 閱讀(292) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久久精品国产sm调教网站| 色综合久久久久网| 色老头网站久久网| 色婷婷久久综合中文久久蜜桃av| 久久精品桃花综合| AV无码久久久久不卡蜜桃| 色综合久久久久网| 亚洲精品国精品久久99热一| 久久久久久狠狠丁香| 合区精品久久久中文字幕一区| 亚洲女久久久噜噜噜熟女| 色综合久久精品中文字幕首页| 久久亚洲视频| 欧美综合天天夜夜久久| 97久久国产综合精品女不卡 | 久久人人爽人人爽人人片av高请| 精品无码久久久久久午夜| 香蕉久久影院| 久久这里只有精品久久| 亚洲七七久久精品中文国产 | 欧美日韩成人精品久久久免费看| 一本一本久久A久久综合精品| 国产精品九九久久免费视频 | 色综合久久最新中文字幕| 日本加勒比久久精品| 久久福利青草精品资源站| 中文字幕久久波多野结衣av| 久久久99精品成人片中文字幕| 国产精品免费福利久久| 欧美黑人激情性久久| 亚洲国产精品高清久久久| 亚洲国产精品一区二区三区久久| 国产成人精品久久亚洲高清不卡| 国内精品久久国产大陆| 久久国产亚洲精品无码| 无码人妻久久久一区二区三区| 国产69精品久久久久观看软件| 人人狠狠综合88综合久久| 久久这里只有精品视频99| 久久久久久亚洲精品无码| 久久精品国产WWW456C0M|