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

            積木

            No sub title

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉載自:http://patmusing.blog.163.com/blog/static/13583496020101501613155/

            Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. – GoF

            提供一種方法,以順序訪問一個聚合對象中的元素,而又不暴露該聚合對象之內部表示。

            An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you want to accomplish. But you probably don’t want to bloat the List interface with operations for different traversals, even if you could anticipate the ones you will need. You might also need to have more than one traversal pending on the same list.

            The Iterator pattern lets you do all this. The key idea in this pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object. The Iterator class defines an interface for accessing the list’s elements. An iterator object is responsible for keeping track of the current element; that is, it knows which elements have been traversed already.

            在軟件構建過程中, 集合對象內部結構常常變化各異。但對于這些集合對象,我們希望在不暴露其內部結構的同時,可以讓外部客戶代碼透明地訪問其中包含的元素;同時這種“透明遍歷”也為“同一種算法在多種結合對象上進行操作”提供了可能。使用面向對象技術,將這種遍歷機制抽象為“迭代器對象”,為“應對變化中的集合對象”提供了一種優雅的遍歷方式。

            Iterator設計模式UML類圖:

            19. C++實現Behavioral - Iterator模式 - 玄機逸士 - 玄機逸士博客

            要點:

            - 迭代抽象: 訪問一個聚合對象的內容,而無需暴露其內部表示。

            - 迭代多態: 為遍歷不同的集合結構提供一個統一的接口,從而支持同樣的算法在不同的集合結構上進行操作。

            - 迭代器通常不應該修改所在集合的結構,如刪除其中一個元素,也就是迭代器通常是只讀的,但未必盡然。

            下面是具體的C++實現代碼:

            // Iterator.h

            #include <string>

            #include <iostream>

            #include <memory>

            using namespace std;

            // 這個類將作為集合中的元素

            class Person

            {

            private:

            string name;

            string mobilephone;

            public:

            // 由于ConcreteCollection類的構造函數中的collection = new T[100];這一句,因此,Person

            // 必須提供缺省構造函數

            Person()

            {

            }

            Person(const string name, const string mobilephone)

            {

            this->name = name;

            this->mobilephone = mobilephone;

            }

            ~Person()

            {

            //由于Person類的析構函數會被調用很多次,所以就把下面一行注釋了,以便使得輸出更整潔一些

            //cout << "in the destructor of Person..." << endl;

            }

            public:

            // 顯示元素本身的內容

            string to_string()

            {

            string str = name + "\t" + mobilephone;

            return str;

            }

            };

            // 這是迭代器所必須實現的接口

            template <typename T>

            class IIterator

            {

            public:

            virtual T next() = 0; // 取出下一個元素

            virtual bool has_next() = 0; // 遍歷完了嗎?如果遍歷完,則返回false;否則,返回true

            public:

            virtual ~IIterator()

            {

            cout << "in the destructor of IIterator..." << endl;

            }

            };

            // 定義IIterable接口,所有的欲使用迭代器的集合類,都必須繼承它

            template <typename T>

            class IIterable

            {

            public:

            virtual auto_ptr<IIterator<T> > get_iterator() = 0; // 獲取迭代器(智能指針)

            public:

            virtual ~IIterable()

            {

            cout << "in the destructor of IIterable..." << endl;

            }

            };

            // 具體集合類

            template <typename T>

            class ConcreteCollection : public IIterable<T>

            {

            private:

            int index;

            T *collection;

            public:

            ConcreteCollection()

            {

            collection = new T[100];

            index = 0;

            }

            // 增加一個元素

            void add_element(T obj)

            {

            collection[index++] = obj;

            }

            // 獲取一個指定位置的元素

            T get_element_at(int i)

            {

            return collection[i];

            }

            // 獲取集合中元素的個數

            int get_size()

            {

            return index;

            }

            // 獲取迭代器(智能指針)

            auto_ptr<IIterator<T> > get_iterator()

            {

            auto_ptr<IIterator<T> > temp(new ConcreteIterator<Person>(this));

            return temp;

            }

            public:

            ~ConcreteCollection()

            {

            delete [] collection; // 對應構造函數中的collection = new T[100];

            cout << "in the destructor of ConcreteCollection..." << endl;

            }

            };

            // 具體迭代器類

            template <typename T>

            class ConcreteIterator : public IIterator<T>

            {

            private:

            int index;

            ConcreteCollection<T> *collection;

            public:

            ConcreteIterator(IIterable<T> *iterable)

            {

            index = 0;

            collection = dynamic_cast<ConcreteCollection<T> *>(iterable);

            // 向下轉型,即將基類的指針轉換為派生類的指針,此處使用dynamic_cast

            // 是為了進行嚴格的類型檢查。一般而言,向下轉型時不安全的,因此要使

            // dynamic_cast

            }

            bool has_next()

            {

            if(0 == collection->get_size())

            {

            return false;

            }

            if(index < collection->get_size())

            {

            return true;

            }

            else

            {

            return false;

            }

            }

            T next()

            {

            return collection->get_element_at(index++);

            }

            public:

            ~ConcreteIterator()

            {

            cout << "in the destructor of ConcreteIterator..." << endl;

            }

            };

            // Iterator.cpp

            #include "Iterator.h"

            int main(int argc, char **argv)

            {

            ConcreteCollection<Person> *collection = new ConcreteCollection<Person>();

            collection->add_element(Person("玄機逸士", "13800000001"));

            collection->add_element(Person("上官天野", "13800000002"));

            collection->add_element(Person("黃藥師 ", "13800000003"));

            collection->add_element(Person("歐陽鋒 ", "13800000004"));

            collection->add_element(Person("段王爺 ", "13800000005"));

            collection->add_element(Person("洪七公 ", "13800000006"));

            auto_ptr<IIterator<Person> > iter(collection->get_iterator());

            while(iter->has_next())

            {

            cout << (iter->next()).to_string() << endl;

            }

            delete collection;

            return 0;

            }

            運行結果:

            玄機逸士 13800000001

            上官天野 13800000002

            黃藥師 13800000003

            歐陽鋒 13800000004

            段王爺 13800000005

            洪七公 13800000006

            in the destructor of ConcreteCollection...

            in the destructor of IIterable...

            in the destructor of ConcreteIterator...

            in the destructor of IIterator...

            與上述程序對應的UML類圖:

            19. C++實現Behavioral - Iterator模式 - 玄機逸士 - 玄機逸士博客

            Iterator模式實現需注意的要點:

            1. 兩個重要接口,即IIterableIIterator,它們分別對應的具體類ConcreteCollectionConcreteIterator。本質上而言ConcreteCollection中存放的是數據,ConcreteIterator中實現的是遍歷的方法。

            2. ConcreteCollection中的getIterator()方式的實現技巧。即

            return new ConcreteIterator(this);

            ConcreteCollection將自己(當然也包括ConcreteCollection中包含的數據)通過ConcreteIterator的構造函數,傳遞給ConcreteIterator,然后ConcreteIterator利用已實現的方法對其進行遍歷。

            3. 模板類的應用。上述實現由于使用了模板,因此ConcreteCollection中的元素也可以是除Person類之外的其他類型。

            posted on 2013-03-08 12:57 Jacc.Kim 閱讀(224) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            国产精品一久久香蕉国产线看| 一本大道久久a久久精品综合| 麻豆国内精品久久久久久| 亚洲嫩草影院久久精品| 久久成人永久免费播放| 久久无码中文字幕东京热| 国产精品美女久久久久网| 久久激情亚洲精品无码?V| 亚洲精品美女久久777777| 青青草国产精品久久| 欧美亚洲国产精品久久高清| 99精品国产在热久久无毒不卡| 无码人妻少妇久久中文字幕| 东京热TOKYO综合久久精品| 婷婷国产天堂久久综合五月| 国产精品天天影视久久综合网| 波多野结衣久久一区二区| 国产成人AV综合久久| 色综合久久久久无码专区| 精品久久久久久无码人妻蜜桃| 久久久久亚洲av无码专区导航| 色综合久久中文字幕综合网| 久久国产成人精品麻豆| 无码日韩人妻精品久久蜜桃| 亚洲欧美日韩久久精品| 久久这里有精品视频| www亚洲欲色成人久久精品| 欧美熟妇另类久久久久久不卡| 香蕉久久夜色精品国产尤物| 久久人人爽人人爽AV片| 国产一区二区三精品久久久无广告| 国产精品99久久99久久久| 久久久久成人精品无码中文字幕| 久久中文字幕人妻丝袜| 2021国内精品久久久久久影院| 亚洲精品国精品久久99热| 久久毛片免费看一区二区三区| 国产成人久久精品麻豆一区| 99久久无码一区人妻| 久久久久亚洲AV无码专区桃色| 久久精品国产99久久香蕉|