zoukankan      html  css  js  c++  java
  • 设计模式-Iterator(行为模式) 将聚合的遍历封装到一个类中

    //以下代码来源: 设计模式精解-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;
    }
  • 相关阅读:
    Working with macro signatures
    Reset and Clear Recent Items and Frequent Places in Windows 10
    git分支演示
    The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
    Build website project by roslyn through devenv.com
    Configure environment variables for different tools in jenkins
    NUnit Console Command Line
    Code Coverage and Unit Test in SonarQube
    头脑王者 物理化学生物
    头脑王者 常识,饮食
  • 原文地址:https://www.cnblogs.com/fourmi/p/12090462.html
Copyright © 2011-2022 走看看