设计模式例程
This commit is contained in:
152
inc/iterator.hpp
Normal file
152
inc/iterator.hpp
Normal file
@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class NumeralSystem;
|
||||
|
||||
class Customer
|
||||
{
|
||||
public:
|
||||
Customer(string name, string clinics = "None") :m_name(name), m_clinics(clinics)
|
||||
{
|
||||
m_num = 0;
|
||||
}
|
||||
string getName();
|
||||
void Regist(NumeralSystem* system);
|
||||
void setNum(int num);
|
||||
int getNum();
|
||||
void setClinic(string clinic);
|
||||
string getClinic();
|
||||
|
||||
private:
|
||||
string m_name;
|
||||
int m_num;
|
||||
string m_clinics;
|
||||
};
|
||||
|
||||
class NumeralIterator
|
||||
{
|
||||
public:
|
||||
NumeralIterator(vector<Customer*> data, int curIdx = -1) :m_data(data), m_curIdx(curIdx) {};
|
||||
bool next();
|
||||
Customer* current();
|
||||
|
||||
private:
|
||||
vector<Customer*> m_data;
|
||||
int m_curIdx;
|
||||
};
|
||||
|
||||
class NumeralSystem
|
||||
{
|
||||
public:
|
||||
static vector<string> m_clinics;
|
||||
NumeralSystem(string name) : m_name(name)
|
||||
{
|
||||
m_curNum = 0;
|
||||
}
|
||||
void pushCustomer(Customer* pCustomer);
|
||||
NumeralIterator getIterator();
|
||||
|
||||
private:
|
||||
vector<Customer*> m_pCustomers;
|
||||
int m_curNum;
|
||||
string m_name;
|
||||
};
|
||||
|
||||
// ======================== <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>磩 ====================
|
||||
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
Iterator() {};
|
||||
virtual ~Iterator() {};
|
||||
virtual string toBegin() = 0;
|
||||
virtual string toNext() = 0;
|
||||
virtual string getCurrent() = 0;
|
||||
virtual bool isEnd() = 0;
|
||||
};
|
||||
|
||||
class Aggregate
|
||||
{
|
||||
public:
|
||||
virtual int Count() = 0;
|
||||
virtual void Push(const string& strValue) = 0;
|
||||
virtual string Pop(const int nIndex) = 0;
|
||||
virtual Iterator* CreateIterator() = 0;
|
||||
};
|
||||
|
||||
class ConcreteIterator : public Iterator
|
||||
{
|
||||
public:
|
||||
ConcreteIterator(Aggregate* pAggregate) :m_nCurrent(0), Iterator()
|
||||
{
|
||||
m_Aggregate = pAggregate;
|
||||
}
|
||||
string toBegin()
|
||||
{
|
||||
return m_Aggregate->Pop(0);
|
||||
}
|
||||
string toNext()
|
||||
{
|
||||
string strRet;
|
||||
m_nCurrent++;
|
||||
if (m_nCurrent < m_Aggregate->Count())
|
||||
{
|
||||
strRet = m_Aggregate->Pop(m_nCurrent);
|
||||
}
|
||||
return strRet;
|
||||
}
|
||||
string getCurrent()
|
||||
{
|
||||
return m_Aggregate->Pop(m_nCurrent);
|
||||
}
|
||||
bool isEnd()
|
||||
{
|
||||
return ((m_nCurrent >= m_Aggregate->Count()) ? true : false);
|
||||
}
|
||||
private:
|
||||
Aggregate* m_Aggregate;
|
||||
int m_nCurrent;
|
||||
};
|
||||
|
||||
class ConcreteAggregate : public Aggregate
|
||||
{
|
||||
public:
|
||||
ConcreteAggregate() :m_pIterator(NULL)
|
||||
{
|
||||
m_vecItems.clear();
|
||||
}
|
||||
~ConcreteAggregate()
|
||||
{
|
||||
if (NULL != m_pIterator)
|
||||
{
|
||||
delete m_pIterator;
|
||||
m_pIterator = NULL;
|
||||
}
|
||||
}
|
||||
Iterator* CreateIterator()
|
||||
{
|
||||
if (NULL == m_pIterator)
|
||||
{
|
||||
m_pIterator = new ConcreteIterator(this);
|
||||
}
|
||||
return m_pIterator;
|
||||
}
|
||||
void Push(const string& strValue)
|
||||
{
|
||||
m_vecItems.push_back(strValue);
|
||||
}
|
||||
string Pop(const int nIndex)
|
||||
{
|
||||
string strRet;
|
||||
if (nIndex < m_vecItems.size())
|
||||
{
|
||||
strRet = m_vecItems[nIndex];
|
||||
}
|
||||
return strRet;
|
||||
}
|
||||
private:
|
||||
vector<string> m_vecItems;
|
||||
Iterator* m_pIterator;
|
||||
|
||||
|
||||
};
|
Reference in New Issue
Block a user