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

<channel>
	<title>세계의끝과 플래시 원더랜드 &#187; 커맨드</title>
	<atom:link href="http://ufx.kr/blog/tag/%ec%bb%a4%eb%a7%a8%eb%93%9c/feed" rel="self" type="application/rss+xml" />
	<link>http://ufx.kr/blog</link>
	<description>Flash + ActionScript &#38; Design</description>
	<lastBuildDate>Fri, 27 Jan 2012 21:51:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>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 203 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 182 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 장 커맨드 패턴 입니다. 커맨드 패턴은 객체지향 프로그래밍에서 가장 중요한 패턴 중 하나이므로 최소한의 커맨드 패턴을 적용하여 개념에 대한 이해를 돕는 포스트로부터 시작하여 차근차근 단계를 밟아 몇개의 포스트로 나눠서 작성 하겠습니다. 이 포스트는 그 중 가장 간단한 형태의 커맨드 패턴에 대해 다룹니다. 먼저 커맨드 패턴에 대한 정의를 보겠습니다. 커맨드 패턴 [...]]]></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 152 times" >Command (simple) Pattern Example 액션스크립트 코드 다운로드 (152)</a>
]]></content:encoded>
			<wfw:commentRss>http://ufx.kr/blog/248/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

