久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

19. C 實(shí)現(xiàn)Behavioral -Iterator模式

 WUCANADA 2012-01-20

19. C++實(shí)現(xiàn)Behavioral - Iterator模式

2010-02-05 00:16:13|  分類: Pattern |  標(biāo)簽: |字號(hào) 訂閱

aka. Cursor

 

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

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

 

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.

 

在軟件構(gòu)建過程中,, 集 合對象內(nèi)部結(jié)構(gòu)常常變化各異,。但對于這些集合對象,,我們希望在不暴露其內(nèi)部結(jié)構(gòu)的同時(shí),可以讓外部客戶代碼透明地訪問其中包含的元素,;同時(shí)這種“透明遍 歷”也為“同一種算法在多種結(jié)合對象上進(jìn)行操作”提供了可能,。使用面向?qū)ο蠹夹g(shù),將這種遍歷機(jī)制抽象為“迭代器對象”,,為“應(yīng)對變化中的集合對象”提供了 一種優(yōu)雅的遍歷方式,。

 

Iterator設(shè)計(jì)模式UML類圖:

19. C++實(shí)現(xiàn)Behavioral - Iterator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客 

要點(diǎn):

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

- 迭代多態(tài): 為遍歷不同的集合結(jié)構(gòu)提供一個(gè)統(tǒng)一的接口,,從而支持同樣的算法在不同的集合結(jié)構(gòu)上進(jìn)行操作。

- 迭代器通常不應(yīng)該修改所在集合的結(jié)構(gòu),,如刪除其中一個(gè)元素,,也就是迭代器通常是只讀的,但未必盡然,。

 

下面是具體的C++實(shí)現(xiàn)代碼:

// Iterator.h

#include <string>

#include <iostream>

#include <memory>

using namespace std;

 

// 這個(gè)類將作為集合中的元素

class Person

{

private:

         string name;

         string mobilephone;

public:

         // 由于ConcreteCollection類的構(gòu)造函數(shù)中的collection = new T[100];這一句,,因此,,Person

         // 必須提供缺省構(gòu)造函數(shù)

         Person()

         {

         }

 

         Person(const string name, const string mobilephone)

         {

                   this->name = name;

                   this->mobilephone = mobilephone;

         }

 

         ~Person()

         {

                   //由于Person類的析構(gòu)函數(shù)會(huì)被調(diào)用很多次,所以就把下面一行注釋了,,以便使得輸出更整潔一些

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

         }

 

public:

         // 顯示元素本身的內(nèi)容

         string to_string()

         {

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

                   return str;

         }

};

 

// 這是迭代器所必須實(shí)現(xiàn)的接口

template <typename T>

class IIterator

{

public:

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

         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;

         }

 

         // 增加一個(gè)元素

         void add_element(T obj)

         {

                   collection[index++] = obj;

         }

 

         // 獲取一個(gè)指定位置的元素

         T get_element_at(int i)

         {

                   return collection[i];

         }

 

         // 獲取集合中元素的個(gè)數(shù)

         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;    // 對應(yīng)構(gòu)造函數(shù)中的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);

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

                                       // 是為了進(jìn)行嚴(yán)格的類型檢查,。一般而言,,向下轉(zhuǎn)型時(shí)不安全的,因此要使

                                       // 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("玄機(jī)逸士", "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;

}

 

運(yùn)行結(jié)果:

玄機(jī)逸士   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...

 

與上述程序?qū)?yīng)的UML類圖:

19. C++實(shí)現(xiàn)Behavioral - Iterator模式 - 玄機(jī)逸士 - 玄機(jī)逸士博客 

Iterator模式實(shí)現(xiàn)需注意的要點(diǎn):

1. 兩個(gè)重要接口,,即IIterableIIterator,,它們分別對應(yīng)的具體類ConcreteCollectionConcreteIterator。本質(zhì)上而言ConcreteCollection中存放的是數(shù)據(jù),,ConcreteIterator中實(shí)現(xiàn)的是遍歷的方法,。

2. ConcreteCollection中的getIterator()方式的實(shí)現(xiàn)技巧。即

         return new ConcreteIterator(this);

    ConcreteCollection將自己(當(dāng)然也包括ConcreteCollection中包含的數(shù)據(jù))通過ConcreteIterator的構(gòu)造函數(shù),,傳遞給ConcreteIterator,,然后ConcreteIterator利用已實(shí)現(xiàn)的方法對其進(jìn)行遍歷。

3. 模板類的應(yīng)用,。上述實(shí)現(xiàn)由于使用了模板,,因此ConcreteCollection中的元素也可以是除Person類之外的其他類型。

 

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多