<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>세계의끝과 플래시 원더랜드 &#187; 반복문</title>
	<atom:link href="http://ufx.kr/blog/tag/%eb%b0%98%eb%b3%b5%eb%ac%b8/feed" rel="self" type="application/rss+xml" />
	<link>http://ufx.kr/blog</link>
	<description>Flash + ActionScript &#38; Design</description>
	<lastBuildDate>Fri, 27 Jan 2012 21:51:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Iterator Pattern 이터레이터 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/188</link>
		<comments>http://ufx.kr/blog/188#comments</comments>
		<pubDate>Tue, 24 Nov 2009 12:21:27 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[Iterator]]></category>
		<category><![CDATA[Iterator Pattern]]></category>
		<category><![CDATA[디자인패턴]]></category>
		<category><![CDATA[반복]]></category>
		<category><![CDATA[반복문]]></category>
		<category><![CDATA[반복자]]></category>
		<category><![CDATA[오라일리]]></category>
		<category><![CDATA[이터레이터]]></category>
		<category><![CDATA[이터레이터 패턴]]></category>
		<category><![CDATA[캡슐화]]></category>
		<category><![CDATA[한빛미디어]]></category>
		<category><![CDATA[헤드퍼스트]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=188</guid>
		<description><![CDATA[Head First Design Pattern 의 제 9 장 내용인 이터레이터 패턴(Iterator Pattern)과 컴포지트 패턴(Composite Pattern) 입니다. 9장의 내용은 이터레이터 패턴과 컴포지트 패턴을 식당 통합 메뉴를 구현하는 연속되는 내용으로 설명하고 있는데요, 이 한 개 챕터의 페이지가 다른 부분보다 훨씬 많은 70페이지인데다가 설명해야 하는 부분도 많고 코드도 복잡하기 때문에 이터레이터와 컴포지트를 2개의 포스트로 나누어서 작성하도록 하겠습니다. 이터레이터 [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_336" class="wp-caption alignleft" style="width: 245px"><img src="http://ufx.kr/blog/wp-content/uploads/2009/05/iterate_graphic.jpg" alt="이런 것은 optical(시각적인) iterator pattern 이라 할 수 있겠죠." title="iterate_graphic" width="235" height="240" class="size-full wp-image-336" /><p class="wp-caption-text">이런 것은 optical(시각적인) iterator pattern 이라 할 수 있겠죠.</p></div>Head First Design Pattern 의 제 9 장 내용인 이터레이터 패턴(Iterator Pattern)과 컴포지트 패턴(Composite Pattern) 입니다. </p>
<p>9장의 내용은 이터레이터 패턴과 컴포지트 패턴을 식당 통합 메뉴를 구현하는 연속되는 내용으로 설명하고 있는데요, 이 한 개 챕터의 페이지가 다른 부분보다 훨씬 많은 70페이지인데다가 설명해야 하는 부분도 많고 코드도 복잡하기 때문에 이터레이터와 컴포지트를 2개의 포스트로 나누어서 작성하도록 하겠습니다. </p>
<p>이터레이터 패턴의 정의는 다음과 같습니다.<br />
<span style="color: #808080;"><br />
&#8220;이터레이터 패턴은 컬렉션 구현 방법을 노출시키지 않으면서도 그 집합체 안에 들어있는 모든 항목에 접근할 수 있게 해 주는 방법을 제공합니다.&#8221;<br />
</span><br />
즉, 반복을 패턴화 한다는 이야긴데요. 어떤식으로 구현하는지, 그리고 단순히 반복문을 사용하는 것과는 어떤 점이 다른지 알아보도록 하겠습니다.</p>
<p><span id="more-188"></span><br />
객체마을 식당과 팬케이크 하우스가 합병하게 되어 메뉴를 통합해야 하는 상황입니다. 문제는 메뉴를 구현하는 방식을 놓고 양쪽의 오너들은 자신의 코드 수정을 꺼려하고 있는 것이죠. 먼저 메뉴를 어떤 식으로 구현하기로 했는지 보도록 할까요?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MenuItem
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">name</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">description</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> vegetarian<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> price<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MenuItem<span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $description<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $vegetarian<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>, $price<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">name</span> = $name;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">description</span> = $description;
			<span style="color: #0033ff; font-weight: bold;">this</span>.vegetarian = $vegetarian;
			<span style="color: #0033ff; font-weight: bold;">this</span>.price = $price;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">name</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">description</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getPrice<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> price;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> isVegetarian<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> vegetarian;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>특별히 어려운 부분은 없는것 같습니다. 메뉴 이름, 메뉴 설명, 채식주의자용 메뉴여부, 가격의 네 가지 변수를 가져오는 getter 메서드 들이 있고, 생성자에서 객체를 만들면서 해당 변수에 대입합니다.</p>
<p>양쪽의 식당은 이 메뉴구현에 맞추어 각자의 메뉴를 입력하면 됩니다만 기존에 사용하던 메뉴 구조가 서로 다릅니다. 먼저 짚고 넘어갈 부분은 책 본문에서는 양쪽의 형식이 다르다는 것을 표현하기 위해 자바의 ArrayList 와 Array 를 사용했지만, 액션스크립트에 ArrayList 는 없기 때문에 이번에 FlashPlayer 10에서 지원하게 된 Vector 로 바꿔서 코드를 번역하였습니다. 또한 원래 DinerHouse 의 메뉴가 배열원소의 갯수를 제한한 Array 였기  때문에<sup>[<a href="http://ufx.kr/blog/188#footnote_0_188" id="identifier_0_188" class="footnote-link footnote-identifier-link" title="액션스크립트랑은 약간 다릅니다.">01</a>]</sup> DinerHouse 의 Array 를 원소 갯수를 제한할 수 있는 Vector 로 바꾸고, PancakeHouse 의 ArrayList 를 Array 로 구현하였습니다. </p>
<p>그럼 PancakeHouse 의 메뉴를 먼저 보겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> PancakeHouseMenu implements IMenu
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> menuItems<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PancakeHouseMenu <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			menuItems = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;K&amp;B 팬케이크 세트&quot;</span>, <span style="color: #990000;">&quot;스크램블드 에그와 토스트가 곁들여진 팬케이크&quot;</span>, <span style="color: #0033ff; font-weight: bold;">true</span>, <span style="color: #000000; font-weight:bold;">2990</span> <span style="color: #000000;">&#41;</span>
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;레귤러 팬케이크 세트&quot;</span>, <span style="color: #990000;">&quot;달걀 후라이와 소시지가 곁들여진 팬케이크&quot;</span>, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">2990</span> <span style="color: #000000;">&#41;</span>
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;블루베리 팬케이크&quot;</span>, <span style="color: #990000;">&quot;신선한 블루베리와 블루베리 시럽으로 만든 팬케이크&quot;</span>, <span style="color: #0033ff; font-weight: bold;">true</span>, <span style="color: #000000; font-weight:bold;">3490</span> <span style="color: #000000;">&#41;</span>
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;와플&quot;</span>, <span style="color: #990000;">&quot;와플, 취향에 따라 블루베리나 딸기를 얹을 수 있습니다.&quot;</span>, <span style="color: #0033ff; font-weight: bold;">true</span>, <span style="color: #000000; font-weight:bold;">3590</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addItem<span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $description<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $vegetarian<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>, $price<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> menuItem<span style="color: #000000; font-weight: bold;">:</span>MenuItem = <span style="color: #0033ff; font-weight: bold;">new</span> MenuItem<span style="color: #000000;">&#40;</span> $name, $description, $vegetarian, $price <span style="color: #000000;">&#41;</span>
			menuItems.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>menuItem<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/*
		 * 이터레이터 패턴 구현으로 사용하지 않게된 메서드
		public function getMenuItems():MenuItem
		{
			return menuItems;
		}
		*/</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> createIterator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IIterator
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> PancakeHouseIterator<span style="color: #000000;">&#40;</span> menuItems <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//기타 메뉴관련 메서드</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>위에서 언급한대로 Array 를 이용하여 메뉴를 구현하고 있습니다.</p>
<p>그럼 DinerHouse 의 메뉴구현은 어떤가요?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> DinerMenu implements IMenu
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static const MAX_ITEM<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">6</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> numberOfItems<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> menuItems<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>MenuItem<span style="color: #000000; font-weight: bold;">&gt;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> DinerMenu <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			menuItems = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>MenuItem<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span> MAX_ITEM, <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000;">&#41;</span>
&nbsp;
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;채식주의자용 BLT&quot;</span>, <span style="color: #990000;">&quot;통밀 위에 (식물성)베이컨, 상추, 토마토를 얹은 메뉴&quot;</span>, <span style="color: #0033ff; font-weight: bold;">true</span>, <span style="color: #000000; font-weight:bold;">2990</span> <span style="color: #000000;">&#41;</span>
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;BLT&quot;</span>, <span style="color: #990000;">&quot;통밀 위에 베이컨, 상추, 토마토를 얹은 메뉴&quot;</span>, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">2990</span> <span style="color: #000000;">&#41;</span>
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;오늘의 스프&quot;</span>, <span style="color: #990000;">&quot;감자 샐러드를 곁들인 오늘의 스프&quot;</span>, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">3290</span> <span style="color: #000000;">&#41;</span>
			addItem<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;핫도그&quot;</span>, <span style="color: #990000;">&quot;사워크라우트, 갖은 양념, 양파, 치즈가 곁들여진 핫도그&quot;</span>, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #000000; font-weight:bold;">3050</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addItem<span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $description<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $vegetarian<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>, $price<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> menuItem<span style="color: #000000; font-weight: bold;">:</span>MenuItem = <span style="color: #0033ff; font-weight: bold;">new</span> MenuItem<span style="color: #000000;">&#40;</span> $name, $description, $vegetarian, $price <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> numberOfItems <span style="color: #000000; font-weight: bold;">&gt;</span>= MAX_ITEM <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;죄송합니다, 메뉴가 꽉 찼습니다. 더 이상 추가할 수 없습니다.&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span>
			<span style="color: #000000;">&#123;</span>
				menuItems<span style="color: #000000;">&#91;</span>numberOfItems<span style="color: #000000;">&#93;</span> = menuItem;
				numberOfItems<span style="color: #000000; font-weight: bold;">++</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #3f5fbf;">/*
		 * 이터레이터 패턴 구현으로 사용하지 않게된 메서드
		public function getMenuItems():MenuItem
		{
			return menuItems;
		}
		*/</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> createIterator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IIterator
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> DinerMenuIterator<span style="color: #000000;">&#40;</span> menuItems <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//기타 메뉴관련 메서드</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>팬케익하우스보다 약간 복잡하지만 Vector 를 사용했기 때문에 전체 메뉴의 갯수가 6개로 제한되어 있다는것 외에는 딱히 다른점은 없습니다.</p>
<p>그럼 이제 이터레이터 인터페이스를 보겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> interface IIterator
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> hasNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #339966; font-weight: bold;">function</span> next<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>다음 수행할 항목이 있는지 알아보는 hasNext() 메서드와 다음 항목을 리턴해주는 next()메서드가 있습니다.</p>
<p>팬케익하우스 이터레이터가 위의 인터페이스를 어떻게 구현했나 살펴볼까요?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> PancakeHouseIterator implements IIterator
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> items<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		<span style="color: #009900;">//생성자에서는 메뉴에서 넘어온 모든 메뉴를 한꺼번에 받음</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PancakeHouseIterator <span style="color: #000000;">&#40;</span> $items<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.items = $items;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//다음 메뉴 배열원소를 리턴</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> next<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> memuItem<span style="color: #000000; font-weight: bold;">:</span>MenuItem = items<span style="color: #000000;">&#91;</span><span style="color: #004993;">position</span><span style="color: #000000;">&#93;</span>;
			<span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">++</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> memuItem;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//다음 메뉴가 있는지 확인하고 Boolean을 리턴</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> hasNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #004993;">position</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= items.<span style="color: #004993;">length</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>다음은 디너하우스의 이터레이터 입니다. MenuItem 을 Vector 에 저장하고 있습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> DinerMenuIterator implements IIterator
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> items<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>MenuItem<span style="color: #000000; font-weight: bold;">&gt;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> DinerMenuIterator <span style="color: #000000;">&#40;</span> $items<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>MenuItem<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.items = $items;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> next<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> memuItem<span style="color: #000000; font-weight: bold;">:</span>MenuItem = items<span style="color: #000000;">&#91;</span><span style="color: #004993;">position</span><span style="color: #000000;">&#93;</span>;
			<span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">++</span>;
			<span style="color: #0033ff; font-weight: bold;">return</span> memuItem;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> hasNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//Vector 의 경우 원소가 없는 것은 null 이기 때문에 null 도 검사</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #004993;">position</span> <span style="color: #000000; font-weight: bold;">&gt;</span>= items.<span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">||</span> items<span style="color: #000000;">&#91;</span><span style="color: #004993;">position</span><span style="color: #000000;">&#93;</span> == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">else</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>그럼 실제 메뉴를 찍어줄 웨이트리스를 만나보겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Waitress
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> pancakeHouseMenu<span style="color: #000000; font-weight: bold;">:</span>IMenu;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> dinerMenu<span style="color: #000000; font-weight: bold;">:</span>IMenu;
&nbsp;
		<span style="color: #009900;">//웨이트리스는 생성자에서 양쪽 식당의 메뉴를 받아 변수에 저장</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Waitress<span style="color: #000000;">&#40;</span> $pancakeHouseMenu<span style="color: #000000; font-weight: bold;">:</span>IMenu, $dinerMenu<span style="color: #000000; font-weight: bold;">:</span>IMenu <span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.pancakeHouseMenu = $pancakeHouseMenu;
			<span style="color: #0033ff; font-weight: bold;">this</span>.dinerMenu = $dinerMenu;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//이터레이터 객체를 준비</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> printMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pancakeIterator<span style="color: #000000; font-weight: bold;">:</span>IIterator = pancakeHouseMenu.createIterator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dinerIterator<span style="color: #000000; font-weight: bold;">:</span>IIterator = dinerMenu.createIterator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;<span style="">\n</span>메뉴n----<span style="">\n</span>아침메뉴&quot;</span> <span style="color: #000000;">&#41;</span>;
			printItMenu<span style="color: #000000;">&#40;</span> pancakeIterator <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;<span style="">\n</span>메뉴n----<span style="">\n</span>점심메뉴&quot;</span> <span style="color: #000000;">&#41;</span>;
			printItMenu<span style="color: #000000;">&#40;</span> dinerIterator <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//실제 개별 메뉴를 찍어줌</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> printItMenu<span style="color: #000000;">&#40;</span> $iterator<span style="color: #000000; font-weight: bold;">:</span>IIterator <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span> $iterator.hasNext<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> menuItem<span style="color: #000000; font-weight: bold;">:</span>MenuItem = MenuItem<span style="color: #000000;">&#40;</span> $iterator.next<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> menuItem.getName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #990000;">&quot;, &quot;</span>, menuItem.getPrice<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #990000;">&quot;--&quot;</span>, menuItem.getDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>웨이트리스 코드에서는 각메뉴의 createIterator() 를 호출하여 이터레이터 객체를 얻고 MenuItem 의 갯수만큼 반복하여 메뉴를 얻습니다. </p>
<p>호스트코드에서는 웨이트리스 객체를 만들어 팬케익하우스와 디너하우스의 메뉴를 인자로 던져 모든 메뉴를 출력합니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> 
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Main extends <span style="color: #004993;">Sprite</span> 
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pancakeHouseMenu<span style="color: #000000; font-weight: bold;">:</span>PancakeHouseMenu = <span style="color: #0033ff; font-weight: bold;">new</span> PancakeHouseMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> dinerMenu<span style="color: #000000; font-weight: bold;">:</span>DinerMenu = <span style="color: #0033ff; font-weight: bold;">new</span> DinerMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> waitress<span style="color: #000000; font-weight: bold;">:</span>Waitress = <span style="color: #0033ff; font-weight: bold;">new</span> Waitress<span style="color: #000000;">&#40;</span> pancakeHouseMenu, dinerMenu <span style="color: #000000;">&#41;</span>;
			waitress.printMenu<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>출력된 모든 메뉴 입니다.<br />
<span style="color: #808080;"><br />
메뉴<br />
&#8212;-<br />
아침메뉴<br />
K&#038;B 팬케이크 세트 ,  2990 &#8212; 스크램블드 에그와 토스트가 곁들여진 팬케이크<br />
레귤러 팬케이크 세트 ,  2990 &#8212; 달걀 후라이와 소시지가 곁들여진 팬케이크<br />
블루베리 팬케이크 ,  3490 &#8212; 신선한 블루베리와 블루베리 시럽으로 만든 팬케이크<br />
와플 ,  3590 &#8212; 와플, 취향에 따라 블루베리나 딸기를 얹을 수 있습니다.</span><br />
<span style="color: #808080;"><br />
메뉴<br />
&#8212;-<br />
점심메뉴<br />
채식주의자용 BLT ,  2990 &#8212; 통밀 위에 (식물성)베이컨, 상추, 토마토를 얹은 메뉴<br />
BLT ,  2990 &#8212; 통밀 위에 베이컨, 상추, 토마토를 얹은 메뉴<br />
오늘의 스프 ,  3290 &#8212; 감자 샐러드를 곁들인 오늘의 스프<br />
핫도그 ,  3050 &#8212; 사워크라우트, 갖은 양념, 양파, 치즈가 곁들여진 핫도그</span></p>
<p>일견 왜 이렇게 복잡하게 반복을 수행해야하나 하는 생각이 들 수 있겠습니다. 책의 359페이지에 따르면 메뉴 항목을 구현하는 방식의 차이<sup>[<a href="http://ufx.kr/blog/188#footnote_1_188" id="identifier_1_188" class="footnote-link footnote-identifier-link" title="Array 와 Vector">02</a>]</sup> 때문에 항상 다른형식 메뉴판 숫자만큼의 for 문이 필요하게 되고, 웨이트리스가 채식주의자용 메뉴만 출력하는 메서드를 구현해야한다면 다른 메서드에서 계속 반복적인 코드를 추가해야 하고, 게다가 구현방식이 다른 다른 식당 메뉴를 더 구현해야 한다면 또다시 모든 메서드에 반복문을 하나씩 더 추가해야 하는데, 이터레이터 패턴으로 구조를 잘 짜 놓으면 이러한 경우에 생길 수 있는 에러를 미연에 방지하고 유지보수성도 좋아지게 되는 것입니다.</p>
<p>그러나 이터레이터는 이러한 상이한 형식을 통합하여 반복시키는것 이상의 훨씬 추상적인 작업도 수행할 수 있습니다. 이터레이터 패턴을 자유자재로 구현하게 되면 반복이 될 것 같지 않은 요소들을 반복시킬 수 있는 길이 열리게 되는 것이죠. 이터레이터 패턴을 다루는 다른 글을 포스팅 하게 되면 이 패턴을 사용하는 진짜 이유에 대해서도 살펴보도록 하겠습니다.</p>
<p><a href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=20" title="Downloaded 210 times"><img src="http://ufx.kr/blog/wp-content/uploads/2009/07/zip_icon.gif" />&nbsp; Iterator Pattern Example 액션스크립트 코드 다운로드</a> - <br />
이 샘플코드는 Vector 클래스를 사용했기 때문에 Flash Player 10 으로 컴파일 해야 합니다.</p>
<p>2009년 5월 27일 초안 작성 / 2009 11월 24일 약간 수정하여 출판</p>
<ol class="footnotes"><li id="footnote_0_188" class="footnote">액션스크립트랑은 약간 다릅니다.</li><li id="footnote_1_188" class="footnote">Array 와 Vector</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/188/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

