관리 메뉴

프로그램 소스

[설계 패턴 C#] 18. 반복자 패턴(Iterator Pattern) 본문

설계 패턴(C#)

[설계 패턴 C#] 18. 반복자 패턴(Iterator Pattern)

Only 프로그램 소스 언제나휴일 2016.12.07 01:00

[설계 패턴 C#] 18. 반복자 패턴(Iterator Pattern)


Iterator.zip


"본문 내용"

[Escort GoF의 디자인 패턴 C#] 18. 반복자 패턴(Iterator Pattern)

[Escort GoF의 디자인 패턴 C#] 18. 반복자 패턴(Iterator Pattern) 설계

[Escort GoF의 디자인 패턴 C#] 18. 반복자 패턴(Iterator Pattern) 구현


     ▶ IContainer.cs

using System;

namespace Iterator

{

    interface IContainer

    {

        bool Add(Object elem);

        void Remove(Object elem);

        void Clear();

        int Count

        {

            get;

        }

    }

}


▶ IIterator.cs

using System;

namespace Iterator

{

    interface IIterator

    {

        void Begin();

        bool MoveNext();

        Object Current

        {

            get;

        }

    }

}


▶ 
Arr.cs

using System;

namespace Iterator

{

    class Arr:IContainer

    {

        Object[] buffer;

        readonly int max_element;       

 

        public Arr(int max_element)

        {

            this.max_element = max_element;

            buffer = new Object[max_element];

        }

        #region IContainer 멤버

        public bool  Add(object elem)

        {

            if(Count < max_element)

            {

                buffer[Count] = elem;

                Count++;

                return true;

            }

            return false;

        }

        public void  Remove(object elem)

        {

            int index = Find(elem);

            if (index == -1)

            {

                return;

            }

            Count--;

            for(int i =index; i<Count;i++)

            {

                buffer[i] = buffer[i+1];

            }           

        }

        public void Clear()

        {

            Count=0;

        }

        public int  Count

        {

            get;

            private set;

        }

        #endregion

 

        public class ArrIterator:IIterator

        {

            Object[] buffer;

            int bsize;

            int index;

            public ArrIterator(Object[] buffer, int bsize)

            {

                this.buffer = buffer;

                this.bsize = bsize;

            }

            #region IIterator 멤버

            void IIterator.Begin()

            {

                index = -1;

            }

            bool IIterator.MoveNext()

            {

                index++;

                if (index < bsize)

                {

                    return true;

                }

                return false;

            }

 

 

            object IIterator.Current

            {

                get { return buffer[index]; }

            }

            #endregion

        }

        public IIterator MakeIterator()

        {

            return new ArrIterator(buffer, Count);

        }

        int Find(Object elem)

        {

            int index=0;

            for( index=0 ; index < Count ; index++)

            {

                if(buffer[index] == elem)

                {

                    return index;

                }

            }

            return -1;

        }

    }

}


▶ Tracer.cs

using System;

 

namespace Iterator

{

    class Tracer

    {

        public static void DoIt()

        {

             Arr arr = new Arr(10);

            arr.Add(3);

            arr.Add(6);

            arr.Add(7);

 

            IIterator iter = arr.MakeIterator();

            iter.Begin();

            while (iter.MoveNext())

            {

                Console.WriteLine(iter.Current.ToString());           

            }

        }

    }

}


▶ Program.cs

using System;

 

namespace Iterator

{

    class Program

    {

        static void Main(string[] args)

        {

            Tracer.DoIt();

 

        }

    }

}


반복자 패턴 예제 실행화면





0 Comments
댓글쓰기 폼