<?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; Design Pattern</title>
	<atom:link href="http://ufx.kr/blog/category/flashpaltform/designpattern/feed" rel="self" type="application/rss+xml" />
	<link>http://ufx.kr/blog</link>
	<description>Flash + ActionScript &#38; Design</description>
	<lastBuildDate>Fri, 03 Sep 2010 05:15:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 112 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>
		<item>
		<title>Command Pattern 커맨드 패턴을 이용한 history undo 작업취소 기능 구현</title>
		<link>http://ufx.kr/blog/330</link>
		<comments>http://ufx.kr/blog/330#comments</comments>
		<pubDate>Sat, 31 Oct 2009 08:51:14 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Command Pattern]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[history undo]]></category>
		<category><![CDATA[undo]]></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=330</guid>
		<description><![CDATA[자바 코드로 된 디자인 패턴 관련 예제를 액션스크립트 코드로 번역하고, 컴파일을 해 보면 &#8220;이것은 마치.. 마법같잖아.&#8221; 하고 느끼는 때가 많습니다.
최하위에 위치한 구상 클래스로부터 시작해서 대략 한 두번 추상층을 거치고 호스트코드[01] 까지 거슬러 올라가 코드가 어떻게 실행되는지 살펴보면 (물리적으로) 마땅히 그런 결과가 나오는 것이 맞긴한데, 전체 클래스들이 관계한 모양새와 그것들이 보여주는 결과만을 보면 신기하기 이를데 없습니다. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-331" title="command_z" src="http://ufx.kr/blog/wp-content/uploads/2009/10/command_z.jpg" alt="command_z" width="233" height="350" />자바 코드로 된 디자인 패턴 관련 예제를 액션스크립트 코드로 번역하고, 컴파일을 해 보면 &#8220;이것은 마치.. 마법같잖아.&#8221; 하고 느끼는 때가 많습니다.<br />
최하위에 위치한 구상 클래스로부터 시작해서 대략 한 두번 추상층을 거치고 호스트코드<sup>[<a href="http://ufx.kr/blog/330#footnote_0_330" id="identifier_0_330" class="footnote-link footnote-identifier-link" title=" 액션스크립트로 말하자면 도큐먼트 클래스 이죠 ">01</a>]</sup> 까지 거슬러 올라가 코드가 어떻게 실행되는지 살펴보면 (물리적으로) 마땅히 그런 결과가 나오는 것이 맞긴한데, 전체 클래스들이 관계한 모양새와 그것들이 보여주는 결과만을 보면 신기하기 이를데 없습니다. 이런 것이 디자인 패턴의 매력이겠죠?</p>
<p>오늘 포스팅하게 될 내용도 그런 신기한 것 중 하나로, 커맨드 패턴을 이용한 작업취소(언두: undo) 또는 작업내역(history) 기능 구현 입니다.<br />
헤드퍼스트 디자인 패턴에 있는 디자인 패턴 부분의 내용을 기본으로 이해하기 좋도록 시각적인 구성을 추가했고, 중요한 기능 중 하나를 추가해 보았습니다.</p>
<p>undo 나 history 기능 자체는 디자인 패턴이 아닙니다. 이 기능은 커맨드 패턴을 구현하다 보면 서비스로 딸려오는 (자연스럽게 구성할 수 있는) 기능 정도로 이해하는 것이 좋습니다.</p>
<p><span id="more-330"></span>먼저 최종 결과 파일부터 볼까요?<br />
<embed type="application/x-shockwave-flash" width="550" height="400" src="http://ufx.kr/blog/wp-content/uploads/2009/10/CommandPatternHistoryUndo.swf"> </embed></p>
<p>위의 컴파일 결과물에서 왼쪽은 리모컨 입니다. 각 숫자는 선풍기의 회전수를 나타내는 회전 강도가 되겠고요, 아래쪽에 undo, redo 버튼, 그리고 현재 history 내역이 무엇인지 볼 수 있는 view history 버튼도 만들어져 있습니다.<br />
각각의 버튼을 눌러보세요. </p>
<p>패턴과는 관계 없지만, 화면에서 보이는 선풍기는 정확하게 표현하자면 실링팬 입니다. 더운 지방이 나오는 영화를 보면 천정에서 천천히 돌아가는 선풍기가 바로 이것이죠. 영어 단어로는 ceiling fan 이라 하는데, 직역하자면 천정 선풍기라는 의미가 되겠습니다.<br />
한편 ceil 이라는 단어는 Math 클래스 에서도 볼 수 있죠. Math.ceil( 1.3 ) 의 결과는 소수점 올림이 적용되어 2 가 나오게 되겠죠? 이때 ceil 이라는 의미가 &#8216;천정&#8217; 또는 &#8216;올림&#8217; 이라면, floor 는 &#8216;바닥&#8217; 또는 &#8216;내림&#8217; 이라는 상대적인 의미로 Math.floor() 에서도 사용됩니다.</p>
<p>ICommand 인터페이스는 이전 커맨드 패턴을 다룬 예제와 전혀 다르지 않습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 ICommand
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> execute<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>아래는 선풍기를 off 한 상태의 커맨드 클래스 입니다. 이렇게 선풍기의 속도를 커맨드 클래스로 만든 클래스가 속도별로 3개가 더 있습니다. (전체 코드는 하단에 다운로드 링크가 있습니다)</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
</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> CeilingFanOffCommand implements ICommand
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> ceilingFan<span style="color: #000000; font-weight: bold;">:</span>CeilingFan;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CeilingFanOffCommand<span style="color: #000000;">&#40;</span> $ceilingFan<span style="color: #000000; font-weight: bold;">:</span>CeilingFan <span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.ceilingFan = $ceilingFan
		<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> execute<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>
			ceilingFan.off<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>CeilingFan 클래스는 아래와 같습니다. 4가지 속도에 해당하는 메서드를 public 으로 제공하고 있고, 메서드가 실행되면 speed 라는 이름의 변수에 저장하고 있습니다.</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
47
48
49
50
51
52
53
54
55
</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>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CeilingFan extends <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> location<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> speed<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> static const <span style="color: #004993;">HIGH</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">3</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> static const <span style="color: #004993;">MEDIUM</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">2</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> static const <span style="color: #004993;">LOW</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span>;
		<span style="color: #0033ff; font-weight: bold;">public</span> static const OFF<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> CeilingFan<span style="color: #000000;">&#40;</span> $location<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</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: #0033ff; font-weight: bold;">this</span>.fan.<span style="color: #004993;">cacheAsBitmap</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>.location = $location;
			<span style="color: #0033ff; font-weight: bold;">this</span>.speed = OFF;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ENTER_FRAME</span>, onEnterFrameHandler <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> high<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>
			speed = <span style="color: #004993;">HIGH</span>;
			outputText<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> medium<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>
			speed = <span style="color: #004993;">MEDIUM</span>;
			outputText<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> low<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>
			speed = <span style="color: #004993;">LOW</span>;
			outputText<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> off<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>
			speed = OFF;
			outputText<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getSpeed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> speed;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> outputText<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>
			txtSpeed.<span style="color: #004993;">text</span> = location <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot; : Fan Speed - &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> speed;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onEnterFrameHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</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: #0033ff; font-weight: bold;">this</span>.fan.<span style="color: #004993;">rotation</span> <span style="color: #000000; font-weight: bold;">+</span>= speed;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>화면에 있는 선풍기의 날개를 속도에 맞게 회전시킬 것이므로 Event.ENTER_FRAME 을 이용해 fla 의 스테이지에 정의 되어 있는 fan 무비클립을 계속 갱신시키고 있습니다. 가장 아래쪽에 있는 onEnterFrameHandler() 메서드에 있는 fan 의 회전속도는 변수 speed 의 숫자값에 따라 달라지겠죠. speed 가 바뀌는 즉시 fan 의 회전속도도 바뀌게 될 것입니다.</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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
</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> fl.controls.Button;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #000000; font-weight: bold;">*</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> RemoteControlWithHistory extends <span style="color: #004993;">Sprite</span> 
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> arrCommands<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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>;;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> commandHistory<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> historyIndex<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> txtHistory<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextField</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextField</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> RemoteControlWithHistory<span style="color: #000000;">&#40;</span> $ceilingFan<span style="color: #000000; font-weight: bold;">:</span>CeilingFan, $numberOfbuttons<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">4</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> noCommand<span style="color: #000000; font-weight: bold;">:</span>ICommand = <span style="color: #0033ff; font-weight: bold;">new</span> NoCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> $numberOfbuttons; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				arrCommands<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> = noCommand;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> ceilingFanHigh<span style="color: #000000; font-weight: bold;">:</span>CeilingFanHighCommand = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFanHighCommand<span style="color: #000000;">&#40;</span> $ceilingFan <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ceilingFanMedium<span style="color: #000000; font-weight: bold;">:</span>CeilingFanMediumCommand = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFanMediumCommand<span style="color: #000000;">&#40;</span> $ceilingFan <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ceilingFanLow<span style="color: #000000; font-weight: bold;">:</span>CeilingFanLowCommand = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFanLowCommand<span style="color: #000000;">&#40;</span> $ceilingFan <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ceilingFanOff<span style="color: #000000; font-weight: bold;">:</span>CeilingFanOffCommand = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFanOffCommand<span style="color: #000000;">&#40;</span> $ceilingFan <span style="color: #000000;">&#41;</span>;
&nbsp;
			setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, ceilingFanOff <span style="color: #000000;">&#41;</span>;
			setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, ceilingFanLow <span style="color: #000000;">&#41;</span>;
			setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2</span>, ceilingFanMedium <span style="color: #000000;">&#41;</span>;
			setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">3</span>, ceilingFanHigh <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span> $numberOfbuttons <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span> $numberOfbuttons<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: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">x</span> = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">10</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> $numberOfbuttons; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> button<span style="color: #000000; font-weight: bold;">:</span>Button = <span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				button.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, buttonWasPushed <span style="color: #000000;">&#41;</span>;
				button.label = i.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				button.<span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">30</span>;
				button.<span style="color: #004993;">y</span> = i <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">&#40;</span> button.<span style="color: #004993;">height</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">5</span> <span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> button <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> undoButton<span style="color: #000000; font-weight: bold;">:</span>Button = <span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> redoButton<span style="color: #000000; font-weight: bold;">:</span>Button = <span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> historylistButton<span style="color: #000000; font-weight: bold;">:</span>Button = <span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			undoButton.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">200</span>;
			redoButton.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">230</span>;
			historylistButton.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">260</span>;
			undoButton.label = <span style="color: #990000;">&quot;undo&quot;</span>;
			redoButton.label = <span style="color: #990000;">&quot;redo&quot;</span>;
			historylistButton.label = <span style="color: #990000;">&quot;view history&quot;</span>;
&nbsp;
			undoButton.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, undoButtonWasPushed <span style="color: #000000;">&#41;</span>
			redoButton.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, redoButtonWasPushed <span style="color: #000000;">&#41;</span>
			historylistButton.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, historyView <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> undoButton <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> redoButton <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> historylistButton <span style="color: #000000;">&#41;</span>;
&nbsp;
			txtHistory.<span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">300</span>;
			txtHistory.<span style="color: #004993;">height</span> = <span style="color: #000000; font-weight:bold;">300</span>;
			txtHistory.<span style="color: #004993;">x</span> = historylistButton.<span style="color: #004993;">width</span>;
			txtHistory.<span style="color: #004993;">y</span> = historylistButton.<span style="color: #004993;">y</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> txtHistory <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">//선풍기의 초기값은 정지상태</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.buttonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">null</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> setCommand<span style="color: #000000;">&#40;</span> $slot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $command<span style="color: #000000; font-weight: bold;">:</span>ICommand <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>
			arrCommands<span style="color: #000000;">&#91;</span> $slot <span style="color: #000000;">&#93;</span> = $command;
		<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> buttonWasPushed<span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> = <span style="color: #0033ff; font-weight: bold;">null</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> slot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000;">&#40;</span> $e == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">uint</span><span style="color: #000000;">&#40;</span> $e.<span style="color: #004993;">target</span>.label <span style="color: #000000;">&#41;</span>;
			arrCommands<span style="color: #000000;">&#91;</span> slot <span style="color: #000000;">&#93;</span>.execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			stackProcess<span style="color: #000000;">&#40;</span> arrCommands<span style="color: #000000;">&#91;</span> slot <span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> stackProcess<span style="color: #000000;">&#40;</span> $command<span style="color: #000000; font-weight: bold;">:</span>ICommand <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: #009900;">//historyIndex가 undo 중일때 undo나 redo가 아닌 새로운 command가 들어올경우 redo 부분이 없어지도록 배열길이를 조정</span>
			commandHistory.<span style="color: #004993;">length</span> = historyIndex;
			commandHistory.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> $command <span style="color: #000000;">&#41;</span>;
			historyIndex = commandHistory.<span style="color: #004993;">length</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> undoButtonWasPushed<span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> = <span style="color: #0033ff; font-weight: bold;">null</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> historyIndex <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				commandHistory<span style="color: #000000;">&#91;</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">--</span>historyIndex <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#93;</span>.execute<span style="color: #000000;">&#40;</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>
				<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;No more Undo&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</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> redoButtonWasPushed<span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span> = <span style="color: #0033ff; font-weight: bold;">null</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> historyIndex <span style="color: #000000; font-weight: bold;">&lt;</span> commandHistory.<span style="color: #004993;">length</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				commandHistory<span style="color: #000000;">&#91;</span> historyIndex<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#93;</span>.execute<span style="color: #000000;">&#40;</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>
				<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;No more Redo&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</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> getCommandList<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: #6699cc; font-weight: bold;">var</span> str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;<span style="">\n</span>------ Remote Control -------&quot;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> arrCommands.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				str <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>[slot &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;] &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> arrCommands<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> 
			<span style="color: #000000;">&#125;</span>
			str <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>[undo] &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> commandHistory
			<span style="color: #0033ff; font-weight: bold;">return</span> str
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> historyView<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</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>
			txtHistory.<span style="color: #004993;">text</span> = <span style="color: #990000;">&quot;******History list******&quot;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> element<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #0033ff; font-weight: bold;">in</span> commandHistory <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				txtHistory.<span style="color: #004993;">appendText</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;<span style="">\n</span>&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> element.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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>클래스가 좀 길군요. 하지만 대부분 생성자 함수와 init() 메서드의 초기화 내용들이므로 지나쳐도 무방하고, 실제 커맨드 패턴을 구현한 내용은 setCommand() 메서드 부터 입니다. 이전 두 개의 커맨드 패턴 관련 포스팅을 읽으신 분들은 이 메서드가 리모컨의 각 버튼에<sup>[<a href="http://ufx.kr/blog/330#footnote_1_330" id="identifier_1_330" class="footnote-link footnote-identifier-link" title="정확하게는 커맨드 슬롯을 저장하는 배열에">02</a>]</sup> 커맨드 객체를 저장해 주는 역할을 한다는 것을 알고 계실겁니다.</p>
<p>setCommand() 메서드 아래에 있는 buttonWasPushed() 메서드 에서는 각 속도가 할당된 버튼을 클릭하면 커맨드 객체를 통해 CeilingFan 객체에 각 속도에 해당하는 메서드를 실행하게 됩니다. 추가로 stackProcess() 메서드를 실행시키고 있는데, stackProcess() 에서는 history undo 기능을 수행할때 필연적으로 만나게 되는 문제에 대한 해결 방법을 제시하고 있습니다.</p>
<p><strong><span style="color: #008080;">history undo 실행 중에 만나는 새로운 커맨드에 대한 처리</span></strong></p>
<p>stackProcess() 메서드가 실행되면 인자로 받은 커맨드 객체를 commandHistory 배열에 넣는 기본 기능을 수행하면서 마지막 라인에서 commandHistory 의 배열 길이를 historyIndex 라는 별도의 변수에 저장합니다. 배열 길이는 언제든 참조가 가능한데 왜 이렇게 하는 것일까요?<br />
stackProcess() 가 그렇게 한번 실행된 후, 두 번째 실행되면 그 이유를 알 수 있습니다. </p>
<p>stackProcess() 메서드의 첫 번째 라인을 보면 commandHistory 의 배열 길이를 강제로 historyIndex 로 맞줘주는 내용이 보입니다. 커맨드 객체가 계속 새로운 커맨드로 진행되는 경우라면 commandHistory.length 와 historyIndex 의 길이는 같을 것이므로<sup>[<a href="http://ufx.kr/blog/330#footnote_2_330" id="identifier_2_330" class="footnote-link footnote-identifier-link" title=" 아직 commandHistory.push( $command ) 하기 이전이므로 ">03</a>]</sup> 이 코드는 실행되는 듯 마는 듯 지나치게 됩니다만, 만약 현재 상태가 undo 상태라면 이야기는 달라집니다.</p>
<p>이 문제는 모든 history undo 기능이 가지고 있는 공통의 문제라 할 수 있겠는데요, 사용자가 A, B, C, D 의 커맨드를 순서대로 실행하다가 undo 를 두번 했다면 현재는 B가 실행되고 있겠죠? 여기서 사용자가 고맙게도 redo 를 다시 두번 실행해 준다면 좋겠지만, 사용자가 어디 그런가요? 현재 B가 실행되고 있는 상황에서 C을 건너뛰고 D를 선택하는 상황이 발생하게 됩니다. </p>
<p>만약 stackProcess() 에서 push() 만 하는 경우라면 어떻게 될까요? undo 상태의 B위치에서 누른 새로운 D는 commandHistory의 5번째 원소가 될테죠.<br />
commandHistory 를 나열해보면 이렇게 되어있겠네요. [ A, B, C, D, D ].<br />
이것은 우리가 바라는게 아니라는것을 알 수 있습니다. undo 상태에서 redo 가 아닌 새로운 커맨드가 들어올 때, history 를 저장하고 있는 배열에서 redo 가 될 부분은 모두 버리고 새로운 커맨드를 쌓아나가야 하는 것이죠.<br />
그래서 우리가 의도한 [ A, B, D ] 가 되도록 하기 위해서 commandHistory.push( $command ) 를 하기 직전에, 별도로 저장한 historyIndex 로 배열의 길이를 조절해 주는 것입니다.</p>
<p>&#8212;</p>
<p>stackProcess() 메서드 아래에는 undoButtonWasPushed() 메서드와 redoButtonWasPushed() 메서드가 있어서 undo나 redo 버튼이 눌렸을때에는 새로운 커맨드를 실행하는 것이 아니라 commandHistory 배열에서 앞뒤로 인덱스를 움직이며 커맨드를 실행해 줍니다.</p>
<p>마지막으로 호스트코드를 살펴보겠습니다. 이쪽은 뭐 간단합니다.<br />
선풍기와 리모컨 객체를 하나씩 만들어 addChild 했을 뿐이네요.</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
</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> ceilingFan<span style="color: #000000; font-weight: bold;">:</span>CeilingFan = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFan<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;Living Room&quot;</span> <span style="color: #000000;">&#41;</span>
			ceilingFan.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">400</span>;
			ceilingFan.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">200</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> ceilingFan <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> remoteControl<span style="color: #000000; font-weight: bold;">:</span>RemoteControlWithHistory = <span style="color: #0033ff; font-weight: bold;">new</span> RemoteControlWithHistory<span style="color: #000000;">&#40;</span> ceilingFan, <span style="color: #000000; font-weight:bold;">4</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> remoteControl <span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>이 예제는 fla 에 있는 이미지 자원을 사용하기 때문에 컴파일도 fla 에서 합니다. 화면에서 모든것을 표현하고 있기 때문에 trace() 대신 화면을 관찰하면 됩니다. 현재 히스토리 현황을 알기 위해서 view history 버튼을 누르세요.</p>
<a href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=18" title="Downloaded 122 times"><img src="http://ufx.kr/blog/wp-content/uploads/2009/07/zip_icon.gif" />&nbsp; Command Pattern ( History Undo) 액션스크립트 코드 다운로드</a> - fla, as, swf 파일 포함.
fla 파일은 CS4 에서 작성
<ol class="footnotes"><li id="footnote_0_330" class="footnote"> 액션스크립트로 말하자면 도큐먼트 클래스 이죠 </li><li id="footnote_1_330" class="footnote">정확하게는 커맨드 슬롯을 저장하는 배열에</li><li id="footnote_2_330" class="footnote"> 아직 commandHistory.push( $command ) 하기 이전이므로 </li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/330/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Command Pattern 커맨드 패턴을 액션스크립트로 컨버팅. 두 번째 – Head First Design Pattern</title>
		<link>http://ufx.kr/blog/327</link>
		<comments>http://ufx.kr/blog/327#comments</comments>
		<pubDate>Sat, 08 Aug 2009 20:58:50 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Command Pattern]]></category>
		<category><![CDATA[Head First]]></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=327</guid>
		<description><![CDATA[지난 포스트에서는 간단한 형태의 커맨드 패턴에 대해 살펴보았습니다. 이 포스트는 여러가지 커맨드 객체를 조합하여 실제 사용될법한 커맨드 패턴 리모컨을 만들어 보겠습니다. 
지난 포스트에서 Light 구상객체를 만들어, LightOnCommand 객체로 감싸고, LightOnCommand 객체를 SimpleRemote 에 setCommand() 메서드를 이용하여 할당 한 후, 필요할 때마다 호출하는것 까지 해 보았습니다.
커맨드 패턴은 이렇게 구상 객체의 메서드를 직접 호출하지 않고, 구상객체를 감싸고 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://ufx.kr/blog/wp-content/uploads/2009/08/commad_center.jpg" alt="commad_center" title="commad_center" width="208" height="151" class="alignleft size-full wp-image-326" /><a href="./248">지난 포스트</a>에서는 간단한 형태의 커맨드 패턴에 대해 살펴보았습니다. 이 포스트는 여러가지 커맨드 객체를 조합하여 실제 사용될법한 커맨드 패턴 리모컨을 만들어 보겠습니다. </p>
<p>지난 포스트에서 Light 구상객체를 만들어, LightOnCommand 객체로 감싸고, LightOnCommand 객체를 SimpleRemote 에 setCommand() 메서드를 이용하여 할당 한 후, 필요할 때마다 호출하는것 까지 해 보았습니다.</p>
<p>커맨드 패턴은 이렇게 구상 객체의 메서드를 직접 호출하지 않고, 구상객체를 감싸고 있는 커맨드 객체를 호출하여 캡슐화 한 것입니다. 이렇게 캡슐화를 하게 되면, 새로운 구상객체가 생겨도 리모컨 객체의 코드의 변경 없이 각 기능별로 ICommand 인터페이스를 구현한 커맨드 객체를 만들고 리모컨 객체에 할당하여 호스트코드에서 모두 동일한 명령어로 구상객체의 메서드를 호출할 수 있게되는 장점을 누릴 수 있게 됩니다.</p>
<p><span id="more-327"></span><br />
지난 포스트에서는 Light 구상객체를 다뤄보았으니, 이번에는 좀 다른 구상객체를 살펴보겠습니다.</p>
<p>먼저 지난번과 같은 ICommand 인터페이스 입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 ICommand
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> execute<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Stereo 클래스는 오디오 시스템을 표현하는 구상 클래스 입니다. 단순히 불을 켜고 끄는것이 아니라 여러 가지 기능을 수행하고 있습니다.</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
</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> Stereo
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> location<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Stereo<span style="color: #000000;">&#40;</span> $location<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.location = $location
		<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> on<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> location , <span style="color: #990000;">&quot;스테레오 켬&quot;</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> off<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> location , <span style="color: #990000;">&quot;스테레오 끔&quot;</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> setCD<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> location , <span style="color: #990000;">&quot;스테레오에 CD 넣음&quot;</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> setDVD<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> location , <span style="color: #990000;">&quot;스테레오에 DVD 넣음&quot;</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> setRadio<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> location , <span style="color: #990000;">&quot;스테레오에 라디오 켬&quot;</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> setVolume<span style="color: #000000;">&#40;</span> $volume<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</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: #004993;">trace</span><span style="color: #000000;">&#40;</span> location , <span style="color: #990000;">&quot;스테레오 볼륨을&quot;</span>, $volume, <span style="color: #990000;">&quot;으로 조정&quot;</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>StereoOnWithCDCommand 클래스는 ICommand 를 구현하는 커맨드 객체 클래스 입니다.</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
</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> StereoOnWithCDCommand implements ICommand
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> stereo<span style="color: #000000; font-weight: bold;">:</span>Stereo;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> StereoOnWithCDCommand<span style="color: #000000;">&#40;</span> $stereo<span style="color: #000000; font-weight: bold;">:</span>Stereo <span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.stereo = $stereo;
		<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> execute<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>
			stereo.on<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			stereo.setCD<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			stereo.setVolume<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">11</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>execute() 메서드를 호출하면 스테레오시스템을 켜고, CD를 넣고, 볼륨을 11로 설정하게 됩니다.<br />
StereoOffCommand 는 execute() 메서드 안에 stereo.off() 가 있다는 것만 제외하면 StereoOnWithCDCommand 클래스와 동일한 클래스 입니다.<br />
StereoOffCommand 클래스를 포함한 전체 코드는 아래에서 다운로드 받을 수 있는 링크가 있습니다.</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
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> RemoteControl
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> onCommands<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> offCommands<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> RemoteControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			onCommands = <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>;
			offCommands = <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;
			<span style="color: #6699cc; font-weight: bold;">var</span> noCommand<span style="color: #000000; font-weight: bold;">:</span>ICommand = <span style="color: #0033ff; font-weight: bold;">new</span> NoCommand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">7</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				onCommands<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> = noCommand
				offCommands<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span> = noCommand
			<span style="color: #000000;">&#125;</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> setCommand<span style="color: #000000;">&#40;</span> $slot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $onCommand<span style="color: #000000; font-weight: bold;">:</span>ICommand, $offCommand<span style="color: #000000; font-weight: bold;">:</span>ICommand <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>
			onCommands<span style="color: #000000;">&#91;</span> $slot <span style="color: #000000;">&#93;</span> = $onCommand;
			offCommands<span style="color: #000000;">&#91;</span> $slot <span style="color: #000000;">&#93;</span> = $offCommand;
		<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> onButtonWasPushed<span style="color: #000000;">&#40;</span> $slot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</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>
			onCommands<span style="color: #000000;">&#91;</span> $slot <span style="color: #000000;">&#93;</span>.execute<span style="color: #000000;">&#40;</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> offButtonWasPushed<span style="color: #000000;">&#40;</span> $slot<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</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>
			offCommands<span style="color: #000000;">&#91;</span> $slot <span style="color: #000000;">&#93;</span>.execute<span style="color: #000000;">&#40;</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> <span style="color: #004993;">toString</span><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: #6699cc; font-weight: bold;">var</span> str<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;<span style="">\n</span>------ Remote Control -------&quot;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> onCommands.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				str <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>[slot &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> i <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;] &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> onCommands<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;  &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> offCommands<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> str
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>SimpleRemoteControl 에 비해서 코드가 많이 추가되었지만 하는일은 변함이 없습니다. 이번에는 다뤄야 하는 커맨드객체의 숫자가 여러개 이므로 배열로 관리를 하고 있고, on 버튼과 off 버튼을 따로 누르도록 만들었습니다. 어떤 구상객체를 담고 있는 커맨드 객체라 하더라도 리모컨 객체는 아무생각없이 execute() 만 호출하면 되는 것이죠. </p>
<p>호스트 코드는 컨버팅 대상인 자바코드 전체를 번역하였기 때문에 내용이 다소 깁니다만, Stereo 객체만을 눈여겨 보시면 되겠습니다.</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
47
48
</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> remoteControl<span style="color: #000000; font-weight: bold;">:</span>RemoteControl = <span style="color: #0033ff; font-weight: bold;">new</span> RemoteControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> livingRoomLight<span style="color: #000000; font-weight: bold;">:</span>Light = <span style="color: #0033ff; font-weight: bold;">new</span> Light<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;거실&quot;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> kitchenLight<span style="color: #000000; font-weight: bold;">:</span>Light = <span style="color: #0033ff; font-weight: bold;">new</span> Light<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주방&quot;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> cellingFan<span style="color: #000000; font-weight: bold;">:</span>CeilingFan = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFan<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;거실&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> garageDoor<span style="color: #000000; font-weight: bold;">:</span>GarageDoor = <span style="color: #0033ff; font-weight: bold;">new</span> GarageDoor<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;&quot;</span> <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> stereo<span style="color: #000000; font-weight: bold;">:</span>Stereo = <span style="color: #0033ff; font-weight: bold;">new</span> Stereo<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;거실&quot;</span> <span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> livingRoomLightOn<span style="color: #000000; font-weight: bold;">:</span>LightOnCommand = <span style="color: #0033ff; font-weight: bold;">new</span> LightOnCommand<span style="color: #000000;">&#40;</span> livingRoomLight <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> livingRoomLightOff<span style="color: #000000; font-weight: bold;">:</span>LightOffCommand = <span style="color: #0033ff; font-weight: bold;">new</span> LightOffCommand<span style="color: #000000;">&#40;</span> livingRoomLight <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> kitchenLightOn<span style="color: #000000; font-weight: bold;">:</span>LightOnCommand = <span style="color: #0033ff; font-weight: bold;">new</span> LightOnCommand<span style="color: #000000;">&#40;</span> kitchenLight <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> kitchenLightOff<span style="color: #000000; font-weight: bold;">:</span>LightOffCommand = <span style="color: #0033ff; font-weight: bold;">new</span> LightOffCommand<span style="color: #000000;">&#40;</span> kitchenLight <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> ceilingFanOn<span style="color: #000000; font-weight: bold;">:</span>CeilingFanOnCommand = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFanOnCommand<span style="color: #000000;">&#40;</span> cellingFan <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> ceilingFanOff<span style="color: #000000; font-weight: bold;">:</span>CeilingFanOffCommand = <span style="color: #0033ff; font-weight: bold;">new</span> CeilingFanOffCommand<span style="color: #000000;">&#40;</span> cellingFan <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> garageDoorUp<span style="color: #000000; font-weight: bold;">:</span>GarageDoorUpCommand = <span style="color: #0033ff; font-weight: bold;">new</span> GarageDoorUpCommand<span style="color: #000000;">&#40;</span> garageDoor <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> garageDoorDown<span style="color: #000000; font-weight: bold;">:</span>GarageDoorDownCommand = <span style="color: #0033ff; font-weight: bold;">new</span> GarageDoorDownCommand<span style="color: #000000;">&#40;</span> garageDoor <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> stereoOnWithCD<span style="color: #000000; font-weight: bold;">:</span>StereoOnWithCDCommand = <span style="color: #0033ff; font-weight: bold;">new</span> StereoOnWithCDCommand<span style="color: #000000;">&#40;</span> stereo <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> stereoOff<span style="color: #000000; font-weight: bold;">:</span>StereoOffCommand = <span style="color: #0033ff; font-weight: bold;">new</span> StereoOffCommand<span style="color: #000000;">&#40;</span> stereo <span style="color: #000000;">&#41;</span>;
&nbsp;
			remoteControl.setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, livingRoomLightOn, livingRoomLightOff <span style="color: #000000;">&#41;</span>;
			remoteControl.setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, kitchenLightOn, kitchenLightOff <span style="color: #000000;">&#41;</span>;
			remoteControl.setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2</span>, ceilingFanOn, ceilingFanOff <span style="color: #000000;">&#41;</span>;
			remoteControl.setCommand<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">3</span>, stereoOnWithCD, stereoOff <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> remoteControl.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
			remoteControl.onButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.offButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.onButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.offButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.onButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.offButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.onButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">3</span> <span style="color: #000000;">&#41;</span>;
			remoteControl.offButtonWasPushed<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">3</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>Stereo 객체를 생성하고 두 개의 커맨드 객체 StereoOnWithCDCommand( stereo ) 와 StereoOffCommand( stereo ) 에 넣어, setCommand 메서드를 이용해서 커맨드 객체를 러퍼런스로 저장하였습니다.<br />
이번 리모컨은 on 버튼과 off 버튼이 따로 있는 리모컨 이므로 사용할 때도 각각의 메서드를 호출하게 됩니다.</p>
<p>실행 결과는 다음과 같습니다.<br />
<span style="color: #808080;"><br />
&#8212;&#8212; Remote Control &#8212;&#8212;-<br />
[slot 0] [object LightOnCommand]  [object LightOffCommand]<br />
[slot 1] [object LightOnCommand]  [object LightOffCommand]<br />
[slot 2] [object CeilingFanOnCommand]  [object CeilingFanOffCommand]<br />
[slot 3] [object StereoOnWithCDCommand]  [object StereoOffCommand]<br />
[slot 4] [object NoCommand]  [object NoCommand]<br />
[slot 5] [object NoCommand]  [object NoCommand]<br />
[slot 6] [object NoCommand]  [object NoCommand]<br />
거실 불 켜짐<br />
거실 불 꺼짐<br />
주방 불 켜짐<br />
주방 불 꺼짐<br />
거실 선풍기 속도 높음<br />
거실 선풍기 끔<br />
거실 스테레오 켬<br />
거실 스테레오에 CD 넣음<br />
거실 스테레오 볼륨을 11 으로 조정<br />
거실 스테레오 끔<br />
</span></p>
<a href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=17" title="Downloaded 123 times"><img src="http://ufx.kr/blog/wp-content/uploads/2009/07/zip_icon.gif" />&nbsp; Command Pattern Example 액션스크립트 코드 다운로드</a> - 18개의 *.as 파일을 포함하고 있는 zip 파일 입니다.
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/327/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Command Pattern 커맨드 패턴을 액션스크립트로 컨버팅 – Head First Design Pattern</title>
		<link>http://ufx.kr/blog/248</link>
		<comments>http://ufx.kr/blog/248#comments</comments>
		<pubDate>Thu, 18 Jun 2009 04:42:03 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Command Pattern]]></category>
		<category><![CDATA[Head First]]></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=248</guid>
		<description><![CDATA[헤드퍼스트 디자인 패턴의 제 6 장 커맨드 패턴 입니다. 커맨드 패턴은 객체지향 프로그래밍에서 가장 중요한 패턴 중 하나이므로 최소한의 커맨드 패턴을 적용하여 개념에 대한 이해를 돕는 포스트로부터 시작하여 차근차근 단계를 밟아 몇개의 포스트로 나눠서 작성 하겠습니다. 이 포스트는 그 중 가장 간단한 형태의 커맨드 패턴에 대해 다룹니다.
먼저 커맨드 패턴에 대한 정의를 보겠습니다.
커맨드 패턴 &#8211; 커맨드 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://ufx.kr/blog/wp-content/uploads/2009/06/remote_controller.jpg" alt="remote_controller" title="remote_controller" width="240" height="180" class="alignleft size-full wp-image-249" />헤드퍼스트 디자인 패턴의 제 6 장 커맨드 패턴 입니다. 커맨드 패턴은 객체지향 프로그래밍에서 가장 중요한 패턴 중 하나이므로 최소한의 커맨드 패턴을 적용하여 개념에 대한 이해를 돕는 포스트로부터 시작하여 차근차근 단계를 밟아 몇개의 포스트로 나눠서 작성 하겠습니다. 이 포스트는 그 중 가장 간단한 형태의 커맨드 패턴에 대해 다룹니다.</p>
<p>먼저 커맨드 패턴에 대한 정의를 보겠습니다.</p>
<p><span style="color: #808080;">커맨드 패턴 &#8211; 커맨드 패턴을 이용하면 요구 사항을 객체로 캡슐화 할 수 있으며, 매개변수를 써서 여러 가지 다른 요구 사항을 집어넣을 수도 있습니다. 또한 요청 내역을 큐에 저장하거나 로그로 기록할 수 도 있으며, 작업취소 기능도 지원 가능합니다.</span></p>
<p>본문 예제에서는 리모컨을 이용하여 홈 오토메이션을 구현하는 내용을 다루고 있습니다.</p>
<p><span id="more-248"></span>커맨드 객체는 모두 같은 인터페이스를 구현 해야 합니다. 커맨드 메서드이기 때문에 메서드 이름은 excute() 가 적당하겠네요.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 ICommand
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> execute<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>그리고 Light 구상 클래스 입니다. 불을 켜고 끄는 기능이 부여 되어있네요.</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
</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> Light
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> on<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: #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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> off<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>다음은 커맨드 인터페이스를 구현하는 LightOnCommand 클래스 입니다.</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
</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> LightOnCommand implements ICommand
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> light<span style="color: #000000; font-weight: bold;">:</span>Light;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> LightOnCommand<span style="color: #000000;">&#40;</span> $light<span style="color: #000000; font-weight: bold;">:</span>Light <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.light = $light;
		<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> execute<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>
			light.on<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>생성자에서 Light 객체를 받아 인스턴스 변수로 저장하고 excute() 가 호출되면 객체 내부의 on() 메서드를 호출 하는 역할을 합니다.<br />
불을 켜고 끄려면 LightOffCommand 클래스도 있어야 겠습니다. 포스트 본문에 보여지지 않는 코드들은 가장 아래에서 다운로드 받을 수 있습니다.</p>
<p>그럼 리모컨 클래스를 살펴보겠습니다.<br />
SimpleRemoteControl 객체는 버튼 하나짜리 리모컨이라고 생각하면 됩니다. 다만 그 버튼에 여러가지 커맨드를 할당하고 해지하면서 기능을 실행 하는 것이죠.</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
</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> SimpleRemoteControl
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> slot<span style="color: #000000; font-weight: bold;">:</span>ICommand;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setCommand<span style="color: #000000;">&#40;</span> $command<span style="color: #000000; font-weight: bold;">:</span>ICommand <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>
			slot = $command
		<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> buttonWasPressed<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>
			slot.execute<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>불을 켜는 프로세스를 잠깐 살펴보면, Light 객체를 생성자로 받은 LightOnCommand 객체를 다시 SimpleRemoteControl 에서 setCommand() 메서드를 이용해 슬롯에 장전하고, buttonWasPressed() 메서드를 이용해 실행하고 있는거네요. </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
</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> remote<span style="color: #000000; font-weight: bold;">:</span>SimpleRemoteControl = <span style="color: #0033ff; font-weight: bold;">new</span> SimpleRemoteControl<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> light<span style="color: #000000; font-weight: bold;">:</span>Light = <span style="color: #0033ff; font-weight: bold;">new</span> Light<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> garageDoor<span style="color: #000000; font-weight: bold;">:</span>GarageDoor = <span style="color: #0033ff; font-weight: bold;">new</span> GarageDoor<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> lightOn<span style="color: #000000; font-weight: bold;">:</span>LightOnCommand = <span style="color: #0033ff; font-weight: bold;">new</span> LightOnCommand<span style="color: #000000;">&#40;</span> light <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> garageOpen<span style="color: #000000; font-weight: bold;">:</span>GarageDoorOpenCommand = <span style="color: #0033ff; font-weight: bold;">new</span> GarageDoorOpenCommand<span style="color: #000000;">&#40;</span> garageDoor <span style="color: #000000;">&#41;</span>;
&nbsp;
			remote.setCommand<span style="color: #000000;">&#40;</span> lightOn <span style="color: #000000;">&#41;</span>;
			remote.buttonWasPressed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//출력 : 불 켜짐</span>
			remote.setCommand<span style="color: #000000;">&#40;</span> garageOpen <span style="color: #000000;">&#41;</span>;
			remote.buttonWasPressed<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//출력 : 차고 문 열림</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>
<p>요컨대 구상객체에 직접 명령을 내리지 않고, 중간 매개체인 커맨드 객체가 대신 처리하게 하도록 명령을 캡슐화 한 것입니다. 이렇게 하면 호스트 코드에서는 어떤 기능이 리모컨에 할당되어 있는지 관계 없이 명령을 실행할 때는 커맨드 메서드인 excute() 만 호출하면 되겠죠.</p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=11" title=" downloaded 89 times" >Command (simple) Pattern Example 액션스크립트 코드 다운로드 (89)</a>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/248/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singleton Pattern 싱글턴 패턴을 액션스크립트로 컨버팅 – Head First Design Pattern</title>
		<link>http://ufx.kr/blog/246</link>
		<comments>http://ufx.kr/blog/246#comments</comments>
		<pubDate>Wed, 17 Jun 2009 18:37:50 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[getInstance]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[Singleton Pattern]]></category>
		<category><![CDATA[static]]></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=246</guid>
		<description><![CDATA[헤드퍼스트 디자인 패턴 제 5 장 싱글턴 패턴 입니다. 싱글턴 패턴은 스터디에서는 건너뛰었지만, 이 일련의 디자인 패턴을 다룬 포스트들은 모든 패턴을 한 번씩 다뤄 보는데도 나름의 의의가 있기도 하므로 짚고 넘어가 보겠습니다.
싱글턴 패턴 &#8211; 싱글턴 패턴은 해당 클래스의 인스턴스가 하나만 만들어지고, 어디서든지 그 인스턴스에 접근할 수 있도록 하기 위한 패턴입니다.
싱글턴 패턴은 객체가 두개가 되지 않도록 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-247" title="welcometo_singleton" src="http://ufx.kr/blog/wp-content/uploads/2009/06/welcometo_singleton.jpg" alt="welcometo_singleton" width="240" height="180" />헤드퍼스트 디자인 패턴 제 5 장 싱글턴 패턴 입니다. 싱글턴 패턴은 스터디에서는 건너뛰었지만, 이 일련의 디자인 패턴을 다룬 포스트들은 모든 패턴을 한 번씩 다뤄 보는데도 나름의 의의가 있기도 하므로 짚고 넘어가 보겠습니다.</p>
<p><span style="color: #808080;">싱글턴 패턴 &#8211; 싱글턴 패턴은 해당 클래스의 인스턴스가 하나만 만들어지고, 어디서든지 그 인스턴스에 접근할 수 있도록 하기 위한 패턴입니다.</span></p>
<p>싱글턴 패턴은 객체가 두개가 되지 않도록 구조화 되어있으므로 애플리케이션에서 히스토리를 관리한다던가, 모달리스 다이알로그 박스를 띄운다던가, 사용자 설정이나 포커스 관리 같이 객체가 하나 이상이 되면 안되는 부분에 사용하게 됩니다.</p>
<p>본문 예제에서는 밀크 초콜렛을 끓여 만드는 초콜렛 보일러를 예로 들고 있습니다. 초콜렛 보일러는 초콜렛 공장의 유일한 보일러 이므로 객체 생성도 한번밖에 할 수 없도록 해야합니다.<br />
보일러는 재료 넣기 &#8211;> 끓이기 &#8211;> 추출의 3 단계를 거쳐 초콜렛을 관리하게 되는데 이전 단계가 완수되지 않으면 다음 단계로 넘어갈 수 없도록 조건문이 걸려 있습니다.</p>
<p><span id="more-246"></span>그럼 싱글턴 패턴이 구현된 클래스를 보도록 하겠습니다. 싱글턴 패턴은 호스트코드를 제외하면 하나의 클래스로 이루어져 있습니다.</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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</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> ChocolateBoiler
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> empty<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> boiled<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #6699cc; font-weight: bold;">var</span> uniqueInstance<span style="color: #000000; font-weight: bold;">:</span>ChocolateBoiler;
		<span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #6699cc; font-weight: bold;">var</span> allowCreateSingleton<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>; 
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ChocolateBoiler<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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: #000000; font-weight: bold;">!</span>allowCreateSingleton <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;Singleton 클래스 이므로 생성자를 직접 호출할 수 없습니다.&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>
				empty = <span style="color: #0033ff; font-weight: bold;">true</span>;
				boiled = <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>ChocolateBoiler
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> uniqueInstance == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				allowCreateSingleton = <span style="color: #0033ff; font-weight: bold;">true</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>
				uniqueInstance = <span style="color: #0033ff; font-weight: bold;">new</span> ChocolateBoiler<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
				allowCreateSingleton = <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</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: #0033ff; font-weight: bold;">return</span> uniqueInstance;
		<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> fill<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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #004993;">isEmpty</span><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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot; - 우유와 초콜렛을 보일러에 투입&quot;</span> <span style="color: #000000;">&#41;</span>
				empty = <span style="color: #0033ff; font-weight: bold;">false</span>;
				boiled = <span style="color: #0033ff; font-weight: bold;">false</span>;
			<span style="color: #000000;">&#125;</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> drain<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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #004993;">isEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> isBoiled<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot; - 끓인 재료를 다음 단계로 넘기기 위해 추출&quot;</span> <span style="color: #000000;">&#41;</span>
				empty = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</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> boil<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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #004993;">isEmpty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">!</span>isBoiled<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot; - 재료를 끓임&quot;</span> <span style="color: #000000;">&#41;</span>
				boiled = <span style="color: #0033ff; font-weight: bold;">true</span>;
			<span style="color: #000000;">&#125;</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> <span style="color: #004993;">isEmpty</span><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> empty;
		<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> isBoiled<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> boiled;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>헤드퍼스트 디자인 패턴의 예제는 JAVA 코드이므로 생성자 메서드를 private 로 설정해서 간단하게 싱글턴을 구현하고 있습니다만, 액션스크립트 3.0은 생성자에 private 를 지원하지 않기 때문에 몇가지 장치가 추가로 필요합니다.</p>
<p>생성자에서 allowCreateSingleton 변수가 true 가 아니면 Error 를 던지게 조건문을 걸어 놓고 getInstance() 메서드 안에서만 allowCreateSingleton 를 관리하는 방식입니다. getInstance() 를 통해서 객체를 생성하기 전 allowCreateSingleton 을 true 로 설정했다가 객체를 new 한 이후에는 다시 false 로 설정하는 것이죠.</p>
<p>이렇게 되면 호스트 코드에서 new ChocolateBoiler() 를 호출해도 Error 를 던지게 되므로 public 으로 접근제한자가 설정되어 있어도 객체는 생성되지 않고, getInstance() 를 호출해서 얻어지는 객체도 조건문을 거쳐 항상 같은 객체를 리턴 받도록 되어 있습니다.</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
</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>;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ChocolateController 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> ChocolateController<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> boiler<span style="color: #000000; font-weight: bold;">:</span>ChocolateBoiler = ChocolateBoiler.getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boiler.fill<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boiler.boil<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boiler.drain<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> boiler2<span style="color: #000000; font-weight: bold;">:</span>ChocolateBoiler = ChocolateBoiler.getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			boiler2.boil<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #009900;">//var boiler3:ChocolateBoiler = new ChocolateBoiler()</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>
<p><span style="color: #808080;">초콜렛 보일러 인스턴스 생성<br />
초콜렛 보일러 인스턴스 반환<br />
- 우유와 초콜렛을 보일러에 투입<br />
- 재료를 끓임<br />
- 끓인 재료를 다음 단계로 넘김<br />
초콜렛 보일러 인스턴스 반환</span></p>
<p>이렇게 boiler2 객체를 만들어도 실제 객체는 같은 객체를 가리키게 되므로, boiler.boil() 이나 boiler2.boil() 이나 결국 같은 객체의 메서드를 실행하게 됩니다. 그래서 초콜렛 제작 순서에 따르지 않은 boiler2.boil() 는 무시되겠죠.</p>
<p>또한 현재 위의 코드에서 주석 처리 되어 있는 new ChocolateBoiler() 을 호출해보면 생성자의 Error 를 던지게 됩니다.</p>
<p><span style="color: #008080;"><strong>싱글턴을 구현하는 또 다른 방법들</strong></span></p>
<p>위의 방법 이외에도 싱글턴은 여러가지 방법으로 구현될 수 있습니다. 에이콘 출판사에서 번역 출간된 &#8220;액션스크립트 3.0 디자인 패턴&#8221; 에서는 외부 클래스를 이용한 싱글턴 예제를 보여주고 있습니다.</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
</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> Singleton
	<span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _instance<span style="color: #000000; font-weight: bold;">:</span>Singleton;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Singleton<span style="color: #000000;">&#40;</span> singletonEnforcer<span style="color: #000000; font-weight: bold;">:</span>SingletonEnforcer <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Singleton
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> Singleton._instance == <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				Singleton._instance = <span style="color: #0033ff; font-weight: bold;">new</span> Singleton<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> SingletonEnforcer<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: #0033ff; font-weight: bold;">return</span> Singleton._instance;
		<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> doSometing<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SOMETHING !&quot;</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #9900cc; font-weight: bold;">class</span> SingletonEnforcer <span style="color: #000000;">&#123;</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
</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> Singleton
	<span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _instance<span style="color: #000000; font-weight: bold;">:</span>Singleton = <span style="color: #0033ff; font-weight: bold;">new</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Singleton<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> _instance <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;Singleton 클래스 이므로 생성자를 직접 호출할 수 없습니다.&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Singleton
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> _instance;
		<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> doSometing<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;SOMETHING !&quot;</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>싱글턴으로 만들 객체가 어차피 애플리케이션에 반드시 필요한 객체일 경우 이런 방식으로 초기에 메모리에 할당하여 만들어 버리는 것입니다. 좀더 단순하므로 에러가 발생할 확률도 적다고 할 수 있겠습니다.</p>
<p>* 본 싱글턴 패턴 에제 코드는 위의 코드가 전부이므로 따로 다운로드를 걸지 않았습니다.</p>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/246/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Observer Pattern 옵저버 패턴 &#8211; 이벤트 디스패처를 이용해 구현</title>
		<link>http://ufx.kr/blog/233</link>
		<comments>http://ufx.kr/blog/233#comments</comments>
		<pubDate>Sun, 07 Jun 2009 19:59:17 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[CustomEvent]]></category>
		<category><![CDATA[dispatcher]]></category>
		<category><![CDATA[EventDispatcher]]></category>
		<category><![CDATA[Observer]]></category>
		<category><![CDATA[subject]]></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=233</guid>
		<description><![CDATA[지난 옵저버 패턴과 커스텀 이벤트에 대한 포스트에서 AS3.0 의 이벤트 시스템이 옵저버 패턴과 밀접한 관련이 있다고 하였는데요, 이번에는 trace 만 찍지 않고, 실제 액션스크립트로 비주얼 하게 구현을 해 보도록 하겠습니다.
요건은 다음과 같습니다.

AS3.0 의 이벤트 디스패처를 이용할 것.
화면에 그래픽 객체가 각자 움직이다가 마우스 클릭을 하면 한 지점으로 모인다.
다시 한번 클릭하면 그래픽 객체들이 다시 각자 움직인다.


옵저버 패턴이므로 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-235" title="bubble" src="http://ufx.kr/blog/wp-content/uploads/2009/06/bubble.jpg" alt="bubble" width="240" height="160" />지난 <a href="http://ufx.kr/blog/170">옵저버 패턴</a>과 <a href="http://ufx.kr/blog/228">커스텀 이벤트에 대한 포스트</a>에서 AS3.0 의 이벤트 시스템이 옵저버 패턴과 밀접한 관련이 있다고 하였는데요, 이번에는 trace 만 찍지 않고, 실제 액션스크립트로 비주얼 하게 구현을 해 보도록 하겠습니다.</p>
<p><span style="color: #008080;">요건은 다음과 같습니다.</span></p>
<ul>
<li>AS3.0 의 이벤트 디스패처를 이용할 것.</li>
<li>화면에 그래픽 객체가 각자 움직이다가 마우스 클릭을 하면 한 지점으로 모인다.</li>
<li>다시 한번 클릭하면 그래픽 객체들이 다시 각자 움직인다.</li>
</ul>
<p><span id="more-233"></span></p>
<p><span style="color: #008080;">옵저버 패턴이므로 등장인물 3인이 필요하겠습니다.</span></p>
<ol>
<li><strong>주제 객체</strong><br />
이벤트 디스패처 (EventDispatcher) 가 주제 역할을 하게 됩니다. 이벤트 시스템의 중심에서 이벤트를 전파하는 역할을 하고 있죠.</li>
<li><strong>옵저버 객체</strong><br />
여기서는 화면에서 움직이는 그래픽 객체가 옵저버 입니다. 옵저버들은 평소에는 다른 기능을 하다가<sup>[<a href="http://ufx.kr/blog/233#footnote_0_233" id="identifier_0_233" class="footnote-link footnote-identifier-link" title="화면에서 각자 움직이다가">01</a>]</sup> 이벤트가 발생하면 지정된 곳으로 모이게 됩니다.</li>
<li><strong>인포 객체</strong><br />
이벤트 디스패처가 옵저버 들에게 전달하는 Event 내부에 같이 실어 보내는 데이터가 인포 객체 입니다.</li>
</ol>
<p><span style="color: #008080;">화면과 기능을 어떻게 구현할 것인가 좀더 구체화 시킬 필요가 있을 것 같습니다.</span></p>
<ul>
<li>옵저버 객체들이 움직이는 방식으로는 Timer 를 이용해 2초 주기의 TweenMax 의 베지어 트윈을 이용해 랜덤한 위치로 결정.</li>
<li>주제가 옵저버에게 전달하는 정보로는 MouseEvent.MOUSE_DOWN 이 일어난 mouseX 와 mouseY 좌표값으로하여 그 정보에 따라 옵저버 객체들이 모이도록 한다.</li>
<li>옵저버 객체들이 주제에 등록되고 등록해제 되는 것을 옵저버 객체에 대한 MouseEvent.MOUSE_DOWN 으로 구현한다.</li>
<li>최초 도큐먼트 클래스에서 모두 옵저버 등록을 하고, 옵저버 객체에 MouseEvent.MOUSE_DOWN 이 한번 일어나면 옵저버 리스트에서 빠지고 (동시에 시각적으로 차이를 내기 위해 alpha = 0.5 해줌) 다시한번 MouseEvent.MOUSE_DOWN 이 일어나면 다시 옵저버 리스트로 들어가도록 한다.</li>
</ul>
<p>그 외에 옵저버와 직접적인 기능과는 관계 없는 내용으로, MouseEvent.ROLL_OVER 가 일어난 객체를 가장 상위로 올리도록 한 후 상하 관계를 쉽게 알 수 있도록 옵저버 객체 중앙에 십자 표시를 하였고, 주제 객체가 정보를 새로 생성했는지 아닌지 시각적으로 구분하기 위해 정보가 생성되면<sup>[<a href="http://ufx.kr/blog/233#footnote_1_233" id="identifier_1_233" class="footnote-link footnote-identifier-link" title="즉, 배경에 MouseEvent.MOUSE_DOWN 이 일어나면">02</a>]</sup> 배경 이미지의 색상을 바꾸는 내용이 추가로 들어갔습니다.</p>
<p>그럼 이제 주제 인터페이스를 보도록 하겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
</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 ISubject
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> addObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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: #339966; font-weight: bold;">function</span> removeObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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: #339966; font-weight: bold;">function</span> notifyObserver<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>지난번 옵저버 패턴과 완전히 동일합니다.</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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</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.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Subject implements ISubject 
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> dispatcher<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">EventDispatcher</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _arrObserver<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _infoObject<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Subject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//ISubject 를 구현 - 옵저버 등록</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> addObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			dispatcher.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> ObserverEvent.NOTIFY, $o.update <span style="color: #000000;">&#41;</span>;
			addObject<span style="color: #000000;">&#40;</span> $o <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> addObject<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			arrObserver.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> $o <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//ISubject 를 구현 - 옵저버 제거</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> removeObserver<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			dispatcher.<span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> ObserverEvent.NOTIFY, $o.update <span style="color: #000000;">&#41;</span>;
			removeObject<span style="color: #000000;">&#40;</span> $o <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> removeObject<span style="color: #000000;">&#40;</span> $o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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> <span style="color: #004993;">index</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = arrObserver.<span style="color: #004993;">indexOf</span><span style="color: #000000;">&#40;</span> $o, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
			arrObserver.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">index</span>, <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//ISubject 를 구현 - 이벤트 발생 --&gt; 정보 전달</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> notifyObserver<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>
			dispatcher.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> ObserverEvent<span style="color: #000000;">&#40;</span> ObserverEvent.NOTIFY, <span style="color: #0033ff; font-weight: bold;">this</span>.infoObject <span style="color: #000000;">&#41;</span> <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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setClick<span style="color: #000000;">&#40;</span> $x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</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>
			makeInfoObject<span style="color: #000000;">&#40;</span> $x, $y <span style="color: #000000;">&#41;</span>
			notifyObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//정보를 Object 로 만들어 변수에 저장</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> makeInfoObject<span style="color: #000000;">&#40;</span> $x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, $y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</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: #0033ff; font-weight: bold;">return</span> _infoObject = <span style="color: #000000;">&#123;</span> infoX <span style="color: #000000; font-weight: bold;">:</span> $x, infoY <span style="color: #000000; font-weight: bold;">:</span> $y <span style="color: #000000;">&#125;</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> <span style="color: #0033ff; font-weight: bold;">get</span> infoObject<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: #0033ff; font-weight: bold;">return</span> _infoObject; <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> <span style="color: #0033ff; font-weight: bold;">get</span> arrObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _arrObserver; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>EventDispatcher 객체를 생성했고 구상 객체 목록을 관리할 수 있도록 배열을 하나 만들었습니다.<br />
이벤트를 통해 전달할 정보를 저장할 Object 객체도 하나 만들었네요.<br />
ObserverEvent.NOTIFY 가 발생하면 dispatcher가 각각의 옵저버들이 걸어놓은 $o.update 메서드를 실행 하는 것입니다.</p>
<p>디스패처용 커스텀 이벤트 입니다. <a href="http://ufx.kr/blog/228">커스텀 이벤트에 대한 포스트</a>에서도 있듯이 커스텀 이벤트의 형태는 변수명이나 상수명을 제외하고는 다른 커스텀 이벤트와 동일합니다.</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
</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.events</span>.<span style="color: #004993;">Event</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ObserverEvent extends <span style="color: #004993;">Event</span>
	<span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> const NOTIFY<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;notify&quot;</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _info<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ObserverEvent<span style="color: #000000;">&#40;</span> $type<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>, $info<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, $bubble<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span>, $cancelable<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">false</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span> $type, $bubble, $cancelable <span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">this</span>._info = $info;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> CustomEvent<span style="color: #000000;">&#40;</span> <span style="color: #004993;">type</span>, _info, <span style="color: #004993;">bubbles</span>, <span style="color: #004993;">cancelable</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> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">info</span><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: #0033ff; font-weight: bold;">return</span> _info;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>옵저버 쪽으로 넘어가 보겠습니다. 인터페이스에는 dispatcher가 호출할 update() 메서드 이외엔 없군요.</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;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">public</span> interface IObserver
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Event</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;">&#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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
</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;">Shape</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">TimerEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextField</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextFormat</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">Timer</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> gs.TweenMax
	<span style="color: #0033ff; font-weight: bold;">import</span> gs.easing.<span style="color: #000000; font-weight: bold;">*</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Item extends <span style="color: #004993;">Sprite</span> implements IObserver
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> txtId<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextField</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _id<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _x<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> _y<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">stageWidth</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">stageHeight</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">timer</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Timer</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Timer</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">2000</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> isInfo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Item <span style="color: #000000;">&#40;</span> $size<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000;">&#41;</span> 
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> circle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span> 0x000000, <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawCircle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, $size <span style="color: #000000;">&#41;</span>
			circle.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> circle <span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> hLine<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			hLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, 0xcc3300 <span style="color: #000000;">&#41;</span>;
			hLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">60</span>, <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>;
			hLine.<span style="color: #004993;">x</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> hLine <span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> vLine<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Shape</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Shape</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			vLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">1</span>, 0xcc3300 <span style="color: #000000;">&#41;</span>;
			vLine.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">60</span> <span style="color: #000000;">&#41;</span>;
			vLine.<span style="color: #004993;">y</span> = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">30</span>;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> vLine <span style="color: #000000;">&#41;</span>
&nbsp;
			txtId = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextField</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> format<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextFormat</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextFormat</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			format.<span style="color: #004993;">font</span> = <span style="color: #990000;">&quot;Verdana&quot;</span>;
			format.<span style="color: #004993;">color</span> = 0xFFFFFF;
			format.<span style="color: #004993;">size</span> = <span style="color: #000000; font-weight:bold;">20</span>;
			txtId.<span style="color: #004993;">defaultTextFormat</span> = format
			txtId.<span style="color: #004993;">selectable</span> = <span style="color: #0033ff; font-weight: bold;">false</span>;
			txtId.<span style="color: #004993;">autoSize</span> = <span style="color: #990000;">&quot;left&quot;</span>;
			txtId.<span style="color: #004993;">backgroundColor</span> = 0x000000;
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>txtId<span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED_TO_STAGE</span>, initStage <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">timer</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">TimerEvent</span>.<span style="color: #004993;">TIMER</span>, onTimeHandler <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">timer</span>.<span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span> $e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</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> event<span style="color: #000000; font-weight: bold;">:</span>ObserverEvent = $e <span style="color: #0033ff; font-weight: bold;">as</span> ObserverEvent;
			<span style="color: #0033ff; font-weight: bold;">this</span>._x = event.<span style="color: #004993;">info</span>.infoX;
			<span style="color: #0033ff; font-weight: bold;">this</span>._y = event.<span style="color: #004993;">info</span>.infoY;
			<span style="color: #009900;">// 정보로 들어온 값이 undefined 인지 아닌지 확인하여 isInfo에 Boolean 으로 저장</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.isInfo = <span style="color: #000000;">&#40;</span> _x <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> _y <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?</span> <span style="color: #0033ff; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #0033ff; font-weight: bold;">false</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onTimeHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TimerEvent</span> = <span style="color: #0033ff; font-weight: bold;">null</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> isInfo <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				concentration<span style="color: #000000;">&#40;</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>
				deconcentration<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> initStage<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</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: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED_TO_STAGE</span>, initStage <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">stageWidth</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span>;
			<span style="color: #004993;">stageHeight</span> = <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</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> <span style="color: #0033ff; font-weight: bold;">set</span> id <span style="color: #000000;">&#40;</span> $value<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>
			_id = $value;
			txtId.<span style="color: #004993;">text</span> = _id.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</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> <span style="color: #0033ff; font-weight: bold;">get</span> id<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> _id; <span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//Event 의 정보 값으로 모이기</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> concentration<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>
			TweenMax.to<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000;">&#123;</span> <span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span>_x, <span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span>_y , ease<span style="color: #000000; font-weight: bold;">:</span>Quart.easeInOut <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//Event 정보 값이 undefined 일 경우 랜덤으로 움직이기</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> deconcentration<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>
			TweenMax.to<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000;">&#123;</span> <span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageWidth</span><span style="color: #000000;">&#41;</span>, <span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageHeight</span><span style="color: #000000;">&#41;</span>, 
					bezier<span style="color: #000000; font-weight: bold;">:</span><span style="color: #000000;">&#91;</span> <span style="color: #000000;">&#123;</span> <span style="color: #004993;">x</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageWidth</span><span style="color: #000000;">&#41;</span>, <span style="color: #004993;">y</span><span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Math</span>.<span style="color: #004993;">round</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">random</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">stageHeight</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#93;</span>, 
					ease<span style="color: #000000; font-weight: bold;">:</span>Quart.easeInOut <span style="color: #000000;">&#125;</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>구상 클래스의 길이가 약간 길지만 대부분 생성자에서 Shape로 그림을 그리는 부분과, 객체 본연의 할일을 부분<sup>[<a href="http://ufx.kr/blog/233#footnote_2_233" id="identifier_2_233" class="footnote-link footnote-identifier-link" title="랜덤으로 2초마다 움직이게 하는 일">03</a>]</sup> 들이고, 옵저버로서의 내용은 인터페이스 메서드인 update() 메서드를 구상하는 내용 뿐 입니다.</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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
</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>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">MouseEvent</span>;
	<span style="color: #0033ff; font-weight: bold;">import</span> gs.TweenLite;
&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;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> subject<span style="color: #000000; font-weight: bold;">:</span>Subject;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> isInfo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> clickedItem<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> bg<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span>;
&nbsp;
		<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>
			subject = <span style="color: #0033ff; font-weight: bold;">new</span> Subject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			createBG<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			createItem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> createBG<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>
			bg = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x555555, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">drawRoundRect</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageWidth</span>, <span style="color: #004993;">stage</span>.<span style="color: #004993;">stageHeight</span>, <span style="color: #000000; font-weight:bold;">50</span>, <span style="color: #000000; font-weight:bold;">50</span> <span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> bg <span style="color: #000000;">&#41;</span>
			bg.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, bgClickHandler<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> createItem<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: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">10</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span>Item = <span style="color: #0033ff; font-weight: bold;">new</span> Item<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
				item.id = i;
				item.<span style="color: #004993;">buttonMode</span> = <span style="color: #0033ff; font-weight: bold;">true</span>;
				item.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, itemClickHandler <span style="color: #000000;">&#41;</span>;
				item.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_OVER</span>, itemRollOverHandler <span style="color: #000000;">&#41;</span>;
				<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span> item <span style="color: #000000;">&#41;</span>;
&nbsp;
				subject.addObserver<span style="color: #000000;">&#40;</span> item <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//옵저버 객체에 마우스 이벤트가 일어나면 handleItem() 으로 객체를 보냄</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> itemClickHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</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> <span style="color: #004993;">target</span><span style="color: #000000; font-weight: bold;">:</span>Item = e.<span style="color: #004993;">currentTarget</span> <span style="color: #0033ff; font-weight: bold;">as</span> Item
			handleItem<span style="color: #000000;">&#40;</span> <span style="color: #004993;">target</span> <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> handleItem<span style="color: #000000;">&#40;</span> $item<span style="color: #000000; font-weight: bold;">:</span>Item <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> isFind<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #0033ff; font-weight: bold;">in</span> subject.arrObserver <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> item === $item <span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					subject.removeObserver<span style="color: #000000;">&#40;</span> $item <span style="color: #000000;">&#41;</span>;
					$item.<span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">0.5</span>;
					isFind = <span style="color: #0033ff; font-weight: bold;">true</span>;
					<span style="color: #0033ff; font-weight: bold;">break</span>
				<span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight: bold;">!</span>isFind <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				subject.addObserver<span style="color: #000000;">&#40;</span> $item <span style="color: #000000;">&#41;</span>
				$item.<span style="color: #004993;">alpha</span> = <span style="color: #000000; font-weight:bold;">1</span>;
			<span style="color: #000000;">&#125;</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> itemRollOverHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</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: #004993;">setChildIndex</span><span style="color: #000000;">&#40;</span> e.<span style="color: #004993;">currentTarget</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Sprite</span>, <span style="color: #004993;">numChildren</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span> <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> bgClickHandler<span style="color: #000000;">&#40;</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</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>
			isInfo = <span style="color: #000000; font-weight: bold;">!</span>isInfo;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> isInfo <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				subject.setClick<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">undefined</span>, <span style="color: #0033ff; font-weight: bold;">undefined</span> <span style="color: #000000;">&#41;</span>;
				TweenLite.to<span style="color: #000000;">&#40;</span> bg, <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000;">&#123;</span> tint<span style="color: #000000; font-weight: bold;">:</span>0x555555 <span style="color: #000000;">&#125;</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>
				subject.setClick<span style="color: #000000;">&#40;</span> <span style="color: #004993;">mouseX</span>, <span style="color: #004993;">mouseY</span> <span style="color: #000000;">&#41;</span>;
				TweenLite.to<span style="color: #000000;">&#40;</span> bg, <span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000;">&#123;</span> tint<span style="color: #000000; font-weight: bold;">:</span>0x8D2545 <span style="color: #000000;">&#125;</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>각 메서드에 주석을 달아 놓았으니 참고하시면 되겠습니다.<br />
위의 도큐먼트 클래스를 컴파일한 결과입니다.<br />
<object width="600" height="400" data="http://ufx.kr/blog/wp-content/uploads/2009/06/observerdispatchereventexample.swf" type="application/x-shockwave-flash"><param name="wmode" value="transparent" /><param name="src" value="http://ufx.kr/blog/wp-content/uploads/2009/06/observerdispatchereventexample.swf" /></object></p>
<p>배경을 한번 클릭하면 이벤트가 발생하고, 다시한번 클릭하면 객체들은 다시 원래의 할일을 합니다.<br />
객체를 클릭하면 옵저버 리스트에서 빠지고, 다시한번 클릭하면 옵저버에 등록됩니다.</p>
<p>이벤트 모델을 이용한 옵저버 패턴은, 옵저버 객체를 임의로 빼거나 추가해도 디스패처로가 다른 옵저버들에게 전달하는 이벤트에는 아무런 문제가 발생하지 않게되죠.<sup>[<a href="http://ufx.kr/blog/233#footnote_3_233" id="identifier_3_233" class="footnote-link footnote-identifier-link" title="객체간 약한 결합 : Loose Coupling">04</a>]</sup> </p>
<p>이렇게 디스패처에 의해 발생된 이벤트에 정보를 실어 옵저버 객체에 전달하고 이벤트 리스너에 등록된 함수를 이용해 객체 내부를 업데이트 하여 정보를 사용하는 방법까지 표현하였습니다.</p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=9" title=" downloaded 91 times" >Observer Pattern Dispatcher Event Example 액션스크립트 코드 다운로드 (91)</a>
<ol class="footnotes"><li id="footnote_0_233" class="footnote">화면에서 각자 움직이다가</li><li id="footnote_1_233" class="footnote">즉, 배경에 MouseEvent.MOUSE_DOWN 이 일어나면</li><li id="footnote_2_233" class="footnote">랜덤으로 2초마다 움직이게 하는 일</li><li id="footnote_3_233" class="footnote">객체간 약한 결합 : Loose Coupling</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/233/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Abstract Factory Pattern 추상 팩토리 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/199</link>
		<comments>http://ufx.kr/blog/199#comments</comments>
		<pubDate>Fri, 29 May 2009 23:03:00 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Factory Pattern]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[Simple Factory]]></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=199</guid>
		<description><![CDATA[헤드퍼스트 디자인 패턴의 제 4 장의 내용은 팩토리 패턴 입니다. 팩토리 패턴에 관련한 포스트는 아래 세 개의 목록이 계속 연결되는 내용이므로 차례대로 읽어나가면 이해에 도움이 될 수 있을 것 입니다.

Simple Factory 심플 팩토리 : 팩토리 패턴의 워밍업
Factory Method Pattern 팩토리 메서드 패턴을 액션스크립트로 컨버팅
Abstract Factory Pattern 추상 팩토리 패턴을 액션스크립트로 컨버팅 (현재 글)

우선 추상 팩토리 [...]]]></description>
			<content:encoded><![CDATA[<p>헤드퍼스트 디자인 패턴의 제 4 장의 내용은 팩토리 패턴 입니다. 팩토리 패턴에 관련한 포스트는 아래 세 개의 목록이 계속 연결되는 내용이므로 차례대로 읽어나가면 이해에 도움이 될 수 있을 것 입니다.</p>
<ol>
<li><a href="http://ufx.kr/blog/191">Simple Factory 심플 팩토리 : 팩토리 패턴의 워밍업</a></li>
<li><a href="http://ufx.kr/blog/196">Factory Method Pattern 팩토리 메서드 패턴을 액션스크립트로 컨버팅</a></li>
<li>Abstract Factory Pattern 추상 팩토리 패턴을 액션스크립트로 컨버팅 (현재 글)</li>
</ol>
<p><img class="alignleft size-full wp-image-200" title="pizza_factory" src="http://ufx.kr/blog/wp-content/uploads/2009/05/pizza_factory.jpg" alt="pizza_factory" width="240" height="160" />우선 추상 팩토리 패턴의 정의를 보도록 하죠.</p>
<p><span style="color: #808080;">&#8220;추상 팩토리 패턴 &#8211; 추상 팩토리 패턴에서는 인터페이스를 이용하여 서로 연관된, 또는 의존하는 객체를 구상 클래스를 지정하지 않고도 생성할 수 있습니다.&#8221;</span></p>
<p>앞서 다룬 팩토리 메서드 패턴에서도 하위형으로 NYPizzaStore 구상 객체를 만들고 상위형이자 추상층인 PizzaStore 로 업 캐스팅 하는 부분이 있습니다만,<sup>[<a href="http://ufx.kr/blog/199#footnote_0_199" id="identifier_0_199" class="footnote-link footnote-identifier-link" title="이 부분이 팩토리 메서드와 추상 팩토리의 경계를 모호하게 만드는 것 같습니다.">01</a>]</sup> 팩토리 메서드 패턴의 핵심은 그 부분에 있는 것이 아니라, 객체 생성을 서브클래스에 캡슐화 시킨다는 훨씬 중요한 내용에 있습니다.</p>
<p><span id="more-199"></span>그러나 이 포스트에서 다루게 되는 추상 팩토리 패턴은 본격적으로 인터페이스와 업 캐스팅을 통해서 객체 자동생성을 구현하게 되고, 그것을 통해 생산성과 직접적인 연관을 가지게 됩니다. 결국 높은 객체 생산 효율을 추구하는 디자인 패턴인 것이죠.</p>
<p>이어지는 피자가게 프랜차이즈 스토리에 대한 설명을 약간 해야 할 것 같습니다. 객체마을 피자가게가 인기가 올라가자 뉴욕과 시카고에 분점을 내기로 한 것까지가 지난번 팩토리메서드의 이야기였습니다. 그런데 일부 분점에서는 자잘한 재료를 더 싼 재료로 바꿔서 원가를 줄이고 마진을 올리고 있다는 정보를 들어왔습니다. 이런 현상이 심해지면 객체마을 피자 브랜드의 품질에 타격이 올 수 있기 때문에 각 분점에서 임의로 재료를 공급해서 조달하는 것을 막고자 하는 것이 이번 추상 팩토리 패턴의 목적 입니다.</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
</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 IPizzaIngredientFactory
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> createDough<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IDough;
		<span style="color: #339966; font-weight: bold;">function</span> createSauce<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>ISauce;
		<span style="color: #339966; font-weight: bold;">function</span> createCheese<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>ICheese;
		<span style="color: #339966; font-weight: bold;">function</span> createVeggies<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>;
		<span style="color: #339966; font-weight: bold;">function</span> createPepperoni<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IPepperoni;
		<span style="color: #339966; font-weight: bold;">function</span> createClam<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IClams;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>피자에 들어가는 각각의 재료를 모두 인터페이스 메서드로 만들었습니다. 이전 버전과는 달리 원재료가 어떻게 공급되는지 표현하기 위해 뭉뚱그린 표현인 토핑(배열)을 없애고 야채(배열), 페퍼로니, 치즈, 조개로 좀더 세분화하였습니다.</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
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> NYPizzaIngredientFactory implements IPizzaIngredientFactory
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> createDough<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IDough
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> ThinCrustDough<span style="color: #000000;">&#40;</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> createSauce<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>ISauce
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> MarinaraSauce<span style="color: #000000;">&#40;</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> createCheese<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>ICheese
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> ReggianoCheese<span style="color: #000000;">&#40;</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> createVeggies<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> veggies<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span> <span style="color: #0033ff; font-weight: bold;">new</span> Garlic<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> Onion<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> Mushroom<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0033ff; font-weight: bold;">new</span> RedPepper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#93;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> veggies;
		<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> createPepperoni<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IPepperoni
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> SlicedPepperoni<span style="color: #000000;">&#40;</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> createClam<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IClams
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">new</span> FreshClams<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>뉴욕 스타일 피자에 맞는 각 재료들을 모두 구현하고 있습니다. 어떤 피자 주문이 들어오더라도 일단 뉴욕 스타일 피자라면 재료공급이 가능한거죠.<sup>[<a href="http://ufx.kr/blog/199#footnote_1_199" id="identifier_1_199" class="footnote-link footnote-identifier-link" title="피자 종류에 따라 사용하지 않는 재료도 일단은 모두 구현을 해 놓았다는 의미 입니다.">02</a>]</sup></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
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> NYPizzaStore extends PizzaStore
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> createPizza<span style="color: #000000;">&#40;</span> $item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Pizza
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = <span style="color: #0033ff; font-weight: bold;">null</span>;
			<span style="color: #009900;">//구상객체로 생성해서 상위형으로 캐스팅합니다. 이게 바로 핵심이죠.</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> ingredientFactory<span style="color: #000000; font-weight: bold;">:</span>IPizzaIngredientFactory = <span style="color: #0033ff; font-weight: bold;">new</span> NYPizzaIngredientFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;cheese&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">//이 클래스에서 사용하는 재료공장은 위에서 이미 정해졌죠.</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> CheesePizza<span style="color: #000000;">&#40;</span> ingredientFactory <span style="color: #000000;">&#41;</span>
				<span style="color: #009900;">//대신 이름은 여기서 달아줘야 합니다.</span>
				pizza.<span style="color: #004993;">setName</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;pepperoni&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> PepperoniPizza<span style="color: #000000;">&#40;</span> ingredientFactory <span style="color: #000000;">&#41;</span>
				pizza.<span style="color: #004993;">setName</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;clam&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> ClamPizza<span style="color: #000000;">&#40;</span> ingredientFactory <span style="color: #000000;">&#41;</span>
				pizza.<span style="color: #004993;">setName</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;veggie&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> VeggiePizza<span style="color: #000000;">&#40;</span> ingredientFactory <span style="color: #000000;">&#41;</span>
				pizza.<span style="color: #004993;">setName</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>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> pizza;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>NYPizzaStore 에 재료를 공급하는 공장을 NYPizzaIngredientFactory 으로 인스턴스변수를 지정했습니다. 어떤 재료공장의 재료를 사용해야하는지 알고 있으므로 CheesePizza를 만들기만 하면 레지오노 치즈와 씬 크러스트 도우를 사용하게 되는겁니다.<br />
팩토리 메서드 패턴에서의 이 부분을 기억하시나요? new NYStyleCheesePizza() 로 치즈피자의 스타일을 지정한 구상객체를 만들었죠. 왜냐하면, 팩토리 메서드때에는 재료를 공급하는 시스템이 없었으니까 어떤 특정한 치즈를 사용하라 라고 지정해 줘야만 했기 때문이죠.</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
</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> CheesePizza extends Pizza
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> ingredientFactory<span style="color: #000000; font-weight: bold;">:</span>IPizzaIngredientFactory;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CheesePizza<span style="color: #000000;">&#40;</span> $ingredientFactory<span style="color: #000000; font-weight: bold;">:</span>IPizzaIngredientFactory <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.ingredientFactory = $ingredientFactory;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> prepare<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문접수 : &quot;</span> , <span style="color: #004993;">name</span> <span style="color: #000000;">&#41;</span>;
			dough = ingredientFactory.createDough<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			sauce = ingredientFactory.createSauce<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			cheese = ingredientFactory.createCheese<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: #004993;">toString</span><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></pre></td></tr></table></div>

<p>시카고 분점이나 뉴욕 분점이나 치즈피자에 들어가는 재료의 종류는 동일합니다.<sup>[<a href="http://ufx.kr/blog/199#footnote_2_199" id="identifier_2_199" class="footnote-link footnote-identifier-link" title="도우, 치즈, 소스의 종류가 틀리지만 어쨌건 도우가 들어가고, 치즈도 들어가고, 소스도 들어간다는 사실 자체가 동일하다는 의미입니다.">03</a>]</sup> 그러므로 피자를 만드는 방법은 동일하지만,<sup>[<a href="http://ufx.kr/blog/199#footnote_3_199" id="identifier_3_199" class="footnote-link footnote-identifier-link" title="구체적으로는 제작순서가 동일하다 라고 할 수 있겠습니다.">04</a>]</sup> 이 CheesePizza 클래스를 NYPizzaStore 에서 사용하면 NYPizzaIngredientFactory 의 치즈를 사용하게 되므로 ReggianoCheese 객체를 사용하게 되고, ChicagoPizzaStore 에서 사용하면 ChicagoPizzaIngredientFactory 의 재료를 사용하게 되어 MozzarellaCheese 객체를 사용하게 되는 것입니다.</p>
<p>Pizza 클래스는 팩토리 메서드 패턴에서도 추상층의 역할을 하긴 했지만, 이번에는 추상층일 뿐만 아니라 정말로 추상 메서드를 사용합니다. 위의 CheesePizza 클래스에서 Pizza 클래스를 확장하고 있고 prepare() 메서드를 override 하고 있는것을 볼 수 있습니다.</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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
</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> Pizza
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</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;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> dough<span style="color: #000000; font-weight: bold;">:</span>IDough;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> sauce<span style="color: #000000; font-weight: bold;">:</span>ISauce;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> veggies<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> cheese<span style="color: #000000; font-weight: bold;">:</span>ICheese;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> pepperoni<span style="color: #000000; font-weight: bold;">:</span>IPepperoni;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> clam<span style="color: #000000; font-weight: bold;">:</span>IClams;
&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> <span style="color: #004993;">setName</span><span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</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: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">name</span> = $name
		<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> prepare<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: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;prepare() 추상 메서드&quot;</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> bake<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 굽습니다 :&quot;</span>, <span style="color: #004993;">name</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> cut<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 자릅니다 :&quot;</span>, <span style="color: #004993;">name</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> box<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 포장합니다 :&quot;</span>, <span style="color: #004993;">name</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> <span style="color: #004993;">toString</span><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: #6699cc; font-weight: bold;">var</span> display<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span>;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;-----&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #004993;">name</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;를 준비합니다 -----&quot;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> dough <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
				display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>도우 반죽중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> dough.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> sauce <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
				display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>소스 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> sauce.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> cheese <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
				display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>치즈 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> cheese.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> veggies.<span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> t<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span>;
				<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> veggies.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
				<span style="color: #000000;">&#123;</span>
					t <span style="color: #000000; font-weight: bold;">+</span>= veggies<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;, &quot;</span>;
				<span style="color: #000000;">&#125;</span>
				display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;<span style="">\n</span>야채 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> t<span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> clam <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
				display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>조개 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> clam.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> pepperoni <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #0033ff; font-weight: bold;">null</span> <span style="color: #000000;">&#41;</span>
				display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;<span style="">\n</span>페퍼로니 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> pepperoni.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> display
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>결국 Pizza 클래스는 모든 피자 만드는 방법을 포괄하고 있고, CheesePizza 클래스는 모든 치즈피자 (시카고든 뉴욕이든) 만드는 방법을 포괄하고 있습니다. 이렇게 할 수 있는 이유는 각 분점에 재료를 공급하는 공장이 알아서 그 분점에 맞는 재료를 공급해 주기 때문입니다.</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
</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: #009900;">//팩토리 메서드 패턴의 호스트코드 와 동일하게 사용</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> nyStore<span style="color: #000000; font-weight: bold;">:</span>PizzaStore = <span style="color: #0033ff; font-weight: bold;">new</span> NYPizzaStore<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> chicagoStore<span style="color: #000000; font-weight: bold;">:</span>PizzaStore = <span style="color: #0033ff; font-weight: bold;">new</span> ChicagoPizzaStore<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = nyStore.orderPizza<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;cheese&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문한 피자 나왔습니다 :&quot;</span>, pizza.getName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
&nbsp;
			pizza = chicagoStore.orderPizza<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;veggie&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문한 피자 나왔습니다 :&quot;</span>, pizza.getName<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></pre></td></tr></table></div>

<p>trace 도 역시 거의 동일합니다.</p>
<p><span style="color: #808080;">주문접수 : 뉴욕스타일 치즈피자<br />
&#8212;&#8211;뉴욕스타일 치즈피자를 준비합니다 &#8212;&#8211;<br />
도우 반죽중&#8230; : 씬 크러스트 도우<br />
소스 추가중&#8230; : 마리나라 소스<br />
치즈 추가중&#8230; : 레지오노 치즈<br />
피자를 굽습니다 : 뉴욕스타일 치즈피자<br />
피자를 자릅니다 : 뉴욕스타일 치즈피자<br />
피자를 포장합니다 : 뉴욕스타일 치즈피자<br />
주문한 피자 나왔습니다 : 뉴욕스타일 치즈피자</span></p>
<p><span style="color: #808080;">주문접수 : 시카고스타일 야채피자<br />
&#8212;&#8211;시카고스타일 야채피자를 준비합니다 &#8212;&#8211;<br />
도우 반죽중&#8230; : 익스트라 크러스트 도우<br />
소스 추가중&#8230; : 플럼토마토 소스<br />
치즈 추가중&#8230; : 모짜렐라 치즈<br />
야채 추가중&#8230; : 검은 올리브, 시금치, 가지,<br />
피자를 굽습니다 : 시카고스타일 야채피자<br />
피자를 자릅니다 : 시카고스타일 야채피자<br />
피자를 포장합니다 : 시카고스타일 야채피자<br />
주문한 피자 나왔습니다 : 시카고스타일 야채피자</span></p>
<p>이렇게 구조를 짜 놓았다면 기존에 이미 잘 돌아가고 있는 분점의 코드는 전혀 수정을 하지 않고 계속 확장을 해 나갈 수 있게 됩니다. 이후로 분점이 다른곳에도 생겨도 재료공장만 추가로 구현하기만 하면 문제없이 확장이 가능하다는 이야기도 됩니다. 또한 새로운 피자메뉴가 개발되어도 Pizza 클래스를 확장하여 새로운 피자제품 클래스만 만들어 내면 모든 분점이 이 새로운 피자 메뉴를 만들 수 있게 되겠죠.</p>
<p>새 클래스를 만들어서 테스트 해 봤더니 컴파일 에러가 났다면? 새로 추가한 클래스 내부의 에러에 국한해서 살펴보면 됩니다. 추상층으로 거슬러 올라가 코드가 어디부터 틀렸는지 죄다 살펴볼 필요가 없는 것입니다.</p>
<p>추상 팩토리 패턴은 각 재료에 대한 모든 구상 클래스가 있습니다. 클래스 갯수가 너무 많아 일일히 이 포스트에 늘어놓진 않았습니다. 컴파일 해보실 분은 아래에 있는 총 35개의 클래스 전체를 다운로드해서 컴파일 해보시기 바랍니다.</p>
<p> </p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=8" title=" downloaded 105 times" >Abstract Factory Pattern Example 액션스크립트 코드 다운로드 (105)</a>
<ol class="footnotes"><li id="footnote_0_199" class="footnote">이 부분이 팩토리 메서드와 추상 팩토리의 경계를 모호하게 만드는 것 같습니다.</li><li id="footnote_1_199" class="footnote">피자 종류에 따라 사용하지 않는 재료도 일단은 모두 구현을 해 놓았다는 의미 입니다.</li><li id="footnote_2_199" class="footnote">도우, 치즈, 소스의 종류가 틀리지만 어쨌건 도우가 들어가고, 치즈도 들어가고, 소스도 들어간다는 사실 자체가 동일하다는 의미입니다.</li><li id="footnote_3_199" class="footnote">구체적으로는 제작순서가 동일하다 라고 할 수 있겠습니다.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/199/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Factory Method Pattern 팩토리 메서드 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/196</link>
		<comments>http://ufx.kr/blog/196#comments</comments>
		<pubDate>Wed, 27 May 2009 00:17:45 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Factory Pattern]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[Simple Factory]]></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=196</guid>
		<description><![CDATA[헤드퍼스트 디자인 패턴의 제 4 장의 내용은 팩토리 패턴 입니다. 이전 포스트에서 심플 팩토리로 워밍업 했었는데요, 형태 자체는 거의 동일하므로 심플 팩토리를 읽고 이 포스트를 읽는다면 보다 확실한 이해를 할 수 있으리라 생각합니다.
우선 팩토리 메서드 패턴의 정의를 보도록 하죠.
&#8220;팩토리 메서드 패턴 &#8211; 팩토리 메서드 패턴에서는 객체를 생성하기 위한 인터페이스[01] 를 정의하는데, 어떤 클래스의 인스턴스를 만들지는 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-195" title="nystylepizza" src="http://ufx.kr/blog/wp-content/uploads/2009/05/nystylepizza.jpg" alt="nystylepizza" width="240" height="203" />헤드퍼스트 디자인 패턴의 제 4 장의 내용은 팩토리 패턴 입니다. 이전 포스트에서 <a href="http://ufx.kr/blog/191">심플 팩토리로 워밍업</a> 했었는데요, 형태 자체는 거의 동일하므로 심플 팩토리를 읽고 이 포스트를 읽는다면 보다 확실한 이해를 할 수 있으리라 생각합니다.</p>
<p>우선 팩토리 메서드 패턴의 정의를 보도록 하죠.</p>
<p><span style="color: #808080;">&#8220;팩토리 메서드 패턴 &#8211; 팩토리 메서드 패턴에서는 객체를 생성하기 위한 인터페이스<sup>[<a href="http://ufx.kr/blog/196#footnote_0_196" id="identifier_0_196" class="footnote-link footnote-identifier-link" title="여기에서의 인터페이스는 interface 키워드를 사용하는 인터페이스를 지칭한다기 보다는 추상메서드도 포함하는 좀더 넓은 의미의 인터페이스를 의미합니다.">01</a>]</sup> 를 정의하는데, 어떤 클래스의 인스턴스를 만들지는 서브클래스에서 결정하게 만듭니다. 팩토리 메서드 패턴을 이용하면 클래스의 인스턴스를 만드는 일을 서브클래스에게 맡기는 것이죠.&#8221;</span></p>
<p>피자 프랜차이즈가 분점을 내게 되었습니다. 뉴욕과 시카고는 <del datetime="2009-05-28T02:30:18+00:00">서울과 대구</del> 양키스와 레드삭스 만큼이나 달라서 피자에 들어가는 재료가 꽤나 다릅니다. 실제로 업무에서도 객체를 생산하는 구조는 동일하지만, 구상 클래스들의 형식이 달라서 그에따른 카테고리를 나눠서 객체를 생성해야 하는때가 빈번하게 생기는데 바로 그런 경우에 팩토리 메서드 패턴을 사용하면 좋을것 같습니다.</p>
<p><span id="more-196"></span>추상층 부터 보겠습니다.</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
</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> PizzaStore
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> createPizza<span style="color: #000000;">&#40;</span> $item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Pizza
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;createPizza( $item:String ) 추상메서드&quot;</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> orderPizza<span style="color: #000000;">&#40;</span> $type<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Pizza
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//Simple Factory에서는 factory.createPizza( $type ) 로 만들었지만 여기서는 추상메서드를 통해 피자를 만듬</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = createPizza<span style="color: #000000;">&#40;</span> $type <span style="color: #000000;">&#41;</span>;
			pizza.prepare<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			pizza.bake<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			pizza.cut<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			pizza.box<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> pizza;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>심플 팩토리의 PizzaStore 에서는 SimplePizzaFactory 객체를 만들고 그 객체의 레퍼런스를 이용해 createPizza( &#8220;{피자종류}&#8221; ) 를 직접 호출하는 방식을 취했지만, 팩토리 메서드 패턴에서는 위에서 보시는 바와 같이 피자를 만드는 메서드를 같은 객체에 있는 createPizza() 추상 메서드를 호출합니다. 뉴욕 피자를 만들것인지 시카고 피자를 만들것인지는 추상층에서는 알고 있을 필요가 없습니다. (알 수 없습니다.)</p>
<p>그럼 심플 팩토리에서 SimplePizzaFactory 클래스 역할을 하게되는 ChicagoPizzaStore<sup>[<a href="http://ufx.kr/blog/196#footnote_1_196" id="identifier_1_196" class="footnote-link footnote-identifier-link" title="뉴욕 분점에서는 당연히 NYPizzaStore를 이용하겠죠.">02</a>]</sup> 를 보겠습니다.</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
</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> ChicagoPizzaStore extends PizzaStore
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> createPizza<span style="color: #000000;">&#40;</span> $item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Pizza
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;cheese&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> ChicagoStyleCheesePizza<span style="color: #000000;">&#40;</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;pepperoni&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> ChicagoStylePepperoniPizza<span style="color: #000000;">&#40;</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;clam&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> ChicagoStyleClamPizza<span style="color: #000000;">&#40;</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $item == <span style="color: #990000;">&quot;veggie&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> ChicagoStyleVeggiePizza<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> pizza;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>클래스 이름만 바뀌었을 뿐 심플 팩토리와 완전히 동일하군요. 다만 심플 팩토리와는 달리 PizzaStore 클래스를 확장(상속)하고 createPizza() 메서드가 override 되었습니다.</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
</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> ChicagoStylePepperoniPizza extends Pizza
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ChicagoStylePepperoniPizza<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;시카고 스타일 페퍼로니피자&quot;</span>;
			dough = <span style="color: #990000;">&quot;익스트라 크러스트 도우&quot;</span>;
			sauce = <span style="color: #990000;">&quot;플럼 토마토 소스&quot;</span>;
			toppings.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;으깬 모짜렐라 치즈&quot;</span> <span style="color: #000000;">&#41;</span>
			toppings.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;검은 올리브&quot;</span> <span style="color: #000000;">&#41;</span>
			toppings.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;시금치&quot;</span> <span style="color: #000000;">&#41;</span>
			toppings.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;가지&quot;</span> <span style="color: #000000;">&#41;</span>
			toppings.<span style="color: #004993;">push</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>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> cut<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>이 클래스가 Pizza 클래스를 확장하였으므로 슈퍼클래스의 변수에 저장한다는 내용입니다. 특이한 내용은 없고, 시카고 스타일 피자는 격자로 잘라야 하므로 Pizza 클래스의 cut() 메서드를 오버라이드 했습니다. (사진들 보니까 그냥 보통 피자들처럼 방사형으로 자르더만&#8230;)</p>
<p>피자 구상 객체들의 추상층인 Pizza 클래스 역시 심플 팩토리와 완전히 동일합니다.</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
</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> Pizza
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</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;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> dough<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> sauce<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> toppings<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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;
		<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> prepare<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> display<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;n-----&quot;</span>;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #004993;">name</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;를 준비합니다 -----&quot;</span>;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;n도우 반죽중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> dough;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;n소스 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> sauce;
			<span style="color: #6699cc; font-weight: bold;">var</span> t<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> toppings.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				t <span style="color: #000000; font-weight: bold;">+</span>= toppings<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;, &quot;</span>;
			<span style="color: #000000;">&#125;</span>
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;n토핑 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> t<span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n&quot;</span>, display <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> bake<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 굽습니다 :&quot;</span>, <span style="color: #004993;">name</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> cut<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 자릅니다 :&quot;</span>, <span style="color: #004993;">name</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> box<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 포장합니다 :&quot;</span>, <span style="color: #004993;">name</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>이 포스트 공개설정 해 놓고 한참있다 보니 정작 호스트코드를 안넣었네요. 어쩐지 뭔가가 빠졌다 싶었습니다.</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
</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: #3f5fbf;">/*
			 * Simple Factory 에서 피자를 만드는 방식
			 * var factory:SimplePizzaFactory = new SimplePizzaFactory()
			 * var store:PizzaStore = new PizzaStore( factory )
			 * var pizza:Pizza = store.orderPizza( &quot;cheese&quot; )
			*/</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> nyStore<span style="color: #000000; font-weight: bold;">:</span>PizzaStore = <span style="color: #0033ff; font-weight: bold;">new</span> NYPizzaStore<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> chicagoStore<span style="color: #000000; font-weight: bold;">:</span>PizzaStore = <span style="color: #0033ff; font-weight: bold;">new</span> ChicagoPizzaStore<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = nyStore.orderPizza<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;cheese&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문한 피자 나왔습니다 :&quot;</span>, pizza.getName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
&nbsp;
			pizza = chicagoStore.orderPizza<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;veggie&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문한 피자 나왔습니다 :&quot;</span>, pizza.getName<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></pre></td></tr></table></div>

<p>trace 내용은 아래와 같습니다.</p>
<p><span style="color: #808080;">&#8212;&#8211;뉴욕 스타일 치즈피자를 준비합니다 &#8212;&#8211;<br />
도우 반죽중&#8230; : 씬 크러스트 도우<br />
소스 추가중&#8230; : 마리나라 소스<br />
토핑 추가중&#8230; : 깍둑썬 레지오노 치즈,<br />
피자를 굽습니다 : 뉴욕 스타일 치즈피자<br />
피자를 자릅니다 : 뉴욕 스타일 치즈피자<br />
피자를 포장합니다 : 뉴욕 스타일 치즈피자<br />
주문한 피자 나왔습니다 : 뉴욕 스타일 치즈피자</span></p>
<p><span style="color: #808080;">&#8212;&#8211;시카고 스타일 야채피자를 준비합니다 &#8212;&#8211;<br />
도우 반죽중&#8230; : 익스트라 크러스트 도우<br />
소스 추가중&#8230; : 플럼 토마토 소스<br />
토핑 추가중&#8230; : 으깬 모짜렐라 치즈, 검은 올리브, 시금치, 가지,<br />
피자를 굽습니다 : 시카고 스타일 야채피자<br />
피자를 네모난 모양으로 자름<br />
피자를 포장합니다 : 시카고 스타일 야채피자<br />
주문한 피자 나왔습니다 : 시카고 스타일 야채피자</span></p>
<p>주석에서 보는 바와 같이 Simple Factory에서는 SimplePizzaFactory 객체를 만들어 PizzaStore() 생성자에 던져야 했지만, 팩토리 메서드에서는 각 분점의 팩토리(NYPizzaStore 등)를 만들면서 상위형으로 업캐스팅 하여 팩토리 객체 자체가 특정 분점의 팩토리를 알고 있도록 만듭니다.</p>
<div id="attachment_194" class="wp-caption alignright" style="width: 250px"><img class="size-full wp-image-194" title="chicagostylepizza" src="http://ufx.kr/blog/wp-content/uploads/2009/05/chicagostylepizza.jpg" alt="시카고 스타일은 이런식!!" width="240" height="180" /><p class="wp-caption-text">시카고 스타일은 이런식!!</p></div>
<p>팩토리 메서드는 이런식으로 서브클래스에서 오버라이드 된 메서드를 통해 객체 생성 작업을 서브클래스에 캡슐화 시킬 수 있습니다. 위의 모든 코드에서 객체를 생성하는 부분은 오로지 ChicagoPizzaStore 에 집중되어 있는걸 볼 수 있죠. 이렇게 하면 슈퍼클래스에 있는 클라이언트 코드와 서브클래스에 있는 객체 생성 코드를 서로 분리하는 효과를 가져오게 되어 유지보수나 코드 수정에 매우 유리한 구조가 되는 것입니다.</p>
<p>피자 사진을 찾는다고 플리커에서 시카고 스타일 피자를 검색했는데, 이건 정말 딱 제가 좋아하는 스타일의 피자네요. Chicago Style Deep Dish Pizza !!! 우리나라에서는 이런 스타일의 피자를 본적이 없는것 같습니다. 아마도 재료비가 엄청나서 만들지 않을것 같네요. (피자 먹고싶어~)</p>
<p> </p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=7" title=" downloaded 83 times" >Factory Pattern (Abstract Methood) Example 액션스크립트 코드 다운로드 (83)</a>
<ol class="footnotes"><li id="footnote_0_196" class="footnote">여기에서의 인터페이스는 interface 키워드를 사용하는 인터페이스를 지칭한다기 보다는 추상메서드도 포함하는 좀더 넓은 의미의 인터페이스를 의미합니다.</li><li id="footnote_1_196" class="footnote">뉴욕 분점에서는 당연히 NYPizzaStore를 이용하겠죠.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/196/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple Factory 심플 팩토리 : 팩토리 패턴의 워밍업 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/191</link>
		<comments>http://ufx.kr/blog/191#comments</comments>
		<pubDate>Tue, 26 May 2009 00:17:29 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Factory Pattern]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[OCP]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Simple Factory]]></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=191</guid>
		<description><![CDATA[헤드퍼스트 디자인 패턴 책의 팩토리 패턴(Factory Pattern) 챕터는 70여페이지에 걸쳐 팩토리 패턴의 이름을 가지고 있는 두가지의 패턴을 다루고 있습니다. 팩토리 메서드 패턴과 추상 팩토리 패턴이죠. 그런데 그 전에 팩토리 패턴은 대략 이런것 이라는 식의 워밍업용의 팩토리 패턴을 언급하고 있는데요. 심플 팩토리(Simple Factory) 입니다.
심플 팩토리는 엄밀히 말하면 패턴이라기 보다는 객체지향 프로그래밍을 할때 늘상 사용하게 되는 관용구 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-192" title="pizza_factory_pattern" src="http://ufx.kr/blog/wp-content/uploads/2009/05/pizza_factory_pattern.jpg" alt="pizza_factory_pattern" width="240" height="180" />헤드퍼스트 디자인 패턴 책의 팩토리 패턴(Factory Pattern) 챕터는 70여페이지에 걸쳐 팩토리 패턴의 이름을 가지고 있는 두가지의 패턴을 다루고 있습니다. <a href="http://ufx.kr/blog/196">팩토리 메서드 패턴</a>과 추상 팩토리 패턴이죠. 그런데 그 전에 팩토리 패턴은 대략 이런것 이라는 식의 워밍업용의 팩토리 패턴을 언급하고 있는데요. 심플 팩토리(Simple Factory) 입니다.</p>
<p>심플 팩토리는 엄밀히 말하면 패턴이라기 보다는 객체지향 프로그래밍을 할때 늘상 사용하게 되는 관용구 같은것이라 패턴으로 취급하지 않는 편입니다. 하지만 뒤에 나올 팩토리 메서드 패턴이나 추상 팩토리 패턴의 기본이 되는 것이라 확실히 짚고 넘어가야 합니다.</p>
<p>심플 팩토리 부분의 코드는 책에 전문이 나와있지 않아 제공되는 자바코드를 직접 컨버팅 하였습니다. 그래서 책의 내용과 약간 다르게 표현된 부분이 있습니다만, 피자는 확실히 주문대로 나옵니다.</p>
<p><span id="more-191"></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
</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> factory<span style="color: #000000; font-weight: bold;">:</span>SimplePizzaFactory = <span style="color: #0033ff; font-weight: bold;">new</span> SimplePizzaFactory<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> store<span style="color: #000000; font-weight: bold;">:</span>PizzaStore = <span style="color: #0033ff; font-weight: bold;">new</span> PizzaStore<span style="color: #000000;">&#40;</span> factory <span style="color: #000000;">&#41;</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = store.orderPizza<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;cheese&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문한 피자 나왔습니다 :&quot;</span>, pizza.getName<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
&nbsp;
			pizza = store.orderPizza<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;veggie&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;주문한 피자 나왔습니다 :&quot;</span>, pizza.getName<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></pre></td></tr></table></div>

<p>SimplePizzaFactory 객체를 만들고 PizzaStore 객체에 인자로 던집니다. PizzaStore 객체의 orderPizza() 메서드를 이용해 피자를 만들어 내네요.</p>
<p>그럼 SimplePizzaFactory 는 어떻게 되어있는지 보겠습니다.</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
</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> SimplePizzaFactory
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> createPizza<span style="color: #000000;">&#40;</span> $type<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Pizza
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza = <span style="color: #0033ff; font-weight: bold;">null</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $type == <span style="color: #990000;">&quot;cheese&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> CheesePizza<span style="color: #000000;">&#40;</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $type == <span style="color: #990000;">&quot;pepperoni&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> PepperoniPizza<span style="color: #000000;">&#40;</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $type == <span style="color: #990000;">&quot;clam&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> ClamPizza<span style="color: #000000;">&#40;</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: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> $type == <span style="color: #990000;">&quot;veggie&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				pizza = <span style="color: #0033ff; font-weight: bold;">new</span> VeggiePizza<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> pizza;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>네, 그냥 인자로 받은 피자 종류에 따라 구상 클래스로부터 객체를 생성하는 역할만 하고 있습니다. 이 부분은 코드 변경에 대해 닫혀있지 않아서 좋지 않은 구조를 가지고 있다고 할 수 있습니다.<sup>[<a href="http://ufx.kr/blog/191#footnote_0_191" id="identifier_0_191" class="footnote-link footnote-identifier-link" title="객체지향 프로그래밍에서의 OCP의 원칙에 따르면 클래스는 확장에 대해서는 열여 있어야 하고(확장가능), 코드 변경에 대해서는 닫혀 있어야(기존에 만든 코드는 변경하지 말것) 합니다.">01</a>]</sup> 피자가게의 메뉴가 변경이 된다면 이 코드를 직접 고쳐야 하는거죠.<br />
일단 심플 팩토리 이기 때문에 이정도의 구현만 하고 넘어갑니다.</p>
<p>위의 SimplePizzaFactory 를 인자로 받는 PizzaStore 를 보도록 할까요?</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
</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> PizzaStore
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> factory<span style="color: #000000; font-weight: bold;">:</span>SimplePizzaFactory;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PizzaStore<span style="color: #000000;">&#40;</span> $factory<span style="color: #000000; font-weight: bold;">:</span>SimplePizzaFactory <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.factory = $factory;
		<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> orderPizza<span style="color: #000000;">&#40;</span> $type<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Pizza
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> pizza<span style="color: #000000; font-weight: bold;">:</span>Pizza;
&nbsp;
			pizza = factory.createPizza<span style="color: #000000;">&#40;</span> $type <span style="color: #000000;">&#41;</span>;
&nbsp;
			pizza.prepare<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			pizza.bake<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			pizza.cut<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			pizza.box<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> pizza;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>SimplePizzaFactory 객체를 생성자에서 받아 레퍼런스를 저장하고, 호스트코드에서 PizzaStore 객체를 만들때 던진 피자 종류를 인자로 받아 SimplePizzaFactory 의 createPizza() 메서드를 이용해서 Pizza 객체를 만들고 필요한 각 메서드를 순서대로 호출합니다.</p>
<p>그리고 피자가게에 반드시 있어야 할 Pizza 클래스 입니다.</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
</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> Pizza
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</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;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> dough<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> sauce<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> toppings<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <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;
		<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> prepare<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> display<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;n-----&quot;</span>;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #004993;">name</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;를 준비합니다 -----&quot;</span>;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;n도우 반죽중...&quot;</span>;
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #990000;">&quot;n소스 추가중...&quot;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> t<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> toppings.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				t <span style="color: #000000; font-weight: bold;">+</span>= toppings<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;, &quot;</span>;
			<span style="color: #000000;">&#125;</span>
			display <span style="color: #000000; font-weight: bold;">+</span>= <span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;n토핑 추가중... : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> t<span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n&quot;</span>, display <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> bake<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 굽습니다 :&quot;</span>, <span style="color: #004993;">name</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> cut<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 자릅니다 :&quot;</span>, <span style="color: #004993;">name</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> box<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;피자를 포장합니다 :&quot;</span>, <span style="color: #004993;">name</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>코드는 길지만 클래스 구조적으로 하는일은 없습니다. 단지 인스턴스 변수에 필요한 내용을 저장하는것 뿐이죠.</p>
<p>피자 구상 클래스 입니다. SimplePizzaFactory 에서 구상객체를 만들때 사용합니다.</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
</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> PepperoniPizza extends Pizza
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> PepperoniPizza<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">name</span> = <span style="color: #990000;">&quot;페퍼로니 피자&quot;</span>;
			dough = <span style="color: #990000;">&quot;크러스트&quot;</span>;
			sauce = <span style="color: #990000;">&quot;마리나라 소스&quot;</span>;
			toppings.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;슬라이스 페퍼로니&quot;</span> <span style="color: #000000;">&#41;</span>
			toppings.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;슬라이스 양파&quot;</span> <span style="color: #000000;">&#41;</span>
			toppings.<span style="color: #004993;">push</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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>이렇게 심플 팩토리는 구조가 간단합니다.</p>
<p>이 피자가게 심플 픽토리에서 SimplePizzaFactory 는 유일하게 구상 클래스 들을 직접 참조하는 클래스 입니다. PizzaStore 클래스는 직접 피자 객체를 만들지 않고 SimplePizzaFactory 를 이용해서 피자를 만드는 구조로 되어 있습니다.</p>
<p> </p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=6" title=" downloaded 123 times" >Simple Factory Example 액션스크립트 코드 다운로드 (123)</a>
<ol class="footnotes"><li id="footnote_0_191" class="footnote">객체지향 프로그래밍에서의 OCP의 원칙에 따르면 클래스는 확장에 대해서는 열여 있어야 하고(확장가능), 코드 변경에 대해서는 닫혀 있어야(기존에 만든 코드는 변경하지 말것) 합니다.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/191/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Decorator Pattern 데코레이터 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/189</link>
		<comments>http://ufx.kr/blog/189#comments</comments>
		<pubDate>Mon, 25 May 2009 17:10:39 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[OOP]]></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=189</guid>
		<description><![CDATA[Head First Design Pattern 의 제 3 장 내용인 데코레이터 패턴(Decorator Pattern) 입니다.
먼저 데코레이터 패턴에 대한 정의 입니다.
&#8220;데코레이터 패턴에서는 객체에 추가적인 요건을 동적으로 첨가한다. 데코레이터는 서브클래스를 만드는 것을 통해서 기능을 유연하게 확장할 수 있는 방법을 제공한다.&#8221;
본문에 예로 든 상황은 커피체인점에서 커피와 첨가물에 대한 가격을 계산하는 프로그램을 만드는 것입니다. 우리나라의 커피체인점은 인기있는 메뉴를 몇개 정하여 정해진 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-190" title="coffee_decoration" src="http://ufx.kr/blog/wp-content/uploads/2009/05/coffee_decoration.jpg" alt="coffee_decoration" width="240" height="240" />Head First Design Pattern 의 제 3 장 내용인 데코레이터 패턴(Decorator Pattern) 입니다.</p>
<p>먼저 데코레이터 패턴에 대한 정의 입니다.</p>
<p><span style="color: #808080;">&#8220;데코레이터 패턴에서는 객체에 추가적인 요건을 동적으로 첨가한다. 데코레이터는 서브클래스를 만드는 것을 통해서 기능을 유연하게 확장할 수 있는 방법을 제공한다.&#8221;</span></p>
<p>본문에 예로 든 상황은 커피체인점에서 커피와 첨가물에 대한 가격을 계산하는 프로그램을 만드는 것입니다. 우리나라의 커피체인점은 인기있는 메뉴를 몇개 정하여 정해진 포맷으로 만들어 내는 형식을 가지고 있죠. 모카 카라멜 마끼아또 같이 이미 휘핑크림이 들어가 있고, 카라멜시럽을 뿌리고 하는 등의 정해진 모양을 만들어 냅니다.</p>
<p>그러나 역시 미국에서는 영화에서 보는것과 같이 종류가 다른 커피에 역시 각기 다른 첨가물을 직접 선택할 수 있게 함으로서 소비자의 취향을 중시하는 분위기가 있는 것입니다. 그래서 예로 든 커피체인점의 첨가물을 위한 프로그래밍은 데코레이션 패턴을 설명하기 위한 매우 적절한 예로 보입니다. 정말 데코레이션 하잖아요.</p>
<p><span id="more-189"></span>클래스는 매우 간단합니다. 전체 음료에 대한 추상클래스 한개와 첨가물에 대한 추상클래스 한개가 필요하고, 그 외에는 음료 종류를 표현하기 위한 구상클래스들과 첨가물의 종류별로 구상클래스들 이므로 실제고 데코레이션 패턴의 골격을 이루고 있는 클래스는 2개의 추상클래스 뿐이라고 할 수 있습니다.</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
</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> Beverage
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</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>;
&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> cost<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;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;cost() 추상메서드 입니다.&quot;</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>이 추상클래스를 구상하게 될 클래스에서 override 할 cost() 메서드와, 첨가물 추상클래스에서 한번 더 override 하여 getDescription() 메서드를 통해 전달받을 description 변수를 정의하고 있습니다.</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
</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> Espresso extends Beverage
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Espresso <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">description</span> = <span style="color: #990000;">&quot;에스프레소&quot;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> cost<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> <span style="color: #000000; font-weight:bold;">1990</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>Beverage 를 확장하고 description 에 자신의 이름을 넣고 가격도 리턴합니다.<br />
이런식으로 구조가 동일한 4개 종류의 커피 각 구상클래스는 아래의 코드 다운로드에서 받을 수 있습니다.</p>
<p>다음은 첨가물을 위한 추상 클래스 입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
</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> CondimentDecorator extends Beverage
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> override <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;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;getDescription() 추상메서드 입니다.&quot;</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>역시 Beverage를 확장하고 getDescription() 메서드를 override 합니다. 즉, 추상클래스를 확장하여 또다시 추상클래스를 만든 것이고 구현하는 추상메서드를 도다시 구상클래스에게 넘기는 것입니다.</p>
<p>CondimentDecorator 클래스를 구상하는 클래스 여러개 중 하나를 보겠습니다.</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
</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> Mocha extends CondimentDecorator
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> beverage<span style="color: #000000; font-weight: bold;">:</span>Beverage;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Mocha <span style="color: #000000;">&#40;</span> $beverage<span style="color: #000000; font-weight: bold;">:</span>Beverage <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.beverage = $beverage;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <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> beverage.getDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;, 모카&quot;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> cost<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> <span style="color: #000000; font-weight:bold;">200</span> <span style="color: #000000; font-weight: bold;">+</span> beverage.cost<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>첨가물 구상 클래스들은 Beverage를 확장한 CondimentDecorator 클래스를 한 번 더 확장하므로 2개의 추상메서드를 오버라이드하고 있습니다. 다른 첨가물 클래스도 모두 같은 구조입니다.</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
</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> beverage<span style="color: #000000; font-weight: bold;">:</span>Beverage = <span style="color: #0033ff; font-weight: bold;">new</span> Espresso<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> beverage.getDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, beverage.cost<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #990000;">&quot;원&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #009900;">//출력 : 에스프레소 1990 원</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> beverage2<span style="color: #000000; font-weight: bold;">:</span>Beverage = <span style="color: #0033ff; font-weight: bold;">new</span> DarkRoast<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			beverage2 = <span style="color: #0033ff; font-weight: bold;">new</span> Mocha<span style="color: #000000;">&#40;</span> beverage2 <span style="color: #000000;">&#41;</span>
			beverage2 = <span style="color: #0033ff; font-weight: bold;">new</span> Mocha<span style="color: #000000;">&#40;</span> beverage2 <span style="color: #000000;">&#41;</span>
			beverage2 = <span style="color: #0033ff; font-weight: bold;">new</span> Whip<span style="color: #000000;">&#40;</span> beverage2 <span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> beverage2.getDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, beverage2.cost<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #990000;">&quot;원&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #009900;">//출력 : 다크로스트, 모카, 모카, 휘핑 크림 1490 원</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> beverage3<span style="color: #000000; font-weight: bold;">:</span>Beverage = <span style="color: #0033ff; font-weight: bold;">new</span> HouseBlend<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			beverage3 = <span style="color: #0033ff; font-weight: bold;">new</span> Soy<span style="color: #000000;">&#40;</span> beverage3 <span style="color: #000000;">&#41;</span>;
			beverage3 = <span style="color: #0033ff; font-weight: bold;">new</span> Mocha<span style="color: #000000;">&#40;</span> beverage3 <span style="color: #000000;">&#41;</span>;
			beverage3 = <span style="color: #0033ff; font-weight: bold;">new</span> Whip<span style="color: #000000;">&#40;</span> beverage3 <span style="color: #000000;">&#41;</span>;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> beverage3.getDescription<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, beverage3.cost<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #990000;">&quot;원&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #009900;">//출력 : 하우스 블렌드 커피, 두유, 모카, 휘핑 크림 1340 원</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>총 3개의 커피 주문을 수행하고 있습니다. 에스프레소(첨가물 없음), 다크로스트(모카 더블, 휘핑크림), 하우스블렌드(두유, 모카, 휘핑크림) 입니다. 아 커피값 저렴해서 좋네요~</p>
<p>이런식으로 음료 객체를 만들고 그 객체를 첨가물 생성자 함수에 던져 감싸서 다시 동일한 객체로 받고 필요한만큼 그것을 반복하는 것으로 데코레이션 패턴을 사용합니다.</p>
<p> </p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=5" title=" downloaded 102 times" >Decorator Pattern Example 액션스크립트 코드 다운로드 (102)</a>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/189/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Observer Pattern 옵저버 패턴의 수정 &#8211; 옵저버는 주제를 몰라요.</title>
		<link>http://ufx.kr/blog/173</link>
		<comments>http://ufx.kr/blog/173#comments</comments>
		<pubDate>Tue, 19 May 2009 19:58:01 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[Observer]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[subject]]></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=173</guid>
		<description><![CDATA[이 포스트는 Head First Design Pattern : Observer Pattern 옵저버 패턴을 액션스크립트로 컨버팅 의 마지막 부분에서 언급했던 잘못된 패턴 구현에 대한 코드 수정을 위한 포스트 입니다.
이 문제의 발단은 옵저버 구상 클래스에서 주제인 weatherData 객체를 레퍼런스로 저장하는데서 비롯되었습니다. 주제가 어떤 녀석인지 옵저버에 저장하는 순간 이 옵저버는 더이상 옵저버가 아니고 주제에 직접적으로 관여하는 입장이 되어버립니다.
아래 코드 부분은 수정된 옵저버 구상 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-174" title="the_observer_2" src="http://ufx.kr/blog/wp-content/uploads/2009/05/the_observer_2.jpg" alt="the_observer_2" width="161" height="240" />이 포스트는 <a href="http://ufx.kr/blog/170"><span style="color: #3299ff; ">Head First Design Pattern : Observer Pattern 옵저버 패턴을 액션스크립트로 컨버팅</span></a> 의 마지막 부분에서 언급했던 잘못된 패턴 구현에 대한 코드 수정을 위한 포스트 입니다.</p>
<p>이 문제의 발단은 옵저버 구상 클래스에서 주제인 weatherData 객체를 레퍼런스로 저장하는데서 비롯되었습니다. 주제가 어떤 녀석인지 옵저버에 저장하는 순간 이 옵저버는 더이상 옵저버가 아니고 주제에 직접적으로 관여하는 입장이 되어버립니다.</p>
<p>아래 코드 부분은 수정된 옵저버 구상 클래스 입니다. 현재 주석처리 되어있는 생성자 부분에서 주제인 weatherData 를 인스턴스 변수로 저장할 뿐만 아니라, 심지어 옵저버 스스로 옵저버가 되기를 희망하며 주제의 메소드를 직접 호출하며 옵저버 등록을 시도하게 되어버렸던 것입니다. 이 결과, 주제는 info를 옵저버 들에게 돌려야 하므로 옵저버들이 누군지 당연히 원래부터 알고 있고<sup>[<a href="http://ufx.kr/blog/173#footnote_0_173" id="identifier_0_173" class="footnote-link footnote-identifier-link" title="주제 객체 내부에 배열로 옵저버 리스트를 관리하고 있었습니다.">01</a>]</sup> 옵저버 역시 주제가 누군지 알고 있을 뿐더러 주제의 메소드를 호출하는 형태를 띄게 되면서, 객체지향 언어에서 지양(止揚)해야 할, 객체간 강한 결합을 해버리게 됩니다.<sup>[<a href="http://ufx.kr/blog/173#footnote_1_173" id="identifier_1_173" class="footnote-link footnote-identifier-link" title="OOP 의 원칙 중 클래스 사이의 결합도는 약하게, 하나의 클래스 안에서의 응집도는 강하게 라는 원칙이 있음은 주지의 사실입니다.">02</a>]</sup> </p>
<p><span id="more-173"></span></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
</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> CurrentConditionsDisplay implements IObserver, IDisplayElement
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> temperature<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #009900;">//주제를 저장하는 인스턴스변수 삭제</span>
		<span style="color: #009900;">//private var weatherData:ISubject;</span>
&nbsp;
		<span style="color: #009900;">//생성자 메서드에 있던 $weatherData:ISubject 주제를 받는 인자 삭제</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CurrentConditionsDisplay <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//생성자에서 옵저버역할에 대해서는 할일 없음. 모두 삭제</span>
			<span style="color: #009900;">//this.weatherData = $weatherData;</span>
			<span style="color: #009900;">//weatherData.registerObserver( this )</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> update<span style="color: #000000;">&#40;</span> $temperature<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, $humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, $pressure<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</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: #0033ff; font-weight: bold;">this</span>.temperature = $temperature;
			<span style="color: #0033ff; font-weight: bold;">this</span>.humidity = $humidity;
			display<span style="color: #000000;">&#40;</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> display<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;현재 온도 :&quot;</span>, temperature, <span style="color: #990000;">&quot;도 / 습도 :&quot;</span>, humidity <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>이를 해결하고자 위의 수정본에서 보는바와 같이 옵저버 구상 클래스 생성자 주변에 있던 주제 관련 코드를 모두 없앴습니다.</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
</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;">&#123;</span>
			<span style="color: #009900;">//주제 객체를 생성</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> weatherData<span style="color: #000000; font-weight: bold;">:</span>WeatherData = <span style="color: #0033ff; font-weight: bold;">new</span> WeatherData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">//디스플레이를(옵저버) 객체를 생성</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay1<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay2<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay3<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">//이렇게 주제가 옵저버를 등록하게 하면 옵저버는 주제를 몰라도 되겠죠.</span>
			weatherData.registerObserver<span style="color: #000000;">&#40;</span> currentDisplay1 <span style="color: #000000;">&#41;</span>;
			weatherData.registerObserver<span style="color: #000000;">&#40;</span> currentDisplay2 <span style="color: #000000;">&#41;</span>;
			weatherData.registerObserver<span style="color: #000000;">&#40;</span> currentDisplay3 <span style="color: #000000;">&#41;</span>;
&nbsp;
			weatherData.setMeasurement<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">65</span>, <span style="color: #000000; font-weight:bold;">30.4</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 1,2,3 의 디스플레이가 출력</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 20 도 / 습도 : 65</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 20 도 / 습도 : 65</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 20 도 / 습도 : 65</span>
			weatherData.removeObserver<span style="color: #000000;">&#40;</span> currentDisplay1 <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 1번을 빼면</span>
			weatherData.setMeasurement<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">28</span>, <span style="color: #000000; font-weight:bold;">90</span>, <span style="color: #000000; font-weight:bold;">29.2</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 2,3 의 디스플레이가 출력</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 28 도 / 습도 : 90</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 28 도 / 습도 : 90</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>수정되기 전의 호스트코드와 마찬가지로 주제 객체(날씨 데이터)를 생성하고, 옵저버(디스플레이)를 필요한 수만큼 생성하였습니다. 그러나 이제 디스플레이를 new 로 생성하면서 생성자에 주제를 던져주지 않죠. 주제에 대한 옵저버 등록은 호스트코드에서 직접 합니다. 그리고 나머지 사용 방법은 이전과 다르지 않습니다.</p>
<p>호스트코드에서 해야 할 일이 약간 늘어났지만, 옵저버가 주제를 전혀 몰라도 된다는 점에서 얻어지는 이득이 훨씬 많습니다. 만약 옵저버에 대한 추가요건이나 버그픽스 요청이 접수 되었다면 주제와의 관계는 전혀 신경쓰지 않고 옵저버 클래스 자체에 집중하여 작업을 할 수 있게 되는 것이죠.</p>
<ol class="footnotes"><li id="footnote_0_173" class="footnote">주제 객체 내부에 배열로 옵저버 리스트를 관리하고 있었습니다.</li><li id="footnote_1_173" class="footnote">OOP 의 원칙 중 클래스 사이의 결합도는 약하게, 하나의 클래스 안에서의 응집도는 강하게 라는 원칙이 있음은 주지의 사실입니다.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/173/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Observer Pattern 옵저버 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/170</link>
		<comments>http://ufx.kr/blog/170#comments</comments>
		<pubDate>Mon, 18 May 2009 17:32:44 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[Observer]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[subject]]></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=170</guid>
		<description><![CDATA[Head First Design Pattern 의 제 2 장 내용인 옵저버 패턴(Observer Pattern) 으로 들어가 보겠습니다. 역시 이번에도 자바 코드를 액션스크립트로 컨버팅 하였습니다.
다음은 책 본문에 있는 옵저버 패턴에 대한 설명입니다.
&#8220;옵저버 패턴 &#8211; 한 객체의 상태가 바뀌면 그 객체에 의존하는 다른 객체들한테 연락이 가고 자동으로내용이 갱신되는 방식으로 일대다(one-to-many) 의존성을 정의합니다.
사실 위의 정의만 보고는 뭐가 뭔지 알쏭달쏭한데요, 차근차근 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-171" title="the_observer" src="http://ufx.kr/blog/wp-content/uploads/2009/05/the_observer.jpg" alt="the_observer" width="240" height="161" />Head First Design Pattern 의 제 2 장 내용인 옵저버 패턴(Observer Pattern) 으로 들어가 보겠습니다. 역시 이번에도 자바 코드를 액션스크립트로 컨버팅 하였습니다.</p>
<p>다음은 책 본문에 있는 옵저버 패턴에 대한 설명입니다.</p>
<p>&#8220;옵저버 패턴 &#8211; 한 객체의 상태가 바뀌면 그 객체에 의존하는 다른 객체들한테 연락이 가고 자동으로내용이 갱신되는 방식으로 일대다(one-to-many) 의존성을 정의합니다.</p>
<p>사실 위의 정의만 보고는 뭐가 뭔지 알쏭달쏭한데요, 차근차근 풀어나가보겠습니다.<br />
<span id="more-170"></span></p>
<p>먼저 옵저버 패턴에 등장하는 세 등장인물을 소개하겠습니다.</p>
<ol>
<li>옵저버 패턴의 중심에서 옵저버들에게 통신 내용을 주는 역할을 하는 객체를 주제 (Subject) 라고 합니다. 다른말로는 주체라고도 할 수 있습니다. 주제를 일컫는 말에는 이외에도 다양하게 있습니다. 액션스크립트에서 익숙한 용어로는 디스패처(dispatcher)도 주제를 표현하는 다른 용어이고, supplier, notifier, publisher, server, service, provider 와 같이 출판자, 공급자, 생산자, 배포자의 의미를 가진 단어도 주제의 다른 단어 형태입니다.</li>
<li>한편 옵저버는 클라이언트, 소비자, 구독자, 리스너 등으로 표현할 수 있습니다. 주제가 던져주는 정보를 받아보는 애들이죠. 옵저버 패턴이라는 명칭 때문에 옵저버가 중요한 역할을 하고 있는것 같지만 실제로 옵저버가 하는 일은 아무것도 없고 모든 일은 주제가 합니다. 옵저버는 주제가 던져주는 내용을 보기만 할 수 있습니다.</li>
<li>그리고 주제가 옵저버들에게 돌리는 내용은 info(정보) 입니다. 주제는 자신이 던져준 info 를 옵저버들이 사용하든 안하든 관심이 없습니다. 다만 옵저버 목록에 있다면 info 를 줄 뿐이죠.</li>
</ol>
<p> </p>
<p>책의 예제는 측정된 기상 정보를 여러개의 단말기에 일제히 보내는 시스템을 구현하고 있습니다.</p>
<p>옵저버 인터페이스를 살펴보겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 IObserver
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span> temp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, pressure<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>온도, 습도, 기압 세가지 측정 값을 구상 클래스에 던져주는 역할을 합니다.</p>
<p>그리고 IDisplayElement 클래스는 info가 옵저버에 도착해서 update 되었을 때 호출하는 display 메소드가 있습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 IDisplayElement
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> display<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>다음은 옵저버 클래스 입니다.<br />
이론적으로 옵저버가 되기 위해서는 IObserver 인터페이스만 구현하면 되지만 예제에서 나온 기상 스테이션의 경우 info 가 notify 될 때 말고도 평상시에 info를 보고싶어할 경우도 있을 수 있으므로 display() 메서드를 인터페이스로 빼서 언제든 인터페이스 메소드만 호출하면 모든 옵저버 객체에서 자신이 표시해야 할 값들을 표시하게 합니다.</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
</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> CurrentConditionsDisplay implements IObserver, IDisplayElement
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> temperature<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> weatherData<span style="color: #000000; font-weight: bold;">:</span>ISubject;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CurrentConditionsDisplay <span style="color: #000000;">&#40;</span> $weatherData<span style="color: #000000; font-weight: bold;">:</span>ISubject <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.weatherData = $weatherData;
			weatherData.registerObserver<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</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> update<span style="color: #000000;">&#40;</span> $temperature<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, $humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, $pressure<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</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: #0033ff; font-weight: bold;">this</span>.temperature = $temperature;
			<span style="color: #0033ff; font-weight: bold;">this</span>.humidity = $humidity;
			display<span style="color: #000000;">&#40;</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> display<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;현재 온도 :&quot;</span>, temperature, <span style="color: #990000;">&quot;도 / 습도 :&quot;</span>, humidity <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>클래스 정의를 하고 있는 라인을 보면 옵저버 구상 클래스는 IObserver, IDisplayElement 두 개의 인터페이스를 구현하고 있음을 알 수있습니다.</p>
<p>그럼 이제 주제 인터페이스를 보겠습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
</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 ISubject
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> registerObserver<span style="color: #000000;">&#40;</span> o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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: #339966; font-weight: bold;">function</span> removeObserver<span style="color: #000000;">&#40;</span> o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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: #339966; font-weight: bold;">function</span> notifyObservers<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>주제 인터페이스에서는 옵저버를 등록할 수 있는 메서드, 등록된 옵저버를 해제할 수 있는 메서드, 그리고 새로운 info 가 도착했음을 알리는 메서드가 있습니다.</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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</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> WeatherData implements ISubject
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//옵저버들을 배열로 저장할 인스턴스선언과 세 가지 측정값을 저장하는 변수선언</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> observers<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> temperature<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> pressure<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> WeatherData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			observers = <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>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//옵저버를 등록, ISubject 인터페이스를 구현함.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> registerObserver<span style="color: #000000;">&#40;</span> o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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>
			observers.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> o <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//등록된 옵저버를 등록을 해제, ISubject 인터페이스를 구현함.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> removeObserver<span style="color: #000000;">&#40;</span> o<span style="color: #000000; font-weight: bold;">:</span>IObserver <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> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = observers.<span style="color: #004993;">indexOf</span><span style="color: #000000;">&#40;</span> o <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> i <span style="color: #000000; font-weight: bold;">&gt;</span>= <span style="color: #000000; font-weight:bold;">0</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				observers.<span style="color: #004993;">splice</span><span style="color: #000000;">&#40;</span> i, <span style="color: #000000; font-weight:bold;">1</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//각 옵저버들에게 info를 전달, ISubject 인터페이스를 구현함.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> notifyObservers<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: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> observers.<span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> observer<span style="color: #000000; font-weight: bold;">:</span>IObserver = observers<span style="color: #000000;">&#91;</span> i <span style="color: #000000;">&#93;</span>;
				observer.update<span style="color: #000000;">&#40;</span> temperature, humidity, pressure <span style="color: #000000;">&#41;</span>;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//측정값(info)이 변경되었음을 경우 notifyObservers()를 호출</span>
		<span style="color: #009900;">//그러나 이 메서드는 현재 변경에 대한 코드가 없는 상태입니다. 메서드를 전달만 하고 있네요.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> measurementsChanged<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>
			notifyObservers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//호스트코드에서 info를 주제에게 던질때 사용하는 메서드</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setMeasurement<span style="color: #000000;">&#40;</span> $temperature<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, $humidity<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, $pressure<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</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: #0033ff; font-weight: bold;">this</span>.temperature = $temperature;
			<span style="color: #0033ff; font-weight: bold;">this</span>.humidity = $humidity;
			<span style="color: #0033ff; font-weight: bold;">this</span>.pressure = $pressure;
			measurementsChanged<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>각 메서드에 대한 설명은 코드 부분에 주석으로 추가하였습니다.</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
</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>;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">describeType</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;">&#123;</span>
			<span style="color: #009900;">//WeatherData 객체를 만들어 각각의 옵저버 구상 객체에 넣어주었습니다.</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> weatherData<span style="color: #000000; font-weight: bold;">:</span>WeatherData = <span style="color: #0033ff; font-weight: bold;">new</span> WeatherData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay1<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span> weatherData <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay2<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span> weatherData <span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay3<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span> weatherData <span style="color: #000000;">&#41;</span>;
&nbsp;
			weatherData.setMeasurement<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">65</span>, <span style="color: #000000; font-weight:bold;">30.4</span> <span style="color: #000000;">&#41;</span>;
				<span style="color: #009900;">//출력 : 현재 온도 : 20 도 / 습도 : 65</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 20 도 / 습도 : 65</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 20 도 / 습도 : 65</span>
&nbsp;
			weatherData.removeObserver<span style="color: #000000;">&#40;</span> currentDisplay1 <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//이렇게 1번 디스플레이를 옵저버 리스트에서 빼면</span>
			weatherData.setMeasurement<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">22</span>, <span style="color: #000000; font-weight:bold;">70</span>, <span style="color: #000000; font-weight:bold;">29.2</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//1번 디스플레이를 뺀 2, 3번만 디스플레이 됩니다.</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 22 도 / 습도 : 70</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 22 도 / 습도 : 70</span>
&nbsp;
			weatherData.registerObserver<span style="color: #000000;">&#40;</span> currentDisplay1 <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">//이렇게 다시 넣어줄 수도 있겠죠.</span>
			weatherData.setMeasurement<span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">28</span>, <span style="color: #000000; font-weight:bold;">90</span>, <span style="color: #000000; font-weight:bold;">29.2</span> <span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// 2, 3, 1 번 모두 디스플레이 됩니다.</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 28 도 / 습도 : 90</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 28 도 / 습도 : 90</span>
				<span style="color: #009900;">//출력 : 현재 온도 : 28 도 / 습도 : 90</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>이렇게 주제 객체가 현재 배열로 가지고 있는 옵저버 목록에 있는 객체들에게 update() 메서드를 호출해서 정보를 전달해 주고, 필요에 따라 옵저버 객체들을 옵저버 목록에서 빼거나 다시 등록할 수 있습니다.</p>
<p> </p>
<p><a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=4" title=" downloaded 123 times" >Observer Pattern Example 액션스크립트 코드 다운로드 (123)</a> </p>
<p> </p>
<p>그런데 헤드퍼스트 디자인 패턴의 옵저버 패턴 예제에는 잘못된 부분이 하나 있습니다.<br />
호스트코드를 보면, 옵저버가 되는 currentDisplay1 객체를 생성할 때에 weatherData 를 레퍼런스로 넘기고 있는 부분입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> currentDisplay1<span style="color: #000000; font-weight: bold;">:</span>CurrentConditionsDisplay = <span style="color: #0033ff; font-weight: bold;">new</span> CurrentConditionsDisplay<span style="color: #000000;">&#40;</span> weatherData <span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>옵저버 구상 클래스(CurrentConditionsDisplay) 의 생성자와 변수에도 주제의 레퍼런스를 저장하도록 하고 있는걸 볼 수 있습니다.</p>
<p>97페이지의 바보같은 질문은 없습니다 코너에 보면 그에대한 이유를 설명해 놓았는데요,</p>
<p><span style="color: #808080;">Q: Subject 에 대한 레퍼런스는 왜 저장하죠? 생성자 말고 다른데서는 쓸 일이 없지 않나요?<br />
A: 예 올바른 지적입니다. 하지만 나중에 옵저버 목록에서 탈퇴해야 한다면 주제 객체에 대한 레퍼런스를 저장해 두면 유용하게 쓸 수 있을 것입니다.</span></p>
<p><span style="color: #3366ff;">그러나 책의 이러한 설명은 옵저버 패턴 원칙에서 벗어나는 내용입니다. 주제는 옵저버 리스트를 가지고 있지만, 옵저버는 주제객체를 알 수 없어야 합니다. 옵저버가 주제객체를 아는 순간 옵저버는 더이상 옵저버가 아니게 되는 것이죠. 이런 형태는 옵저버 패턴이라 부르지 않고 그저 객체들간 통신을 하고 있는 것입니다.</span></p>
<p>그러므로 이러한 옵저버 패턴의 잘못된 적용을 바로 잡기 위해 수정된 코드를 포함하여 다음 포스트를 작성하겠습니다. <a href="http://ufx.kr/blog/173"><span style="color: #3299ff;">Observer Pattern 옵저버 패턴의 수정 &#8211; 옵저버는 주제를 몰라요.</span></a></p>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/170/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Strategy Pattern 스트래티지 패턴으로 액션어드밴쳐 게임 만들기</title>
		<link>http://ufx.kr/blog/163</link>
		<comments>http://ufx.kr/blog/163#comments</comments>
		<pubDate>Sat, 16 May 2009 21:38:46 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Strategy]]></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>
		<category><![CDATA[헤드퍼스트]]></category>

		<guid isPermaLink="false">http://ufx.kr/blog/?p=163</guid>
		<description><![CDATA[헤드퍼스트 디자인 패턴 책에는 오리를 이용하여 스트래티지 패턴을 설명하였습니다. 스트래티지 패턴은 OOP의 중요한 원칙 중 하나인 &#8220;상속보다는 합성&#8221; 원칙을 직접 표현하고 있기 때문에 반복학습을 통한 확실한 이해를 하기 위해 오리가 아닌 액션어드벤쳐 게임을 주제로 삼아 이 패턴을 다시한번 설명하도록 하겠습니다.
전체 클래스와 인터페이스를 한눈에 보기 위해 starUML을 이용해 클래스 다이어그램을 작성해 보았습니다.
먼저 Character 클래스는 Knight 와 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-165" title="zelda_and_link" src="http://ufx.kr/blog/wp-content/uploads/2009/05/zelda_and_link.gif" alt="zelda_and_link" width="96" height="71" />헤드퍼스트 디자인 패턴 책에는 오리를 이용하여 스트래티지 패턴을 설명하였습니다. 스트래티지 패턴은 OOP의 중요한 원칙 중 하나인 &#8220;상속보다는 합성&#8221; 원칙을 직접 표현하고 있기 때문에 반복학습을 통한 확실한 이해를 하기 위해 오리가 아닌 액션어드벤쳐 게임을 주제로 삼아 이 패턴을 다시한번 설명하도록 하겠습니다.</p>
<p>전체 클래스와 인터페이스를 한눈에 보기 위해 starUML을 이용해 클래스 다이어그램을 작성해 보았습니다.</p>
<div id="attachment_164" class="wp-caption alignnone" style="width: 560px"><img class="size-full wp-image-164" title="strategypattern_actionadventuregame" src="http://ufx.kr/blog/wp-content/uploads/2009/05/strategypattern_actionadventuregame.png" alt="Strategy Pattern - Action Adventure Game" width="550" height="466" /><p class="wp-caption-text">Strategy Pattern - Action Adventure Game</p></div>
<p>먼저 Character 클래스는 Knight 와 Wizard 클래스의 추상층입니다. 각 직업들이 가지는 공통적인 특성을 정의하여 서브클래스에서 확장(상속) 사용하도록 합니다.</p>
<p>모든 캐릭터들은 무기를 사용할 수 있고 갑옷을 입을 수 있기 때문에 이 두가지의 기능을 커다란 알고리즘으로 분류하여 각각 인터페이스를 만듭니다.<br />
무기를 사용하는 것은 useWeapon() 메서드를 호출하여 무기를 사용하는 모습을 그래픽으로 처리하게 하고, 갑옷을 착용하면 갑옷 종류에 따라 다른 보너스 hp 를 얻도록 할 계획입니다.</p>
<p><span id="more-163"></span></p>
<p>아래의 IArmor 인터페이스를 보면 wearBonus() 메서드의 리턴값이 int 로 되어 있음을 볼 수 있습니다. 이것은 갑옷 종류에 따른 hp 보너스 포인트를 리턴할 수 있도록 한 것입니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 IArmor
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> wearBonus<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>IArmor 인터페이스를 구상하는 클래스중 하나인 Plate 클래스를 보면 인터페이스에 있는 메서드를 구현하고 있고 wearBonus() 메서드에서 int 값 50을 리턴하고 있습니다.</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;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Plate implements IArmor
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Plate <span style="color: #000000;">&#40;</span><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>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> wearBonus<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;현재 hp에 + 50&quot;</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #000000; font-weight:bold;">50</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>이런식으로 아무것도 입지 않은 Naked 클래스를 포함하여, 필요한 갑옷마다 hp 보너스를 설정하여 인터페이스를 구현합니다.</p>
<p>그럼 이제 IWeapon 과 IArmor 인터페이스를 사용하게 되는 Character 클래스를 보면 모든 캐릭터들이 공통적으로 필요한 변수를 설정하고 몇가지에는 getter 를 설정하였습니다. 각 메서드에 대한 설명은 주석으로 달아놓았습니다.</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
47
48
49
50
51
52
53
54
55
56
</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> Character
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> weapon<span style="color: #000000; font-weight: bold;">:</span>IWeapon;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> armor<span style="color: #000000; font-weight: bold;">:</span>IArmor;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _hp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> _characterClass<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Character <span style="color: #000000;">&#40;</span><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>
&nbsp;
		<span style="color: #009900;">//무기를 장비함</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> equipWeapon<span style="color: #000000;">&#40;</span> $weapon<span style="color: #000000; font-weight: bold;">:</span>IWeapon <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>
			weapon = $weapon;
		<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> weaponAction<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>
			weapon.useWeapon<span style="color: #000000;">&#40;</span><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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> equipArmor<span style="color: #000000;">&#40;</span> $armor<span style="color: #000000; font-weight: bold;">:</span>IArmor <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>
			armor = $armor;
&nbsp;
			<span style="color: #009900;">//해당 갑옷의 보너스 hp를 리턴받아 캐릭터의 hp에 더함</span>
			_hp <span style="color: #000000; font-weight: bold;">+</span>= armor.wearBonus<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #009900;">//방어력, 이동속도 등 다른 착용효과 구현 가능</span>
		<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> getObjectWeapon<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>IWeapon
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">return</span> weapon
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//캐릭터 이름, 직업, hp 현황을 리턴</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getStat<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> _name <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;(&quot;</span> <span style="color: #000000; font-weight: bold;">+</span> _characterClass <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;) 현재 hp : &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> _hp
		<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> <span style="color: #0033ff; font-weight: bold;">get</span> hp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">return</span> _hp; <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> <span style="color: #0033ff; font-weight: bold;">get</span> <span style="color: #004993;">name</span><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> _name; <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> <span style="color: #0033ff; font-weight: bold;">get</span> characterClass<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> _characterClass; <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>그리고 Character 클래스를 확장한 Knight 클래스에서는 Character 클래스에서 protected 로 선언하여 상속한 변수들에 초기값을 입력하여 캐릭터를 생성하게 됩니다.</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
</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> Knight extends Character
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Knight <span style="color: #000000;">&#40;</span> $name<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			_hp = <span style="color: #000000; font-weight:bold;">100</span>;
			_name = $name;
			_characterClass = <span style="color: #990000;">&quot;기사&quot;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">name</span> , <span style="color: #990000;">&quot; (이)라는 이름을 가진 &quot;</span>, characterClass ,<span style="color: #990000;">&quot; 캐릭터를 생성하였습니다.&quot;</span> <span style="color: #000000;">&#41;</span>
			weapon = <span style="color: #0033ff; font-weight: bold;">new</span> Fist<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			armor = <span style="color: #0033ff; font-weight: bold;">new</span> Naked<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>여기까지 하면 주먹( new Fist(); ) 이라는 무기와 아무것도 걸치지 않은 맨몸( new Naked(); )의 hp 100짜리 기사 캐릭터가 생성 됩니다.</p>
<p>IArmor를 짚어나가고 있으므로 그 부분을 집중해서 보도록 하겠는데요, 여기서 중요한 부분은 Character 클래스에서 IArmor 데이터형으로 인스턴스 변수를 생성한 armor 에<sup>[<a href="http://ufx.kr/blog/163#footnote_0_163" id="identifier_0_163" class="footnote-link footnote-identifier-link" title="protected var armor:IArmor; 부분을 말합니다.">01</a>]</sup> new Naked(); 라는 갑옷를 대입하여 현재 갑옷상태를 아무것도 입지 않은 상태로 초기 지정하였습니다. 즉, 추상층<sup>[<a href="http://ufx.kr/blog/163#footnote_1_163" id="identifier_1_163" class="footnote-link footnote-identifier-link" title="Character 클래스를 말합니다.">02</a>]</sup> 에서는 상위 형(type)인 인터페이스 형(type)으로 변수 선언을 하고, 구상층에서 상속받은 그 변수에 구체적인 형태를 대입하는 것입니다. 이렇게 하는 이유는 아래 호스트 코드에서 살펴보도록 하겠습니다.</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;">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> player1<span style="color: #000000; font-weight: bold;">:</span>Character = <span style="color: #0033ff; font-weight: bold;">new</span> Knight<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;세계의끝&quot;</span> <span style="color: #000000;">&#41;</span>;
							<span style="color: #009900;">//출력 : -----새로운 캐릭터를 생성합니다.-----</span>
							<span style="color: #009900;">//출력 : 세계의끝  (이)라는 이름을 가진  기사  캐릭터를 생성하였습니다.</span>
							<span style="color: #009900;">//출력 : 맨주먹입니다. (무기가 없습니다)</span>
							<span style="color: #009900;">//출력 : 아무것도 착용하지 않았습니다.</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> player1.getStat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : 세계의끝(기사) 현재 hp : 100</span>
			player1.weaponAction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : 주먹을 휘두릅니다.</span>
			player1.equipWeapon<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> Sword<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력 : Sword 를 장비하였습니다.</span>
			player1.weaponAction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : Sword 를 휘두릅니다</span>
			player1.equipArmor<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> Plate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력 : 판금갑옷을 착용하였습니다.</span>
							<span style="color: #009900;">//출력 : 현재 hp에 + 50</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> player1.getStat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : 세계의끝(기사) 현재 hp : 150</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> player1.getObjectWeapon<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력 : [object Sword]</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> player2<span style="color: #000000; font-weight: bold;">:</span>Character = <span style="color: #0033ff; font-weight: bold;">new</span> Wizard<span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;댄스댄스댄스&quot;</span> <span style="color: #000000;">&#41;</span>;
							<span style="color: #009900;">//출력 : -----새로운 캐릭터를 생성합니다.-----</span>
							<span style="color: #009900;">//출력 : 댄스댄스댄스  (이)라는 이름을 가진  마법사  캐릭터를 생성하였습니다.</span>
							<span style="color: #009900;">//출력 : 맨주먹입니다. (무기가 없습니다)</span>
							<span style="color: #009900;">//출력 : 아무것도 착용하지 않았습니다.</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> player2.getStat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : 댄스댄스댄스(마법사) 현재 hp : 80</span>
			player2.weaponAction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : 주먹을 휘두릅니다.</span>
			player2.equipWeapon<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> Staff<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력 : Staff 를 장비하였습니다.</span>
			player2.weaponAction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : Staff 에서 마법을 발사합니다.</span>
			player2.equipArmor<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> Leather<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력 : 가죽갑옷을 착용하였습니다.</span>
							<span style="color: #009900;">//출력 : 현재 hp에 + 30</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> player2.getStat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력 : 댄스댄스댄스(마법사) 현재 hp : 110</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> player2.getObjectWeapon<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력 : [object Staff]</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>8 번 라인을 보면, 위에서 언급한 것과 마찬가지로 new Knight( &#8220;세계의끝&#8221; ) 를 상위 type 인<br />
var player1:Character 로 받고 있는데요, 이런 형태의 캐스팅을 하게 되면, 인스턴스 변수를 사용할 때에 그게 어떤 구체적인 형태인지 호스트코드에서 알 필요 없이 알아서 사용됩니다.<br />
위의 player1은 type 이 Character 지만 Knight 클래스라는 것을 player1 은 이미 알고 있다는 의미 입니다. 마찬가지로 Character 클래스의 equipArmor() 메서드에서 armor.wearBonus() 를 하면 캐릭터가 뭘 입었는 몰라도,<sup>[<a href="http://ufx.kr/blog/163#footnote_2_163" id="identifier_2_163" class="footnote-link footnote-identifier-link" title="보다 정확하게 말하면 뭘 입었는지 관계 없이">03</a>]</sup> 보너스 hp는 조사하면 다 나온다는 것이죠.</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
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #339966; font-weight: bold;">function</span> wearBonus<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> bonus<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> armor == <span style="color: #990000;">&quot;Plate&quot;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		bonus = <span style="color: #000000; font-weight:bold;">50</span>;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> armor == <span style="color: #990000;">&quot;Leather&quot;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		bonus = <span style="color: #000000; font-weight:bold;">30</span>;
	<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
		bonus = <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> bonus;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>게다가 새로운 갑옷이 늘어날 때마다 else if 를 추가하지 않아도 됩니다. 무엇보다 확장에 유연하고 수정사항이 생겼을때 어디를 얼마만큼 고쳐야 하는지 명확하게 알 수 있게 됩니다.</p>
<p><img class="aligncenter size-full wp-image-166" title="zelda_link_to_the_past_stamp" src="http://ufx.kr/blog/wp-content/uploads/2009/05/zelda_link_to_the_past_stamp.gif" alt="zelda_link_to_the_past_stamp" width="105" height="61" /></p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=3" title=" downloaded 143 times" >Strategy Pattern : Action Adventure Game 액션스크립트 코드 다운로드 (143)</a>
<p> </p>
<p>이 포스트를 쓰면서 계속 마계촌이 생각나서 검색하다 재미있는 것이 있어 마무리로 올려봤습니다.</p>
<p><object width="425" height="344" data="http://www.youtube.com/v/rKPQ9XuR-kg&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;feature=player_embedded&amp;fs=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="src" value="http://www.youtube.com/v/rKPQ9XuR-kg&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;feature=player_embedded&amp;fs=1" /><param name="allowfullscreen" value="true" /></object></p>
<p>마계촌의 후속작인 극마계촌의 PV 입니다.</p>
<ol class="footnotes"><li id="footnote_0_163" class="footnote">protected var armor:IArmor; 부분을 말합니다.</li><li id="footnote_1_163" class="footnote">Character 클래스를 말합니다.</li><li id="footnote_2_163" class="footnote">보다 정확하게 말하면 뭘 입었는지 관계 없이</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/163/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Strategy Pattern 스트래티지 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/160</link>
		<comments>http://ufx.kr/blog/160#comments</comments>
		<pubDate>Fri, 15 May 2009 18:55:43 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[composition]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Strategy]]></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=160</guid>
		<description><![CDATA[이번에는 Head First Design Pattern 의 제 1 장 내용인 스트래티지 패턴(Strategy Pattern : 전략 패턴) 입니다. 역시 이번에도 자바 코드를 액션 스크립트로 컨버팅 하였습니다.
책에는 스트래티지 패턴을 이렇게 정의하고 있습니다.
&#8220;스트래티지 패턴(Strategy Pattern)에서는 알고리즘군을 정의하고 각각을 캡슐화하여 교환해서 사용할 수 있도록 만든다. 스트래티지 패턴을 사용하면 알고리즘을 사용하는 클라이언트와는 독립적으로 알고리즘을 변경할 수 있다&#8221;
슈퍼클래스에 구체적인 행동에 대한 [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-161" title="strategy_pattern_chess" src="http://ufx.kr/blog/wp-content/uploads/2009/05/strategy_pattern_chess.jpg" alt="strategy_pattern_chess" width="240" height="220" />이번에는 Head First Design Pattern 의 제 1 장 내용인 스트래티지 패턴(Strategy Pattern : 전략 패턴) 입니다. 역시 이번에도 자바 코드를 액션 스크립트로 컨버팅 하였습니다.</p>
<p>책에는 스트래티지 패턴을 이렇게 정의하고 있습니다.</p>
<p>&#8220;스트래티지 패턴(Strategy Pattern)에서는 알고리즘군을 정의하고 각각을 캡슐화하여 교환해서 사용할 수 있도록 만든다. 스트래티지 패턴을 사용하면 알고리즘을 사용하는 클라이언트와는 독립적으로 알고리즘을 변경할 수 있다&#8221;</p>
<p>슈퍼클래스에 구체적인 행동에 대한 메서드가 있는 경우 서브클래스에서 의도하지 않은 오류가 발생할 수 있는 가능성이 있습니다. 오리는 무조건 날 수 있을거라고 생각하고 슈퍼클래스인 Duck에 fly() 메서드를 만들고 구체적으로 날라다니는 구현을 한 경우 고무오리나 모형오리도 날라다니는 오류를 책에서 예를 들어 설명하고 있습니다.</p>
<p>그래서 Duck 클래스에서 개별적인 행동을 빼내서 인터페이스로 만들고 그 인터페이스를 구상하는 클래스들을 만들고, Duck 클래스를 상속받아 세부적인 구현을 하는 서브클래스들에서 인터페이스를 통해 구상 클래스를 사용하게 됩니다.</p>
<p><span id="more-160"></span></p>
<p>먼저 Duck 클래스에서 구체적인 행동인 날아다니는 행동을 인터페이스로 만듭니다.<br />
책에는 인터페이스 이름을 FlyBehavior 라고 하였지만, 뒷 부분을 단어를 빼고 인터페이스임을 확실하게 하기 위하여 앞에 I 를 붙여 IFly 로 인터페이스를 만들었습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
</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 IFly
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> fly<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>그리고 IFly 를 구상하는 구상 클래스를 날아다니는 형태별로 작성합니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
</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> FlyWithWings implements IFly
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> fly<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
</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> FlyNoWay implements IFly
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> fly<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>0
1
2
3
4
5
6
7
8
9
</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> FlyRocketPowered implements IFly
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> fly<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>그리고 Duck 클래스를 아래와 같이 만들었습니다.  Duck 클래스는 슈퍼클래스이자 구상클래스 입니다.</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
</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> Duck
	<span style="color: #000000;">&#123;</span>
		<span style="color: #009900;">//서브클래스에서 사용할 수 있도록 protected 로 변수선언</span>
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> quackBehavior<span style="color: #000000; font-weight: bold;">:</span>IQuack;
		<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #6699cc; font-weight: bold;">var</span> flyBehavior<span style="color: #000000; font-weight: bold;">:</span>IFly;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Duck<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//오리의 모양새를 표현하는 메서드. 서브클래스에서 override 하지 않으면 컴파일 에러를 던지도록 함</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> display<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: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;추상 메서드입니다 override 하세요&quot;</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> performQuack<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>
			quackBehavior.quack<span style="color: #000000;">&#40;</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> performFly<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>
			flyBehavior.fly<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//서브클래스에서도 항상 똑같을 것으로 예상되는 메서드는 Duck 클래스에서 구현해 버립니다.</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> swim<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: #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>
&nbsp;
		<span style="color: #009900;">//아래의 두개의 메서드는 인터페이스로 빼낸 두가지의 행동을 동적으로 바꾸기 위한 setter 메서드들</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> setIFly<span style="color: #000000;">&#40;</span> $fly<span style="color: #000000; font-weight: bold;">:</span>IFly <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>
			flyBehavior = $fly;
		<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> setIQuack<span style="color: #000000;">&#40;</span> $quack<span style="color: #000000; font-weight: bold;">:</span>IQuack <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>
			quackBehavior = $quack;
		<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>
<p>Duck 클래스를 확장(상속)하여 MallardDuck(물오리) 클래스를 만들었는데 아래와 같이 Duck에서 상속한 변수에 MallardDuck에 알맞는 구상클래스<sup>[<a href="http://ufx.kr/blog/160#footnote_0_160" id="identifier_0_160" class="footnote-link footnote-identifier-link" title="꽥꽥 거리는 것을 구상한 Quack 클래스와 날아다니는것을 구상한 FlyWithWings 클래스 입니다.">01</a>]</sup> 로 생성한 구상객체를 대입합니다.</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
</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> MallardDuck extends Duck
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MallardDuck<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900;">//quackBehavior와 flyBehavior는 Duck에서 상속받은 인스턴스변수</span>
			<span style="color: #009900;">//인스턴스변수에 구상클래스의 인스턴스(구체적인 행동)를 생성하여 집어넣음</span>
			quackBehavior = <span style="color: #0033ff; font-weight: bold;">new</span> Quack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			flyBehavior = <span style="color: #0033ff; font-weight: bold;">new</span> FlyWithWings<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #009900;">//Duck 클래스의 추상 메서드를 override</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> display<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n저는 물오리 입니다.&quot;</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>IQuack 인터페이스와 그것을 구상한 클래스들은 IFly와 동일한 구조를 가지고 있습니다. 하단에 모든 코드를 다운받을 수 있도록 해 놓았으니 본문에는 Quack 관련 클래스들을 생략하겠습니다.</p>
<p>ModelDuck(모형오리) 클래스도 만들어서 MallardDuck(물오리) 클래스와는 살짝 다른 구상클래스 객체를 대입합니다.</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;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ModelDuck extends Duck
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ModelDuck<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			quackBehavior = <span style="color: #0033ff; font-weight: bold;">new</span> MuteQuack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			flyBehavior = <span style="color: #0033ff; font-weight: bold;">new</span> FlyNoWay<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> display<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: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n저는 모형오리 입니다.&quot;</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>호스트코드<sup>[<a href="http://ufx.kr/blog/160#footnote_1_160" id="identifier_1_160" class="footnote-link footnote-identifier-link" title="도큐먼트 클래스의 코드를 말합니다">02</a>]</sup> 에서는 MallardDuck과 ModelDuck을 Duck으로 상위형변환 하면서 객체를 만들고 동일한 메소드 들을 호출하면 각 구상 객체에 할당된 인터페이스를 구상한 객체에 맞는 메소드를 실행하게 됩니다.</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
</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;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> mallard<span style="color: #000000; font-weight: bold;">:</span>Duck = <span style="color: #0033ff; font-weight: bold;">new</span> MallardDuck<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			mallard.display<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;		<span style="color: #009900;">//출력: 저는 물오리 입니다.</span>
			mallard.performQuack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력: 꽥!</span>
			mallard.performFly<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력: 날고 있어요!!</span>
&nbsp;
			<span style="color: #6699cc; font-weight: bold;">var</span> model<span style="color: #000000; font-weight: bold;">:</span>Duck = <span style="color: #0033ff; font-weight: bold;">new</span> ModelDuck<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			model.display<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>		<span style="color: #009900;">//출력: 저는 모형오리 입니다.</span>
			model.performQuack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력: &lt;&lt; 조용~ &gt;&gt;</span>
			model.performFly<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력: 저는 못 날아요</span>
&nbsp;
			<span style="color: #009900;">//실행중에 오리의 행동을 바꾸고 싶으면 원하는 행동에 해당하는 Duck의 setter 메소드를 호출합니다.</span>
			model.setIFly<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> FlyRocketPowered<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
			model.performFly<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//출력: 로켓 추진으로 날아갑니다.</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>이렇게 하면 Duck 에서 상속한 모든 오리들에게 IFly 인터페이스가 나는것과 관련있는 다수의 구상 클래스 중에서 설정된 구상 클래스를 위임 받게 됩니다. 이렇게 함으로써 호스트코드에서는 어떤 오리가 오건간에 항상 똑같이 performFly() 메서드만 호출하면 각각의 오리에 알맞는 날아다니는 동작이 표현되는 것입니다.</p>
<p>이 패턴이 강력한 또한가지 이유는 런타임에 구상 객체를 변경할 수 있다는 점입니다. 위의 호스트코드에서 보듯이 모형오리에게 setIFly( new FlyRocketPowered() ) 를 해 주면 Duck 의 setIFly 메서드가 해당 인터페이스의 구상객체를 FlyRocketPowered 객체로 실시간으로 변경하여, 날지 못하던 모형오리에게 로켓추진을 부여할 수 있게 되는 것입니다.</p>
<p>클래스를 상속하여 오버라이드 하지 않고, 이런식으로 인터페이스를 통해 구상클래스를 합치는 것을 합성<sup>[<a href="http://ufx.kr/blog/160#footnote_2_160" id="identifier_2_160" class="footnote-link footnote-identifier-link" title="구성도 같은 말입니다.">03</a>]</sup> 한다고 하고 영어로는 composition 이라고 씁니다. 합성은 객체지향 프로그래밍에서 OOP의 중요한 원칙 중 하나 입니다.</p>
<p> </p>
<a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=2" title=" downloaded 99 times" >Strategy Pattern Example 액션스크립트 코드 다운로드 (99)</a>
<ol class="footnotes"><li id="footnote_0_160" class="footnote">꽥꽥 거리는 것을 구상한 Quack 클래스와 날아다니는것을 구상한 FlyWithWings 클래스 입니다.</li><li id="footnote_1_160" class="footnote">도큐먼트 클래스의 코드를 말합니다</li><li id="footnote_2_160" class="footnote">구성도 같은 말입니다.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/160/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adapter Pattern 어댑터 패턴을 액션스크립트로 컨버팅 &#8211; Head First Design Pattern</title>
		<link>http://ufx.kr/blog/155</link>
		<comments>http://ufx.kr/blog/155#comments</comments>
		<pubDate>Thu, 14 May 2009 08:21:41 +0000</pubDate>
		<dc:creator>세계의끝</dc:creator>
				<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Adapter Pattern]]></category>
		<category><![CDATA[Head First]]></category>
		<category><![CDATA[MallardDuck]]></category>
		<category><![CDATA[WildTurkey]]></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=155</guid>
		<description><![CDATA[제가 참여하고 있는 스터디에서 교재로 사용하고 있는 오라일리/한빛출판사에서 발간된 Head First Design Pattern 입니다. 스터디에 대한 이해도를 높이고자 어댑터 패턴에 관한 자바 코드를 액션스크립트 코드로 컨버팅해 보았습니다.
책에는 어댑터 패턴을 이렇게 정의하고 있습니다.
&#8220;어댑터 패턴(Adapter Pattern) &#8211; 한 클래스의 인터페이스를 클라이언트에서 사용하고자 하는 다른 인터페이스로 변환합니다. 어탭터를 이용하면 인터페이스 호환성 문제 때문에 같이 쓸 수 없는 클래스들을 [...]]]></description>
			<content:encoded><![CDATA[<p><img class=" alignleft" title="Head First Design Pattern" src="http://image.yes24.com/goods/1778966/M" alt="Head First Design Pattern" width="193" height="220" />제가 참여하고 있는 스터디에서 교재로 사용하고 있는 오라일리/한빛출판사에서 발간된 Head First Design Pattern 입니다. 스터디에 대한 이해도를 높이고자 어댑터 패턴에 관한 자바 코드를 액션스크립트 코드로 컨버팅해 보았습니다.</p>
<p>책에는 어댑터 패턴을 이렇게 정의하고 있습니다.</p>
<p>&#8220;어댑터 패턴(Adapter Pattern) &#8211; 한 클래스의 인터페이스를 클라이언트에서 사용하고자 하는 다른 인터페이스로 변환합니다. 어탭터를 이용하면 인터페이스 호환성 문제 때문에 같이 쓸 수 없는 클래스들을 연결해서 쓸 수 있습니다.&#8221;</p>
<p>호스트코드에서 어떤 특정한 구상 클래스를 인터페이스를 통해  사용하고 있는 상황에서, 새로운 구상 클래스의 요건이 생겨서 제 3의 클래스가 생겼습니다. 이 때 호스트코드와 인터페이스의 코드는 변경하지 않고 (게다가 동일한 사용 방법으로) 제 3의 구상클래스의 메소드도 추가 사용할 수 있도록 해주는 패턴이라 할 수 있겠습니다.</p>
<p><span id="more-155"></span></p>
<p>IDuck 인터페이스 입니다. 책에서는 Duck 이지만 인터페이스임을 확실히 하기 위해 Duck 앞에 I를 붙였습니다.</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 IDuck
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> quack <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: #339966; font-weight: bold;">function</span> fly <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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>IDuck 을 구상하는 MallardDuck(물오리) 클래스 입니다.</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
</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> MallardDuck implements IDuck
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> quack<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: #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;">public</span> <span style="color: #339966; font-weight: bold;">function</span> fly<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>새로 등장한 ITurkey (칠면조) 인터페이스 입니다.</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 ITurkey
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> gobble<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: #339966; font-weight: bold;">function</span> fly<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;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>ITurkey 인터페이스를 구상하는 WildTurkey 클래스 입니다.</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
</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> WildTurkey implements ITurkey
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> gobble<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: #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>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> fly<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: #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: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>이렇게 두개의 구상 클래스<sup>[<a href="http://ufx.kr/blog/155#footnote_0_155" id="identifier_0_155" class="footnote-link footnote-identifier-link" title="MallardDuck 과 WildTurkey 를 말합니다.">01</a>]</sup> 는 각자 하는 일이 다르지만 어떠한 요구에 의해 호스트코드에서 WildTurkey 를 MallardDuck 과 동일하게 사용해야 하는 경우가 생겨버렸습니다.</p>
<p>이런경우에 아래와 같이 IDuck 인터페이스를 구현한 TurkeyAdapter 클래스를 이용해 ITurkey 인터페이스를 구상한 WildTurkey 객체를 감싸서 MallardDuck처럼 보이도록 변장을 시킬 수 있습니다.</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
</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> TurkeyAdapter implements IDuck
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> turkey<span style="color: #000000; font-weight: bold;">:</span>ITurkey;
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TurkeyAdapter <span style="color: #000000;">&#40;</span> $turkey<span style="color: #000000; font-weight: bold;">:</span>ITurkey <span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span>.turkey = $turkey
		<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> quack<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>
			turkey.gobble<span style="color: #000000;">&#40;</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> fly<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: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">5</span>; i<span style="color: #000000; font-weight: bold;">++</span> <span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">//칠면조가 어댑터로 실행된 것을 구분하기 위해 오리흉내(5번 짧게 날기) 를 냅니다.</span>
				turkey.fly<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>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>아래와 같이 호스트코드 <sup>[<a href="http://ufx.kr/blog/155#footnote_1_155" id="identifier_1_155" class="footnote-link footnote-identifier-link" title="도큐먼트 클래스를 말합니다">02</a>]</sup>  에서는 생성한 WildTurkey 객체를 TurkeyAdapter로 한번 감싸서, 오리류(IDuck) 전용의 testDuck 메서드에 던지고 MallardDuck의 메서드인 quack() 과 fly() 메서드명을 사용하지만, WildTurkey의 gobble() 과 fly() 메서드를 실행하게 할 수 있습니다.</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
</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> duck<span style="color: #000000; font-weight: bold;">:</span>MallardDuck = <span style="color: #0033ff; font-weight: bold;">new</span> MallardDuck<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> turkey<span style="color: #000000; font-weight: bold;">:</span>WildTurkey = <span style="color: #0033ff; font-weight: bold;">new</span> WildTurkey<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> turkeyAdapter<span style="color: #000000; font-weight: bold;">:</span>IDuck = <span style="color: #0033ff; font-weight: bold;">new</span> TurkeyAdapter<span style="color: #000000;">&#40;</span> turkey <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n칠면조가 말함&quot;</span> <span style="color: #000000;">&#41;</span>;
			turkey.gobble<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			turkey.fly<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n오리가 말함&quot;</span> <span style="color: #000000;">&#41;</span>;
			testDuck<span style="color: #000000;">&#40;</span> duck <span style="color: #000000;">&#41;</span>;
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;n칠면조 어댑터가 말함&quot;</span> <span style="color: #000000;">&#41;</span>;
			testDuck<span style="color: #000000;">&#40;</span> turkeyAdapter <span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> testDuck<span style="color: #000000;">&#40;</span> $duck<span style="color: #000000; font-weight: bold;">:</span>IDuck <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>
			$duck.quack<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			$duck.fly<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>

<div id="attachment_156" class="wp-caption alignleft" style="width: 179px"><img class="size-full wp-image-156" title="adapter_pattern_01" src="http://ufx.kr/blog/wp-content/uploads/2009/05/adapter_pattern_01.png" alt="호스트코드의 실행 결과 입니다." width="169" height="264" /><p class="wp-caption-text">호스트코드의 실행 결과.</p></div>
<p>이 호스트 코드를 실행하면 왼쪽 trace문을 볼 수 있습니다.</p>
<p><span style="color: #808080;">참고로 testDuck( turkeyAdapter ); 에 오리로 감싼 칠면조를 인자로 던지지 않고 testDuck( turkey ); 칠면조를 직접 던지면 컴파일 에러가 발생합니다. 컴파일러의 입장에서는 testDuck( $duck:IDuck ); 메서드는 인자를 오리로 던지라는데 칠면조를 던졌으므로 type casting 에러가 발생하게 되는 것이죠.</span></p>
<p><span style="color: #808080;"><a class="downloadlink" href="http://ufx.kr/blog/wp-content/plugins/download-monitor/download.php?id=1" title=" downloaded 83 times" >Adapter Pattern Example 액션스크립트 코드 다운로드 (83)</a></span></p>
<div id="attachment_157" class="wp-caption alignnone" style="width: 550px"><img class="size-full wp-image-157" title="rubber_duck_sea_by_whispering_hills" src="http://ufx.kr/blog/wp-content/uploads/2009/05/rubber_duck_sea_by_whispering_hills.jpg" alt="rubber_duck_sea_by_whispering_hills" width="540" height="364" /><p class="wp-caption-text">이런 광경이 연상됩니다.</p></div>
<ol class="footnotes"><li id="footnote_0_155" class="footnote">MallardDuck 과 WildTurkey 를 말합니다.</li><li id="footnote_1_155" class="footnote">도큐먼트 클래스를 말합니다</li></ol>]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/155/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
