<?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>ActionScript 3.0 Design Patterns &#187; Memento</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/memento/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript 3.0 Memento Design Pattern: Flash Media Server 3 Application</title>
		<link>http://www.as3dp.com/2008/01/actionscript-30-memento-design-pattern-flash-media-server-3-application/</link>
		<comments>http://www.as3dp.com/2008/01/actionscript-30-memento-design-pattern-flash-media-server-3-application/#comments</comments>
		<pubDate>Sun, 27 Jan 2008 22:28:16 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Memento]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2008/01/27/actionscript-30-memento-design-pattern-flash-media-server-3-application/</guid>
		<description><![CDATA[Jumping Out of Sequence: Memento Brings You Back In the last installment on the Memento Design Pattern, you saw an abstract minimalist version to get an idea of how the Memento saved state and got it back again. I used a string variable as the &#8220;state&#8221; to be saved and retrieved without breaking encapsulation. This [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Jumping Out of Sequence: Memento Brings You Back</strong></p>
<p>In the last installment on the Memento Design Pattern, you saw an abstract minimalist version to get an idea of how the Memento saved state and got it back again. I used a string variable as the &#8220;state&#8221; to be saved and retrieved without breaking encapsulation. This time around I used the Memento to solve a more practical and definitely real world problem. How to allow a user to jump around a multimedia online presentation without getting lost. To get an idea of how this works, take a look at a working example of this application at:</p>
<p><a href="http://www.sandlight.com/memento/">http://www.sandlight.com/memento/</a></p>
<p>When you run the application, you can jump to another level, and then just click the <strong>Return to Last</strong> button, and it will take you back to your jump point. Also, I put up the zip file to save time in getting all of the code in. Figure 1 shows what you can expect to see:</p>
<p><a href='http://www.as3dp.com/wp-content/uploads/2008/01/memen11.jpg' title=''><img src='http://www.as3dp.com/wp-content/uploads/2008/01/memen11.jpg' alt='' /></a><br />
<em>Figure 1</em></p>
<p>You can download it at:</p>
<p><a href="http://poobah.mwd.hartford.edu/wsanders/DesignPatterns/FMementoS.zip">Download the FMS Memento Zip File</a></p>
<p>All of the files are in Flash CS3 format, but the ActionScript files are pretty easy to port over to Flex.</p>
<p><span id="more-63"></span></p>
<p><strong>The Player</strong></p>
<p>The FMS player is unremarkable design wise, but its job is nothing more than to serve as a client to use the Memento and provide me with a practical way to create an online learning system to provide the user with optimum usability. To use this, you&#8217;ll need to download and install FMS3 (you can use FMS2 if you have it installed). You can get the Developers version of FMS3 free at Adobe.com if you don&#8217;t have it. (When you install it on your Windows or Linux box, just ignore the part about a serial number and you automatically get the Developers version.) For you Flex developers, all you&#8217;ll need is a Button component. The rest is just coded in ActionScript 3.0.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p63code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p636"><td class="line_numbers"><pre>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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
</pre></td><td class="code" id="p63code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">ObjectEncoding</span>;
&nbsp;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DoLoadFMS <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rtmpNow:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> good:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadSlide:LoadSlide;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swf:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nc:<span style="color: #0066CC;">NetConnection</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dummy:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cueMe:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> firstCue:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnI:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> setBtn1:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> setBtn2:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> setBtn3:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> setBtn4:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> getBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopVid:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseVid:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidState:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> slideState:<span style="color: #0066CC;">String</span>;
		<span style="color: #808080; font-style: italic;">//</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ct:Caretaker;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> orig:Originator;
		<span style="color: #808080; font-style: italic;">//</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidNow:<span style="color: #0066CC;">Number</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> firstLoad <span style="color: #66cc66;">&#40;</span>swf:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			loadSlide=<span style="color: #000000; font-weight: bold;">new</span> LoadSlide<span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>loadSlide<span style="color: #66cc66;">&#41;</span>;
			loadSlide.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">288</span>, loadSlide.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">36</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DoLoadFMS <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Uncomment following line if you're using FMS2 instead of FMS3</span>
			<span style="color: #808080; font-style: italic;">//NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;</span>
&nbsp;
			nc=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			rtmpNow=<span style="color: #ff0000;">&quot;rtmp://192.168.0.11/memento/dp&quot;</span>;
			nc.<span style="color: #0066CC;">connect</span> <span style="color: #66cc66;">&#40;</span>rtmpNow<span style="color: #66cc66;">&#41;</span>;
			nc.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>, checkConnect<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkConnect <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			good=<span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span> == <span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>good<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,cleanUp<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				vid=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span>  ;
				vid.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">240</span>;
				vid.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">180</span>;
				vid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">18</span>,vid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">36</span>;
				addChild <span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				btn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				btn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Start&quot;</span>;
				btn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">50</span>;
				btn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">54</span>,btn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">230</span>;
				addChild <span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
				btn.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,startClass<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				stopVid=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				stopVid.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Stop&quot;</span>;
				stopVid.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">50</span>;
				stopVid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">114</span>,stopVid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">230</span>;
				addChild <span style="color: #66cc66;">&#40;</span>stopVid<span style="color: #66cc66;">&#41;</span>;
				stopVid.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,stopMem<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				pauseVid=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				pauseVid.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Pause&quot;</span>;
				pauseVid.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">50</span>;
				pauseVid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">174</span>,pauseVid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">230</span>;
				addChild <span style="color: #66cc66;">&#40;</span>pauseVid<span style="color: #66cc66;">&#41;</span>;
				pauseVid.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,pauseMem<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				btnI=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				btnI.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Introduction&quot;</span>;
				btnI.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">24</span>,btnI.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">295</span>;
				addChild <span style="color: #66cc66;">&#40;</span>btnI<span style="color: #66cc66;">&#41;</span>;
				btnI.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,setVidState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				setBtn1=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				setBtn1.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">24</span>,setBtn1.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">325</span>;
				setBtn1.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Part I&quot;</span>;
				addChild <span style="color: #66cc66;">&#40;</span>setBtn1<span style="color: #66cc66;">&#41;</span>;
				setBtn1.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,setVidState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				setBtn2=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				setBtn2.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">24</span>,setBtn2.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">355</span>;
				setBtn2.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Part II&quot;</span>;
				addChild <span style="color: #66cc66;">&#40;</span>setBtn2<span style="color: #66cc66;">&#41;</span>;
				setBtn2.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,setVidState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				setBtn3=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				setBtn3.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">24</span>,setBtn3.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">385</span>;
				setBtn3.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Part III&quot;</span>;
				addChild <span style="color: #66cc66;">&#40;</span>setBtn3<span style="color: #66cc66;">&#41;</span>;
				setBtn3.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,setVidState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				setBtn4=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				setBtn4.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">24</span>,setBtn4.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">415</span>;
				setBtn4.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Part IV&quot;</span>;
				addChild <span style="color: #66cc66;">&#40;</span>setBtn4<span style="color: #66cc66;">&#41;</span>;
				setBtn4.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,setVidState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				getBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span>  ;
				getBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">130</span>,getBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">295</span>;
				getBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Return to Last&quot;</span>;
				addChild <span style="color: #66cc66;">&#40;</span>getBtn<span style="color: #66cc66;">&#41;</span>;
				getBtn.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getVidState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				<span style="color: #808080; font-style: italic;">//Capture metadata from FLV file</span>
				dummy=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span>  ;
				ns.<span style="color: #006600;">client</span>=dummy;
				dummy.<span style="color: #006600;">onMetaData</span>=getMeta;
&nbsp;
				<span style="color: #808080; font-style: italic;">//Get cue points</span>
				cueMe=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span>  ;
				ns.<span style="color: #006600;">client</span>=cueMe;
				cueMe.<span style="color: #006600;">onCuePoint</span>=getAcue;
&nbsp;
				swf=<span style="color: #ff0000;">&quot;swf0.swf&quot;</span>;
				slideState=swf;
				firstLoad <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				ct=<span style="color: #000000; font-weight: bold;">new</span> Caretaker  ;
				orig=<span style="color: #000000; font-weight: bold;">new</span> Originator  ;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startClass <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">play</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mementoU&quot;</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">attachNetStream</span> <span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> stopMem <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #0066CC;">clear</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> pauseMem <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #006600;">togglePause</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> cleanUp <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			good=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetStream.Play.Stop&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>good<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns.<span style="color: #0066CC;">close</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				vid.<span style="color: #0066CC;">clear</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getMeta <span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//All metadata can be captured here.</span>
			<span style="color: #808080; font-style: italic;">//e.g. mdata.duration;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getAcue <span style="color: #66cc66;">&#40;</span>cue:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			swf=cue.<span style="color: #0066CC;">name</span>;
			swf+= <span style="color: #ff0000;">&quot;.swf&quot;</span>;
			slideState=swf;
			afterLoad <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> afterLoad <span style="color: #66cc66;">&#40;</span>swf:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			loadSlide.<span style="color: #006600;">undo</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loadSlide=<span style="color: #000000; font-weight: bold;">new</span> LoadSlide<span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>loadSlide<span style="color: #66cc66;">&#41;</span>;
			loadSlide.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">288</span>,loadSlide.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">36</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setVidState <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			vidState=ns.<span style="color: #0066CC;">time</span>;
			orig.<span style="color: #006600;">setState</span> <span style="color: #66cc66;">&#40;</span>vidState,slideState<span style="color: #66cc66;">&#41;</span>;
			ct.<span style="color: #006600;">addMemento</span> <span style="color: #66cc66;">&#40;</span>orig.<span style="color: #006600;">createMemento</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Introduction&quot;</span> :
					ns.<span style="color: #0066CC;">seek</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
					slideState=<span style="color: #ff0000;">&quot;swf0.swf&quot;</span>;
					afterLoad <span style="color: #66cc66;">&#40;</span>slideState<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Part I&quot;</span> :
					ns.<span style="color: #0066CC;">seek</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">232.498</span><span style="color: #66cc66;">&#41;</span>;
					slideState=<span style="color: #ff0000;">&quot;swf1.swf&quot;</span>;
					afterLoad <span style="color: #66cc66;">&#40;</span>slideState<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Part II&quot;</span> :
					ns.<span style="color: #0066CC;">seek</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">293.355</span><span style="color: #66cc66;">&#41;</span>;
					slideState=<span style="color: #ff0000;">&quot;swf2.swf&quot;</span>;
					afterLoad <span style="color: #66cc66;">&#40;</span>slideState<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Part III&quot;</span> :
					ns.<span style="color: #0066CC;">seek</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">384.6</span><span style="color: #66cc66;">&#41;</span>;
					slideState=<span style="color: #ff0000;">&quot;swf3.swf&quot;</span>;
					afterLoad <span style="color: #66cc66;">&#40;</span>slideState<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;Part IV&quot;</span> :
					ns.<span style="color: #0066CC;">seek</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">461.04</span><span style="color: #66cc66;">&#41;</span>;
					slideState=<span style="color: #ff0000;">&quot;swf4.swf&quot;</span>;
					afterLoad <span style="color: #66cc66;">&#40;</span>slideState<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getVidState <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			orig.<span style="color: #006600;">setMemento</span> <span style="color: #66cc66;">&#40;</span>ct.<span style="color: #006600;">getMemento</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;allState&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			vidNow=orig.<span style="color: #006600;">useVid</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			ns.<span style="color: #0066CC;">seek</span> <span style="color: #66cc66;">&#40;</span>vidNow<span style="color: #66cc66;">&#41;</span>;
			swf=orig.<span style="color: #006600;">useSlide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			afterLoad <span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You can see in the setVidState and getVidState methods that the Memento has been called to preserve this client&#8217;s state. The state that will be a &#8220;snapshot&#8221; of the state is the video&#8217;s current position and the swf file that is loaded as a presentation slide. To help loading everything, the following class acts as a loyal assistant:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p63code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p637"><td class="line_numbers"><pre>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" id="p63code7"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoadSlide <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> LoadSlide <span style="color: #66cc66;">&#40;</span>swf:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">url</span>=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;VideoCode/&quot;</span>+swf<span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> undo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			loader.<span style="color: #006600;">unload</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it for the player. All that&#8217;s left is to build a Memento pattern that it can use.</p>
<p><strong>Originator with Multiple States</strong></p>
<p>In making the Originator, I did not want it to be the client. It makes perfect sense for the Originator to be a client,but the client is not part of the design pattern (as is the case in many) and I wanted the flexibility to use it independent of what happens to be in the Originator. I added a couple of getters to return a saved state back to a client. It make it easier for flexible use. Also, notice that two different types of variables—a string and a number—make up the state that will be placed into a memento.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p63code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p638"><td class="line_numbers"><pre>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" id="p63code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Originator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mstate:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidstate:<span style="color: #0066CC;">Number</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setState <span style="color: #66cc66;">&#40;</span>vidstate:<span style="color: #0066CC;">Number</span>,mstate:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mstate</span>=mstate;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">vidstate</span>=vidstate;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createMemento <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Memento
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Memento<span style="color: #66cc66;">&#40;</span>vidstate,mstate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setMemento <span style="color: #66cc66;">&#40;</span>m:Memento<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			vidstate=m.<span style="color: #006600;">getVid</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			mstate=m.<span style="color: #006600;">getSlide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> useVid <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> vidstate;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> useSlide <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> mstate;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>If you look at the Originator in abstract Memento design pattern in the initial Memento entry, other than two variables instead of one and the added methods, it&#8217;s pretty much the same.</p>
<p><strong>More Memento</strong></p>
<p>The Memento class now has to deal with two different state parameters, each a different type. That&#8217;s not a problem. Two getter methods are set up and two parameters are in the constructor function instead of one. Again, it&#8217;s not very different from the abstract Memento example that appears on this blog.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p63code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p639"><td class="line_numbers"><pre>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" id="p63code9"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Memento
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidstate:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mstate:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//SetState()</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Memento <span style="color: #66cc66;">&#40;</span>vState:<span style="color: #0066CC;">Number</span>, clipSetter:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			vidstate=vState;
			mstate=clipSetter;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> vidstate;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//GetState()</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getSlide <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> mstate;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <strong><code>//GetState</code></strong> comment shows what replaced the single <strong><code>GetState</code></strong> method in the abstract Memento class.</p>
<p><strong>Caretaker with Associative Array</strong></p>
<p>To stash the dual states, I decided to use an associative array. In ActionScript, an associative array is nothing more than an instance of an Object and you can reference the elements using string names rather than numbers as is the case with the Array class. The associative array may not be as handy for working with a stack, but because I only want the application to save the last state of the online learning application, I could reuse the same element over and over. Here&#8217;s the new caretaker.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p63code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6310"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p63code10"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Caretaker
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> storage:<span style="color: #0066CC;">Object</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Caretaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			storage=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addMemento <span style="color: #66cc66;">&#40;</span>m:Memento<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			storage<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;allState&quot;</span><span style="color: #66cc66;">&#93;</span>=m;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMemento <span style="color: #66cc66;">&#40;</span>id:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Memento
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> storage<span style="color: #66cc66;">&#91;</span>id<span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Memento is a valuable tool for capturing state and keeping it encapsulated. It took me about two minutes to set up a state-saving application, but it was not encapsulated—exposing the data to the vagaries of other program elements. However, once you have your hands on a generic Memento, they are very easy to adjust to new state demands and what is stored in a state.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2008%2F01%2Factionscript-30-memento-design-pattern-flash-media-server-3-application%2F&amp;title=ActionScript%203.0%20Memento%20Design%20Pattern%3A%20Flash%20Media%20Server%203%20Application" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/01/actionscript-30-memento-design-pattern-flash-media-server-3-application/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Memento Design Pattern: Encapsulating Saved States</title>
		<link>http://www.as3dp.com/2008/01/actionscript-30-memento-design-pattern-encapsulating-saved-states/</link>
		<comments>http://www.as3dp.com/2008/01/actionscript-30-memento-design-pattern-encapsulating-saved-states/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 22:37:25 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Memento]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2008/01/24/actionscript-30-memento-design-pattern-encapsulating-saved-states/</guid>
		<description><![CDATA[The Undo Pattern One of my most-used keyboard combinations is Ctrl+Z to undo just about anything from a program line of code to a graphic drawing. Fortunately for all of us, we can undo just about anything and get back to a state where things were okay—or at least not as bad. The Memento Design [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Undo Pattern</strong></p>
<p>One of my most-used keyboard combinations is Ctrl+Z to undo just about anything from a program line of code to a graphic drawing. Fortunately for all of us, we can undo just about anything and get back to a state where things were okay—or at least not as bad. The Memento Design Pattern is designed to save state without violating encapsulation. Violating encapsulation could be a problem because encapsulation helps insure the application&#8217;s reliability and extensibility. Saving state in itself is pretty simple—just dump the state&#8217;s value into a variable, array or object and retrieve it when you need it. However, doing so exposes the state to other objects that might affect it in ways you cannot control.</p>
<p><strong>An Enigma</strong></p>
<p>While the purpose of the Memento is about as clear as you can get, it&#8217;s implementation was somewhat of an enigma to me. First, it is one of only three patterns that do not have an interface or abstract class participants (the Singleton and Facade are the others). I suppose that&#8217;s not that big of a deal, but those participants in the pattern always seemed to be the glue that holds everything together. That does not mean that the Memento pattern has no interfaces; just not classes identified as such. (More on this further on.) Second, in looking at every Memento design I could find, they seemed to be all over the map—including a couple that added interface participants in the pattern. Like most patterns, I like to begin with a minimalist example to reveal its structure. Also, I like to stick very close to The Gang of Four&#8217;s structure; so part of the problem is probably my inherent conservatism when it comes to design patterns. Some of the Memento examples I looked at had me hard-pressed to believe that they were actually Mementos as described by Gamma and his associates. Everyone likes the <em>undo idea</em>, but I&#8217;m not so sure that they implement their Mementos with the kind of state encapsulation envisioned by the pattern&#8217;s architects.</p>
<p><strong>Sticking Close to the Memento</strong></p>
<p>To get started, take a look at the Memento class diagram. I included labels in red for the nature of the relations between the three main classes and the dog-eared boxes as well:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2008/01/MementoSmall.png" alt="MementoSmall" title="MementoSmall" width="495" height="193" class="alignnone size-full wp-image-2737" /><br />
<em>Figure 1:Memento Class Diagram</em></p>
<p>The Gang of Four describes the interfaces in terms of the relations between the three participants as wide and narrow relative to the Memento class. First, the Caretaker class acts like a guardhouse storing the mementos and keeping them from other objects giving it a <em>narrow interface</em> to the Memento class. Second, the Originator has a <em>wide interface</em> so that it can restore itself to a previous state and create mementos that will save a given state.<br />
<span id="more-60"></span></p>
<p>My ultimate goal is to take an application that coordinates a video and movie clips for an online learning experience. The user can decide in the middle of a lesson to go somewhere else, and no matter where she goes, her previous place (state) will be resumed as soon as she decides to come back. For example, she might decide to go back over some earlier material to check something and then return to her current place. Actually, this is a pretty simple application using the Memento because it only saves a single previous state with two dimensions.One dimension is the place in the video and the other dimension is the accompanying movie clip. As long as those two states can be preserved, any previous paths are ignored beyond a single step back and return.</p>
<p>For an abstract example of the Memento in use, however, I decided to go with an array that can save multiple steps—states. I also tried an associative array, and that worked fine, but for this example I couldn&#8217;t resist the stack nature of arrays because they can be viewed as a trail of bread crumbs of past states.</p>
<p><strong>A Minimalist Abstract Memento</strong></p>
<p>In looking at the class diagram, you can see the three participants in the class: The Originator, Memento and Caretaker. The originator creates mementos to store a state. This is done by creating a memento and setting the state as a function of a memento getter. The memento stores the internal state of the originator, and the caretaker, with a narrow interface to the memento and only pass the memento. The structure both stores and retrieves states safe from other objects. The following example uses the same terms for both the classes and the operations as the Class Diagram.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p60code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6015"><td class="line_numbers"><pre>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" id="p60code15"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Originator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mstate:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setState <span style="color: #66cc66;">&#40;</span>mstate:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Setting state to: &quot;</span>+mstate<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mstate</span>=mstate;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createMemento <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Memento
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Creating memento with current state&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Memento<span style="color: #66cc66;">&#40;</span>mstate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setMemento <span style="color: #66cc66;">&#40;</span>m:Memento<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			mstate=m.<span style="color: #006600;">getState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;State of originator after getting memento:&quot;</span>+mstate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <strong><code>trace()</code></strong> statements in the class help to show the changing states and what happens when the different methods act on them. The Memento class shows a very simple set of getters and setters for preserving the states as you can see in the following code listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p60code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6016"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p60code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Memento
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mstate:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//SetState()</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Memento <span style="color: #66cc66;">&#40;</span>stateSetter:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			mstate=stateSetter;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//GetState()</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getState <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> mstate;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The final class of the Memento trio is the Caretaker. Notice how it aggregates the Memento class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p60code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6017"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p60code17"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Caretaker
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> storage:<span style="color: #0066CC;">Array</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Caretaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			storage=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addMemento <span style="color: #66cc66;">&#40;</span>m:Memento<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			storage.<span style="color: #0066CC;">push</span> <span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMemento <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">index</span>:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:Memento
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> storage<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">index</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>To test the class, I created a SaveState class. You could use the Originator for generating values, but I like the idea that the Memento can stand as a structure for any application. So in reality, the SaveState class is really any client that may want to use the Memento for saving and retrieving its state.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p60code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6018"><td class="line_numbers"><pre>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" id="p60code18"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SaveState <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ct:Caretaker;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> orig:Originator;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SaveState <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ct=<span style="color: #000000; font-weight: bold;">new</span> Caretaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			orig= <span style="color: #000000; font-weight: bold;">new</span> Originator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			orig.<span style="color: #006600;">setState</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;First Step&quot;</span><span style="color: #66cc66;">&#41;</span>;
			ct.<span style="color: #006600;">addMemento</span> <span style="color: #66cc66;">&#40;</span>orig.<span style="color: #006600;">createMemento</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			orig.<span style="color: #006600;">setState</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Second Step&quot;</span><span style="color: #66cc66;">&#41;</span>;
			ct.<span style="color: #006600;">addMemento</span> <span style="color: #66cc66;">&#40;</span>orig.<span style="color: #006600;">createMemento</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			orig.<span style="color: #006600;">setState</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Third Step&quot;</span><span style="color: #66cc66;">&#41;</span>;
			ct.<span style="color: #006600;">addMemento</span> <span style="color: #66cc66;">&#40;</span>orig.<span style="color: #006600;">createMemento</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//The fourth step was not stored in Caretaker</span>
			orig.<span style="color: #006600;">setState</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fourth Step&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Change index value (below) to see what</span>
			<span style="color: #808080; font-style: italic;">//happens when you retrieve a state</span>
			orig.<span style="color: #006600;">setMemento</span> <span style="color: #66cc66;">&#40;</span>ct.<span style="color: #006600;">getMemento</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Working with the Memento</strong></p>
<p>After the initial struggle of getting the Memento design pattern squared away, I came to see how easy it can be adapted to for use with a wide variety of applications. Notice in the example that in an abstraction of step-by-step process, the first three steps (states) were saved by a memento for retrieval and resetting the sequence. The length of the storage array is 3 instead of 4 simply because the last step was not saved. By placing between 0 and 2 in the <code>getMemento</code> method, you can retrieve any of the saved states. This would simulate the retrieval of a &#8220;saved place&#8221; in an application that allows a user to save previous places (states) in an online learning environment and later retrieve them.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2008%2F01%2Factionscript-30-memento-design-pattern-encapsulating-saved-states%2F&amp;title=ActionScript%203.0%20Memento%20Design%20Pattern%3A%20Encapsulating%20Saved%20States" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/01/actionscript-30-memento-design-pattern-encapsulating-saved-states/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

