青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

積木

No sub title

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

常用鏈接

留言簿(1)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

轉(zhuǎn)載自: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

提供一種方法,以順序訪問一個聚合對象中的元素,而又不暴露該聚合對象之內(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)的同時,可以讓外部客戶代碼透明地訪問其中包含的元素;同時這種“透明遍歷”也為“同一種算法在多種結(jié)合對象上進行操作”提供了可能。使用面向?qū)ο蠹夹g(shù),將這種遍歷機制抽象為“迭代器對象”,為“應(yīng)對變化中的集合對象”提供了一種優(yōu)雅的遍歷方式。

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

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

要點:

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

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

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

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

// Iterator.h

#include <string>

#include <iostream>

#include <memory>

using namespace std;

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

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ù)會被調(diào)用很多次,所以就把下面一行注釋了,以便使得輸出更整潔一些

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

}

public:

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

string to_string()

{

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

return str;

}

};

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

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];

}

// 獲取集合中元素的個數(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

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

// 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;

}

運行結(jié)果:

玄機逸士 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++實現(xiàn)Behavioral - Iterator模式 - 玄機逸士 - 玄機逸士博客

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

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

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

return new ConcreteIterator(this);

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

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

posted on 2013-03-08 12:57 Jacc.Kim 閱讀(226) 評論(0)  編輯 收藏 引用 所屬分類: 設(shè)計模式
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久久www成人免费精品| 国产精品拍天天在线| 亚洲美女淫视频| 亚洲人成久久| 亚洲精品国久久99热| 亚洲精品男同| 一区二区高清在线| 午夜精品久久| 久久一二三四| 欧美日韩国产一区| 欧美日韩一区二区在线播放| 国产精品久久久免费| 99国内精品久久| 亚洲免费在线观看视频| 久久精品国语| 欧美精品一区二区三| 国产精品色午夜在线观看| 国产综合自拍| 在线亚洲免费| 老色鬼精品视频在线观看播放| 亚洲国产精品99久久久久久久久| 亚洲精品国产精品国自产观看浪潮| 亚洲欧美日本国产专区一区| 久久久久久穴| 国产精品日韩电影| 亚洲日本成人| 久久九九全国免费精品观看| 91久久国产综合久久91精品网站| 亚洲欧美日韩一区在线| 欧美激情国产日韩| 国语自产在线不卡| 午夜国产不卡在线观看视频| 免费看亚洲片| 亚洲一区免费网站| 欧美激情第10页| 激情av一区| 欧美有码视频| 中日韩男男gay无套| 乱人伦精品视频在线观看| 国产精品日韩| 亚洲在线一区二区| 亚洲精品久久久久| 欧美bbbxxxxx| 亚洲国产一区二区视频| 久久影视精品| 久久国产日本精品| 国产精品国产三级国产aⅴ浪潮| 亚洲精品一区二区三区福利| 欧美aⅴ99久久黑人专区| 欧美中在线观看| 国产专区欧美精品| 久久先锋资源| 久久久久久久网| 狠狠88综合久久久久综合网| 欧美一区二区三区免费视频| 一本大道久久精品懂色aⅴ| 欧美精品在线观看播放| 亚洲日本中文| 亚洲高清不卡av| 欧美**字幕| 亚洲精品综合精品自拍| 欧美激情精品久久久| 久久在线播放| 最新成人av在线| 亚洲国产精品女人久久久| 欧美成人乱码一区二区三区| 亚洲精品九九| 日韩视频一区二区在线观看 | 欧美华人在线视频| 久久久亚洲高清| 亚洲国产成人精品视频| 欧美大片91| 欧美久久久久久蜜桃| 亚洲一二三四区| 黑人巨大精品欧美一区二区小视频| 亚洲一区二区三区免费视频| 一区二区免费看| 国产欧美一级| 欧美激情二区三区| 欧美日韩一区免费| 久久精品99| 女人香蕉久久**毛片精品| 一本不卡影院| 欧美在线视频二区| 亚洲激情在线播放| 中文国产成人精品久久一| 国产亚洲一区精品| 亚洲激情av在线| 国产欧美一区二区三区在线老狼 | 一区二区三区不卡视频在线观看| 亚洲裸体视频| 国产日韩欧美在线看| 免费亚洲一区二区| 欧美午夜不卡视频| 免费在线欧美视频| 欧美日韩日韩| 久久久在线视频| 欧美日韩国产色站一区二区三区| 久久国产精品亚洲va麻豆| 欧美va天堂在线| 欧美主播一区二区三区| 欧美成人性生活| 久久国产精品久久久久久久久久| 免费一级欧美在线大片| 西西人体一区二区| 欧美精品一区二区三区久久久竹菊| 欧美一级久久久久久久大片| 欧美精品成人91久久久久久久| 久久狠狠亚洲综合| 欧美四级剧情无删版影片| 蜜桃久久av一区| 国产精品自拍小视频| 亚洲精品少妇网址| 亚洲高清久久网| 欧美一级淫片播放口| 亚洲桃花岛网站| 欧美a级在线| 欧美成人精品福利| 韩日成人在线| 欧美一区不卡| 性欧美精品高清| 欧美日韩午夜精品| 亚洲国产精品一区二区三区| 伊人夜夜躁av伊人久久| 欧美一二三区精品| 欧美在线视频a| 国产精品日本精品| 亚洲一区二区三区777| 亚洲天堂av在线免费观看| 欧美精品电影| 国内精品美女在线观看| 老牛影视一区二区三区| 国产欧美在线视频| 亚洲欧美日产图| 午夜精品短视频| 国产精品尤物福利片在线观看| 99re亚洲国产精品| 亚洲特色特黄| 国产精品久久久久一区| 亚洲天堂免费观看| 亚洲欧美另类国产| 国产精品一区二区欧美| 欧美亚洲免费| 噜噜噜在线观看免费视频日韩| 好看的av在线不卡观看| 久久久噜噜噜久久狠狠50岁| 免费黄网站欧美| 亚洲精品美女久久久久| 欧美日韩国产bt| 亚洲一区久久久| 久久精品久久综合| 亚洲高清在线观看| 欧美日韩国产亚洲一区| 亚洲一区二区三区中文字幕在线| 久久成人18免费网站| 在线精品视频免费观看| 欧美电影免费观看高清完整版| 亚洲精品偷拍| 久久久av网站| 亚洲免费观看视频| 国产精品亚洲成人| 久久久噜噜噜久噜久久| 亚洲激情偷拍| 欧美在线视频一区二区三区| 亚洲高清视频在线观看| 国产精品国产三级国产普通话99 | 蜜桃久久精品一区二区| 亚洲乱码视频| 国产欧美 在线欧美| 久久久久一区| 一区二区三区日韩精品| 开心色5月久久精品| 99这里只有久久精品视频| 国产精品一区二区黑丝| 欧美激情一区二区三区高清视频| 亚洲宅男天堂在线观看无病毒| 欧美不卡视频一区发布| 午夜欧美精品久久久久久久| 亚洲激情在线激情| 国产日韩欧美中文| 欧美精品导航| 久热成人在线视频| 亚洲欧美在线视频观看| 亚洲黄色一区二区三区| 久久久之久亚州精品露出| 夜夜爽www精品| 亚洲国产精品t66y| 国产欧美日韩精品在线| 久久手机免费观看| 亚洲欧美日韩一区二区三区在线| 国外成人在线视频| 国产精品日韩在线播放| 欧美日韩亚洲一区二区| 久久久亚洲人| 亚洲欧美日韩另类| 亚洲桃花岛网站| 日韩午夜av在线| 亚洲精品乱码久久久久久蜜桃91 | 欧美成人免费视频| 久久aⅴ国产欧美74aaa|