码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式-Iterator(行为模式) 将聚合的遍历封装到一个类中

时间:2019-12-24 11:44:56      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:def   code   argv   ext   get   else   first   one   c++   

//以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码

//Aggregate.h

#pragma once

class Iterator;

typedef int Object;

class Interator;

class Aggregate
{
public:
    virtual ~Aggregate();
    virtual Iterator* CreateIterator() = 0;
    virtual Object GetItem(int idx) = 0;
    virtual int GetSize() = 0;
protected:
    Aggregate();
private:
};

class ConcreteAggregate :public Aggregate
{
public:
    enum 
    {
        SIZE=3
    };
    ConcreteAggregate();
    ~ConcreteAggregate();
    Iterator* CreateIterator();
    Object GetItem(int idx);
    int GetSize();
protected:
private:
    Object _objs[SIZE];
};

//Aggregate.cpp

#include"Aggregate.h"
#include"Iterator.h"
#include<iostream>

Aggregate::Aggregate(){}
Aggregate::~Aggregate(){}

ConcreteAggregate::ConcreteAggregate()
{
    for (int i = 0; i < SIZE; ++i)
    {
        _objs[i] = i;
    }
}

ConcreteAggregate::~ConcreteAggregate(){}

Iterator* ConcreteAggregate::CreateIterator()
{
    return new ConcreateIterator(this);
}

Object ConcreteAggregate::GetItem(int idx)
{
    if (idx < this->GetSize())return _objs[idx];
    else return -1;
}

int ConcreteAggregate::GetSize()
{
    return SIZE;
}

//Iterator.h

#pragma once

class Aggregate;

typedef int Object;

class Iterator
{
public:
    virtual ~Iterator();
    virtual void First() = 0;
    virtual void Next() = 0;
    virtual bool IsDone() = 0;
    virtual Object CurrentItem() = 0;
protected:
    Iterator();
private:
};

class ConcreateIterator :public Iterator
{
public:
    ConcreateIterator(Aggregate* ag, int idx = 0);
    ~ConcreateIterator();
    void First();
    void Next();
    bool IsDone();
    Object CurrentItem();
protected:
private:
    Aggregate* _ag;
    int _idx;
};

//Iterator.cpp

#include"Iterator.h"
#include"Aggregate.h"

Iterator::Iterator()
{

}
Iterator::~Iterator()
{

}
ConcreateIterator::ConcreateIterator(Aggregate* ag, int idx)
{
    this->_ag = ag;
    this->_idx = idx;
}
ConcreateIterator::~ConcreateIterator(){}
Object ConcreateIterator::CurrentItem()
{
    return _ag->GetItem(_idx);
}
void ConcreateIterator::Next()
{
    if (_idx < _ag->GetSize())++_idx;
}

bool ConcreateIterator::IsDone()
{
    return (_idx == _ag->GetSize());
}

void ConcreateIterator::First()
{
    _idx = 0;
}

//main.cpp

#include"Aggregate.h"
#include"Iterator.h"
#include<string>
#include<iostream>
int main(int args, char* argv)
{
    Aggregate* ag = new ConcreteAggregate();
    Iterator* it = new ConcreateIterator(ag);
    for (; !(it->IsDone()); ++it)
        std::cout << it->CurrentItem() << std::endl;
    getchar();
    return 0;
}

设计模式-Iterator(行为模式) 将聚合的遍历封装到一个类中

标签:def   code   argv   ext   get   else   first   one   c++   

原文地址:https://www.cnblogs.com/fourmi/p/12090462.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!