• <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

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            設(shè)計(jì)模式之Adapter模式和Template Method模式

            Adpater(適配器)模式接受一種類型并提供其他類型的接口。它用在具相同思想的的,只是表達(dá)方式不同的類或函數(shù)中。
            例如:斐波那契數(shù)列發(fā)生類
            #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 ///:~
            調(diào)用operator()來使用它。下面是使用Adpater模式的實(shí)現(xiàn)代碼
            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) {}//通過斐波那契數(shù)列的長度來初始化類。它對輸入迭代器的約束條件比較嚴(yán)格
              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(模板模式)通過調(diào)用基類的不同函數(shù)來驅(qū)動程序運(yùn)行。 它的一個重要特征:定義在基類中(有時(shí)作為一個私有成員函數(shù))并且不能改動。例如:
            #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();
            }
            驅(qū)動應(yīng)用程序運(yùn)行的“引擎”就是模板方法模式。在GUI中,這個引擎就是主要的時(shí)間環(huán),客戶端只要提供customize1()和custmoize2()的定義就可以運(yùn)行程序。
            }
            }

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

            久久精品无码专区免费东京热 | 久久精品国产清自在天天线 | 久久久亚洲欧洲日产国码是AV| 性高朝久久久久久久久久| 亚洲AⅤ优女AV综合久久久| 久久久SS麻豆欧美国产日韩| 久久精品国产精品青草| 久久久精品久久久久影院| 91久久精品91久久性色| 亚洲午夜无码久久久久小说| 国产亚洲精品美女久久久| 久久影视国产亚洲| 日本道色综合久久影院| 久久久黄色大片| 久久久久亚洲爆乳少妇无| 国内精品久久久久久久97牛牛| 久久久免费观成人影院| 国产精品久久久久久影院| 久久久久久综合网天天| 久久精品国产亚洲精品| 51久久夜色精品国产| 久久久久免费看成人影片| 综合久久一区二区三区 | 精品午夜久久福利大片| 久久亚洲精品无码VA大香大香| 91精品婷婷国产综合久久| 久久精品国产亚洲AV无码麻豆| 少妇无套内谢久久久久| 日产久久强奸免费的看| 精品久久人人妻人人做精品| 香蕉久久夜色精品国产小说| 777米奇久久最新地址| 久久久亚洲欧洲日产国码aⅴ| 影音先锋女人AV鲁色资源网久久 | 伊人久久大香线蕉av不变影院| 伊人久久大香线蕉精品不卡| 色综合久久88色综合天天| 国产成年无码久久久久毛片| 亚洲精品无码久久久影院相关影片| 久久精品日日躁夜夜躁欧美| 久久精品青青草原伊人|