打印

设计模式:用C++实现一个迭代器模式,将它应用于分发服务器的重构。

[复制链接]
732|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
keer_zu|  楼主 | 2015-12-1 16:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
头文件:

#ifndef __ITERATOR_H__
#define __ITERATOR_H__

const long DEFAULT_LIST_CAPACITY = 200;

template <class Item>
class List {
public:
        List(long size = DEFAULT_LIST_CAPACITY);
        List(List&);
        ~List();
        List& operator=(const List&);
/*
*/
        long Count() const;
        Item& Get(long index) const;
        Item& First() const;
        Item& Last() const;
        bool Includes(const Item&) const;
/*
*/
        void Append(const Item&);
        void Prepend(const Item&);
/*
*/
        void Remove(const Item&);
        void RemoveLast();
        void RemoveFirst();
        void RemoveAll();
/*
*/
        Item& Top() const;
        void Push(const Item&);
        Item& Pop();
};


template <class Item>
class Iterator
{
public:
        virtual void First() = 0;
        virtual void Next() = 0;
        virtual bool IsDone() const = 0;
        virtual Item CurrentItem() const = 0;
protected:
Iterator();
};


template <class Item>
class ListIterator : public Iterator<Item> {
public:
        ListIterator(const List<Item>* aList);
/*
*/
        virtual void First();
        virtual void Next();
        virtual bool IsDone() const;
        virtual Item CurrentItem() const;
private:
        const List<Item>* _list;
        long _courrent;
};


template <typename Item>
class ReverseListIterator : public Iterator<Item> {
public:
        ReverseListIterator(const List<Item>* aList);
        virtual void First();
        virtual void Next();
        virtual bool IsDone() const;
        virtual Item CurrentItem() const;
private:
        const List<Item>* _list;
        long _courrent;
};


#endif

相关帖子

沙发
keer_zu|  楼主 | 2015-12-1 16:17 | 只看该作者
这个后续需要继续完善,以具体的list代替List.

使用特权

评论回复
板凳
keer_zu|  楼主 | 2015-12-1 16:18 | 只看该作者
具体方法定义:
#include "iterator.h"

template <class Item>
ListIterator<Item>::ListIterator(const List<Item>* aList)
        : _courrent(0)
        , _list(aList)
{

}

template <class Item>
void ListIterator<Item>::First()
{
        _courrent = 0;
}

template <class Item>
void ListIterator<Item>::Next()
{
        _courrent ++;
}

template <class Item>
bool ListIterator<Item>::IsDone() const
{
        return _courrent >= _list->Count();
}

template <class Item>
Item ListIterator<Item>::CurrentItem() const
{
        return _list->Get(_courrent);
}


template <class Item>
ReverseListIterator<Item>::ReverseListIterator(const List<Item>* aList)
        : _courrent(0)
        , _list(aList)
{

}

template <class Item>
void ReverseListIterator<Item>::First()
{
        _courrent = 0;
}

template <class Item>
void ReverseListIterator<Item>::Next()
{
        _courrent ++;
}

template <class Item>
bool ReverseListIterator<Item>::IsDone() const
{
        return _courrent >= _list->Count();
}

template <class Item>
Item ReverseListIterator<Item>::CurrentItem() const
{
        return _list->Get(_courrent);
}

int main()
{

        return 0;
}

使用特权

评论回复
地板
keer_zu|  楼主 | 2015-12-1 16:19 | 只看该作者
List的成员函数(方法)具体的list中实现。所贴代码都可以在linux环境中利用:  g++ -g -o oi iterator.cpp 编译

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1314

主题

12271

帖子

53

粉丝