/* 小魚號的代碼日志
* 設(shè)計模式
* 迭代器模式
* 提供了統(tǒng)一的方法遍歷對象
* 缺點:
* 每個聚合對象都需要一個迭代器闯传,會生成多個迭代器
* 實例:大學(xué)院系信息展示
*/
#include<iostream>
#include<list>
#include<vector>
#include<array>
using namespace std;
class Department
{
public:
Department(string strName)
{
m_strName = strName;
}
string getName()
{
return m_strName;
}
void setName(string strName)
{
m_strName = strName;
}
private:
string m_strName;
};
class Iterator
{
public:
virtual bool hasNext() = 0;
virtual Department* next() = 0;
virtual void remove() = 0;
};
//計算機(jī)學(xué)院迭代器
class ComputerCollegeIterator :public Iterator
{
public:
ComputerCollegeIterator(vector<Department*> vDepartment)
{
m_pos = 0;
m_vDepartment = vDepartment;
}
public:
bool hasNext()
{
if(m_pos >= m_vDepartment.size())
{
return false;
}
return true;
}
Department* next()
{
return m_vDepartment[m_pos++];
}
void remove()
{
}
private:
int m_pos;
//計算機(jī)學(xué)院是按照vector存放系
vector<Department*> m_vDepartment;
};
//信息工程學(xué)院迭代器
class InfoCollegeIterator :public Iterator
{
public:
InfoCollegeIterator(array<Department*,5> ADepartment)
{
m_pos = 0;
m_ADepartment = ADepartment;
}
public:
bool hasNext()
{
if(m_pos >= m_ADepartment.size())
{
return false;
}
return true;
}
Department* next()
{
return m_ADepartment[m_pos++];
}
void remove()
{
}
private:
int m_pos;
//計算機(jī)學(xué)院是按照array存放系
array<Department*,5> m_ADepartment;
};
class College
{
public:
College(string name)
{
m_name = name;
}
string getName()
{
return m_name;
}
virtual void addDepartment(string name) = 0;
virtual Iterator* creatorIterator() = 0;
protected:
string m_name;
};
class ComputerCollege : public College
{
public:
ComputerCollege(string name) :College(name)
{
addDepartment("Soft Department");
addDepartment("Net Department");
addDepartment("BigData Department");
}
void addDepartment(string name)
{
Department* part = new Department(name);
m_vDepartment.push_back(part);
}
Iterator* creatorIterator()
{
return new ComputerCollegeIterator(m_vDepartment);
}
private:
vector<Department*> m_vDepartment;
};
class InfoCollege : public College
{
public:
InfoCollege(string name):College(name)
{
m_index = 0;
addDepartment("Info Department1");
addDepartment("Info Department2");
addDepartment("Info Department3");
addDepartment("Info Department4");
addDepartment("Info Department5");
}
void addDepartment(string name)
{
Department* part = new Department(name);
m_ADepartment[m_index++] = part;
}
Iterator* creatorIterator()
{
return new InfoCollegeIterator(m_ADepartment);
}
private:
array<Department*,5> m_ADepartment;
int m_index;
};
void printDepartment(Iterator* it)
{
while(it->hasNext())
{
Department* partment = it->next();
cout << partment->getName() << endl;
}
}
void testIterator()
{
vector<College*> collegeList;
College* college = new ComputerCollege("computer");
collegeList.push_back(college);
college = new InfoCollege("infomation");
collegeList.push_back(college);
auto it = collegeList.begin();
while(it != collegeList.end())
{
cout << "college name:" << (*it)->getName() << endl;
cout << "college department:" << endl;
printDepartment((*it)->creatorIterator());
cout << "=====================" << endl;
it++;
}
}
【C++設(shè)計模式】迭代器模式
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門被因,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卿拴,“玉大人滥玷,你說我怎么就攤上這事∥±猓” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵蛋欣,是天一觀的道長航徙。 經(jīng)常有香客問我,道長陷虎,這世上最難降的妖魔是什么到踏? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮尚猿,結(jié)果婚禮上窝稿,老公的妹妹穿的比我還像新娘。我一直安慰自己凿掂,他們只是感情好伴榔,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著庄萎,像睡著了一般踪少。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上糠涛,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼凌埂!你這毒婦竟也來了栽连?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布茂洒,位于F島的核電站孟岛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏督勺。R本人自食惡果不足惜渠羞,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望智哀。 院中可真熱鬧次询,春花似錦、人聲如沸瓷叫。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽摹菠。三九已至雌芽,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辨嗽,已是汗流浹背世落。 一陣腳步聲響...