<?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; Polymorphism</title>
	<atom:link href="http://www.as3dp.com/category/polymorphism/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>Friends with Benefits: State and Factory Method Together at Last—Part II</title>
		<link>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/</link>
		<comments>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 09:23:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1106</guid>
		<description><![CDATA[In the first part of this article, Friends with Benefits: Refactoring with Multiple Design Patterns—Part I you saw how a Factory Method design pattern was used to hold and deliver data to a DataProvider object. The Factory Method design pattern allowed for easy update by simply adding items to concrete products from a Product interface [...]]]></description>
			<content:encoded><![CDATA[<p>In the first part of this article, <a href="http://www.as3dp.com/2009/06/16/friends-with-benefits-refactoring-with-multiple-design-patterns—part-i/"> Friends with Benefits: Refactoring with Multiple Design Patterns—Part I</a> you saw how a Factory Method design pattern was used to hold and deliver data to a DataProvider object. The Factory Method design pattern allowed for easy update by simply adding items to concrete products from a Product interface (DataSupply) or new concrete Product classes. New data can be added to the Concrete Products and the Client makes requests through the Creator interface. The request is filtered through the Concrete Creator to the Product interface to the concrete Product classes. Now, all we need to do is to refactor the player with a design pattern. However, before starting <strong> download </strong> the application (With 12 classes in the design patterns, a Client class plus three folders of videos, there’s a lot!)  So first off, <a href="http://www.sandlight.net/dpBlog/FactoryMethodState.zip">click here to Download All Files </a>. You can see a <a href="http://www.sandlight.net/dpBlog/dualdp.html">working version of the player and multiple data sets here</a>. These will help pull together all of the elements used in this project.</p>
<p><strong>Here Comes the Big Bad State Machine</strong></p>
<p>Before we get going on refactoring the video player application, we need to see where it will go in the context of both the State and the Factory Method design patterns. Figure 1 shows the relationship between the refactored player (State) and the factory that delivers the data (Factory Method).<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/dualdpfilessm.png" alt="dualdpfilessm" title="dualdpfilessm" width="500" height="392" class="alignnone size-full wp-image-1103" /><br />
<em><strong>Figure 1:</strong> Files for two design patterns in class diagram</em></p>
<p>The images are organized in an “open” folder, and the videos are placed in separate folders showing their relative position to the classes making up the two design patterns. The Client makes a request through the Factory Method to populate a List component with data from a DataProvider. Then, the request from the Client to the player can be made through the List component by clicking on the video selection. Figure 2 shows what the final product looks like so that you can more easily follow the refactoring:<br />
<span id="more-1106"></span><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/dualvid500.png" alt="dualvid500" title="dualvid500" width="500" height="365" class="alignnone size-full wp-image-1114" /></p>
<p><em><strong>Figure 2:</strong> Selected video running in state machine player</em></p>
<p>If Figure 2 looks different from what we started in Part I of this article, it’s because the application changed. Originally, the plan was to take the application made for showing F4V files (H.264 formatted HD files) using the latest version of Flash Media Server (FMS) and show how it was refactored. However, since most readers do not develop with FMS, the plan was changed to show a progressive download application that anyone could use. The State design pattern is almost identical to the one used in our book to develop a player. Likewise, we don’t plan to try your patience by rehashing Chapter 10 or the article on <a href= "http://www.adobe.com/devnet/flashmediaserver/articles/video_state_machine_as3.html" > DevNet </a> that cover statecharts, the details of state machines and the State Design Pattern. Instead, I’d like to focus on the Client and show how it makes requests to different sets of classes using different design patterns. Therefore, I’ll only <em>briefly</em> go over the State Design Pattern to help you orient yourself to its use in the current context with another design pattern.</p>
<p><strong>State Machine as Time Machine</strong></p>
<p>I like to imagine the state machine as a time machine. Each time represents a different state. As you go from one state to another, things look different and the possibilities are different. If you go to the mid 18th century you’ll find horse-powered transportation and in that state you’ll find no air transportation beyond hot air balloons. Conversely, if you go into the future to the 22nd century you may find transportation that runs only on hydrogen and air travel only occurs beyond the stratosphere. In different time periods you expect different capabilities. Likewise, in different states, you have different expectations. For example, in the video player, if you are in a <em>stop state</em> you cannot stop. That’s because in that state, you’re already stopped. Likewise, in a <em>play state</em> you cannot go to play, because you’re already there.</p>
<p>Some states are trickier than others, but you have options. In one video player I made, I would not allow the user to go from Pause to Play or Stop. The user had to first Un-Pause by going to Pause again. That was when the main Pause state was set up as a toggle. One blogger complained that he just couldn’t deal with a Player that would not allow one to go from Pause to Stop or Play. The reason behind my decision was that some funny things happened when one left the Pause state without un-pausing it first. (Now that ActionScript 3.0 has a <strong>NetStream.resume()</strong> method, the toggle is no longer required.) Of course, if one decides that it’s perfectly fine to go from a Pause state to a Play or Stop; all you’ve got to do is to change the code in the Pause state—in fact it’s really easy to do and is one of the advantages of the State Design Pattern. (In case you haven’t noticed, I have no god-like powers to enforce code decisions—you’re programmers; so change the code all you want!)</p>
<p>By setting up your program as state machines, you have the option of easily changing the states. You can change the contents of the states (as suggested in the previous paragraph) or you can add new states. As your application changes and grows, all you need to do is to change the states to reflect the application’s functionality.</p>
<p><strong>Avoiding Conditional Statements</strong></p>
<p>My favorite feature of the State Design Pattern is that it has <em>no conditional statements</em>. Count ‘em—none. In fact, in this entire program, you’ll find only a single <strong>switch</strong> statement in the <strong>ConcreteCreatorVid</strong> class in the Factory Method pattern portion of the program. If you look at different Factory Method patterns in most languages, you’ll find that the concrete creator classes contain one or more conditional statement; so, sometime you <em>do</em> need them. (I’ve concealed one more conditional in the Client that takes care of a little bling, but you’ll have to search to find it—besides it doesn’t count because it’s in the Client.)</p>
<p><a href="http://www.as3dp.com/2007/12/21/lets-get-rid-of-conditionals/">What do I have against conditional statements?</a> Well, it’s not so much that I have anything against conditionals as it is the fact I like the idea of not using them. The State design makes you stop and think of what possibly can happen in a given state. Then instead of first having to filter through conditional statements, you simply do exactly what the current state allows. The logic flow is more pleasing and forces me to be a better programmer.</p>
<p><strong>The Mighty Client</strong></p>
<p>One of the original goals of refactoring my video player was to give it more flexibility and to cut down on the size of a single big class—essentially a huge self-serving Client. The idea of the Client is to make requests, and it <strong>should not be used as</strong> <em>an everything class</em>. Several design patterns include the Client as a participant, but even they recognize the role of the Client as a request-maker.</p>
<p>In Figure 1, you can see that the Client makes requests through the Creator interface in the Factory Method pattern and through the Context class in the State pattern.  A Client making requests from multiple classes or interfaces is not unusual—consider the <a href="http://www.as3dp.com/category/design-patterns/abstract-factory-pattern/"> Abstract Factory </a> pattern. The request-making involves a set of UIs for different data sets for the DataProvider object. It provides the List UI with selections used to request the different videos available for displaying in the player. The following listing shows that the great majority of the Client is taken up by UIs and methods for making request with the UIs.</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('p1106code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11065"><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
</pre></td><td class="code" id="p1106code5"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Progressive Download</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> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">List</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">ListEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</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;">Shape</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;">events</span>.<span style="color: #006600;">NetStatusEvent</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;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <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> btnGp1:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnGp2:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnGp3:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> playList:<span style="color: #0066CC;">List</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">List</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resumeBtn:<span style="color: #0066CC;">Button</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: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> flv:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> metaCap:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopState:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mset:<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> group1:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> group2:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> group3:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> create:Creator=<span style="color: #000000; font-weight: bold;">new</span> ConcreteCreatorVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</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>;
			<span style="color: #808080; font-style: italic;">//Handle end of flv play status</span>
			ns.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,flvCheck<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setProviders<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Set up object to capture metadata</span>
			metaCap = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span>  ;
			ns.<span style="color: #006600;">client</span> = metaCap;
			metaCap.<span style="color: #006600;">onMetaData</span> = getMeta;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Video Buttons and List Components</span>
			playList.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,playList.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>playList<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			group1 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group1;
&nbsp;
			stopBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			stopBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Stop&quot;</span>;
			stopBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>;
			stopBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">142</span>,stopBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>stopBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			pauseBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pauseBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Pause&quot;</span>;
			pauseBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>;
			pauseBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">206</span>,pauseBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pauseBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			resumeBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			resumeBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Resume&quot;</span>;
			resumeBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">55</span>;
			resumeBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">270</span>,resumeBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>resumeBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Add Event Listeners</span>
			playList.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ListEvent.<span style="color: #006600;">ITEM_CLICK</span>,doPlay<span style="color: #66cc66;">&#41;</span>;
			stopBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,doStop<span style="color: #66cc66;">&#41;</span>;
			pauseBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,pauseNow<span style="color: #66cc66;">&#41;</span>;
			resumeBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,resumeNow<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> setProviders<span style="color: #66cc66;">&#40;</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;">//Data provider buttons</span>
			btnGp1.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp1.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">160</span>;
			btnGp1.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group1&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnGp2.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp2.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">190</span>;
			btnGp2.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group2&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnGp3.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp3.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">220</span>;
			btnGp3.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group3&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp3<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Add Event Listeners</span>
			btnGp1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp1<span style="color: #66cc66;">&#41;</span>;
			btnGp2.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp2<span style="color: #66cc66;">&#41;</span>;
			btnGp3.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp3<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> 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: #66cc66;">&#40;</span>mset<span style="color: #66cc66;">&#41;</span> ? removeChildAt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">null</span>;
			mset = <span style="color: #000000; font-weight: bold;">true</span>;
			vid.<span style="color: #0066CC;">width</span> = mdata.<span style="color: #0066CC;">width</span>;
			vid.<span style="color: #0066CC;">height</span> = mdata.<span style="color: #0066CC;">height</span>;
			<span style="color: #000000; font-weight: bold;">var</span> backdrop:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x611427,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>vid.<span style="color: #006600;">x</span>,vid.<span style="color: #006600;">y</span>,vid.<span style="color: #0066CC;">width</span>,vid.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>backdrop,<span style="color: #cc66cc;">0</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> getGp1<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>
			group1 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group1;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp2<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>
			group2 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group2&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group2;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp3<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>
			group3 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group3&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group3;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> flvCheck<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stopState=<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: #66cc66;">&#40;</span>stopState<span style="color: #66cc66;">&#41;</span> ? halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">stopPlay</span><span style="color: #66cc66;">&#40;</span>ns<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> doPlay<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ListEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			flv = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">item</span>.<span style="color: #0066CC;">data</span>;
			context.<span style="color: #006600;">startPlay</span><span style="color: #66cc66;">&#40;</span>ns,flv<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> doStop<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>
			halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> pauseNow<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>
			context.<span style="color: #006600;">pausePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> resumeNow<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>
			context.<span style="color: #006600;">resumePlay</span><span style="color: #66cc66;">&#40;</span>ns<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>I’ve got to admit that for a Client class, this one seems pretty big. However, all of the data for selecting the videos and working the video player are either part of the Factory Method or State design patterns. With only slight modifications <em>in the Client</em> this same player can handle FMS files, including F4V files. I’ve used this same State pattern for several different kinds of players, including the one operating FMIS 3.2 and playing high definition videos (<a href="http://www.sandlight.net/prague/"> such as these</a>) that we discussed in Part I of this post.</p>
<p><strong>The Context Class</strong></p>
<p>The Context class is the key to understanding the State Design Pattern as a <em>pattern</em> and not simply a state machine.  When you look at it,  the class appears to be nothing more than a <em>getter/setter</em> class. However, it is far more. It establishes which state is the <strong>current state</strong>. By setting up the current state, it provides for one of the best examples of polymorphism that can be found in OOP. The State interface provides four methods, but depending on the <em>current</em> concrete state class, each method is implemented differently. The following listing shows the Context class used in this example:</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('p1106code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11066"><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
</pre></td><td class="code" id="p1106code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Context Class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> playState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resumeState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> state:State;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Video Player is on&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Play</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stopState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			pauseState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			resumeState = <span style="color: #000000; font-weight: bold;">new</span> Resume<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			state=stopState;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">startPlay</span><span style="color: #66cc66;">&#40;</span>ns,flv<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">stopPlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">pausePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">resumePlay</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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>state:State<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;A new state is set&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span>=state;
		<span style="color: #66cc66;">&#125;</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>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> state;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPlayState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">playState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStopState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">stopState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPauseState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pauseState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResumeState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">resumeState</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 Context class delegates requests (from the Client) to a State object. All methods either use or return a State object. The trick in the Context is to keep track of the current state so that when a requested method is returned to the Client, it is done in the appropriate manner. (e.g., a stop play request <em>will not</em> be sent from a stop state.)</p>
<p><strong>The State Interface and the States</strong></p>
<p>The state interface and the concrete states are relatively easy. Using either an interface or abstract class, the State interface provides the essential methods that can be implemented in the concrete state classes.  First, the State interface shows four simple methods—playing, stopping, pausing and resuming video play:</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('p1106code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11067"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1106code7"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//State Interface</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">interface</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All of the methods have the same parameters except for the <strong>startPlay()</strong> method that has an additional string parameter for the video URL. The rest are for directing actions in the <strong>NetStream()</strong> object.</p>
<p>Finally, the concrete State classes that implement the State interface are <em>set states</em>. That means that the action associated with the state has been set so that you can possibly do whatever that state allows. For example, if you’re in the Stop state, you can go to the Play state, but since you’re already in the Stop state, it doesn’t make any sense to go there. (Think of yourself at a stop sign in traffic. You can go to “drive” but since you’re at stop, you cannot “go” there.) So, without further ado, let’s look at the four state classes:</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('p1106code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11068"><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
</pre></td><td class="code" id="p1106code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Stop State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Stop</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Stop State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>flv<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Begin playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPlayState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;You're already stopped&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;Cannot go to Pause from Stop.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;Cannot go to Resume from Stop.&quot;</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>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Play State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Play</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Play</span><span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Play State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<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;You're already playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</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>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop playing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStopState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">pause</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;Begin pause.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPauseState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;Cannot resume from a play state.&quot;</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>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Pause State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Pause</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Pause</span><span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Pause State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<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;You have to go to resume&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;Don't go to Stop from Pause&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Allow going from Pause state to Stop state</span>
			<span style="color: #808080; font-style: italic;">//ns.close();</span>
			<span style="color: #808080; font-style: italic;">//context.setState(context.getStopState());</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;Cannot pause from Pause state.&quot;</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> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #006600;">resume</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;Quit pausing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getResumeState</span><span style="color: #66cc66;">&#40;</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>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Resume</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Resume <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Resume<span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Resume State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<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;Start over&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPlayState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</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>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop playing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStopState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">pause</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;Pause.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPauseState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</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;Already in resume state.&quot;</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>In addition to providing a set of methods that direct the program to the next possible set of states, the constructor function in each state class sets itself as the current state. Put differently, each state sets the current context to itself.</p>
<p><strong>Using Multiple Design Patterns</strong></p>
<p>Creating programs with multiple design patterns is no more unusual than writing programs with multiple methods or properties. Further, this particular application suggests that you can mix and match design patterns and reuse the major components for any number of projects. Further, the Client doesn’t know (or care) what changes are made in the different components as long as they follow the rules laid out by the interface and the request to the appropriate component in the pattern is unchanged.</p>
<p>The difficult part is doing it right the first time. Then, later as the program requires changes (<em>as it always does</em>), it’s easy to do so. Further, you can take the exact same design patterns and use them for different applications, which saves time as well.</p>
<p>As a final note, I’ve left several trace statements in the code to help you better see what happens in different states. Once in a given state and running the program from within either Flex or Flash, note the trace output. Also, try requesting “impossible” states—such as pressing the Stop button when the program is already stopped. Finally, if you do not like a condition placed on a state (such as disallowing movement from one state to a certain different state), go ahead and change the program.</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%2F2009%2F06%2Ffriends-with-benefits-state-and-factory-method-together-at-last-part-ii%2F&amp;title=Friends%20with%20Benefits%3A%20State%20and%20Factory%20Method%20Together%20at%20Last%E2%80%94Part%20II" 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/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: Program to an Interface; not an implementation</title>
		<link>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/</link>
		<comments>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 12:55:24 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=685</guid>
		<description><![CDATA[The first principle of design patterns is, Program to an interface, not an implementation Simply put, the Gang of Four urges programmers to declare variables only to abstract classes and interfaces and not concrete implementations. You never want to type your instance as a concrete class derived from an interface or abstract class—only to the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule1.png" alt="bucketrule1" title="bucketrule1" width="150" height="133" class="alignleft size-full wp-image-762" />The first principle of design patterns is,</p>
<blockquote><p>Program to an interface, not an implementation</p></blockquote>
<p> Simply put, the Gang of Four urges programmers to declare variables only to abstract classes and interfaces and not concrete implementations. You never want to type your instance as a concrete class derived from an interface or abstract class—only to the interface.</p>
<p>Okay, you may be thinking that we already covered this principle in our book. That’s true. We did in both the introduction (pp. 45-49) and in part of the chapter on the Observer design pattern (pp. 285-288.) We also had a lot of examples, and so why rehash the same principle here? The following reasons seemed compelling enough to warrant additional discussion:</p>
<ul>
<li>Includes dynamic binding</li>
<li>Closely tied to polymorphism</li>
<li> Built into Creation design patterns</li>
<li> Easy to ‘take to work’</li>
</ul>
<p>Before starting, we’ll use the concept of an <strong>interface</strong> to refer to programs that use either abstract classes or ActionScript 3.0 interface. So, if you see a reference to an <em>interface</em>, it could well be an abstract class. It’s the concept of an interface that’s important; not the ActionScript 3.0 interface statement.<br />
<span id="more-685"></span><br />
As you read this, you may hear echoes of the <a href="http://www.as3dp.com/2009/02/08/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/">Liskov Substitution Principle</a> (LSP). I’m really not sure what relation there is between the LSP and the first GoF principle. Suffice it to say that LSP is part of the back-story (as they say in Hollywood), but only part. During the 1980s and 1990s when a lot of the OOP and Design Patterns concepts were being developed and presented at OOPSLA, ideas were shared. Further, GoF references <em>Abstraction and Specification in Program Development</em> by Barbara Liskov and John Guttag (McGraw-Hill, New York, 1986) so Gamma and his associates were aware of Liskov’s work. But to say that this principle was <em>derived</em> from LSP is a leap that I’m not willing to take. However, I certainly see no contradiction between LSP and GoF’s first principle.</p>
<p><strong>Dynamic Binding, Polymorphism and Interfaces</strong></p>
<p><a href= "http://www.as3dp.com/2008/12/19/actionscript-30-design-patterns-and-polymorphism-what’s-the-point/">Polymorphism</a> has been discussed elsewhere on this blog, and we all know it is one of the pillars of OOP. This first principle of design patterns is tied into polymorphism. In order to follow the path from interfaces to polymorphism, we need to start with an operation’s <strong>signature</strong>. As you know, a signature is made up of the following:</p>
<ul>
<li>operation’s name</li>
<li>objects it takes as parameters</li>
<li>return value</li>
</ul>
<p><em>In this context</em> we can discuss interfaces as all of the operations in an object. For the most part, an object as used by GoF refers to a <em>class</em> and not an instance of a class as the term <em>object</em> is conventionally used. (I won’t belabor the meaning of signatures and interfaces here, but if you want more details on both concepts, see the discussion beginning on page 19 of our book.)</p>
<p>When we type an object, such as,</p>
<p><code><strong>var myInstance:String; </strong></code></p>
<p>we tend to think of <strong>String</strong> as a <em>class type</em>, but in fact it denotes a particular <em>interface</em>. “Any request that matches a signature in the object’s interface may be sent to the object” (GoF 13).</p>
<p>Bumping this up to user created classes, we type to the interface, which is found in the supertype relative to a subtype. That is, a subclasses’ type resides in the parent class. Because all knowledge of an object is known only through its interface, the request does not address the implementation. As a result, objects with different implementations can have the same interface.</p>
<p>Several different objects can have the same interface (primarily based on the superclass) but different implementations, and since the actual operation that is performed is dependent on both the request and receiving object’s name, the actual outcome occurs at run-time.  Such an association between the a request and its operation at run-time is known as <strong>dynamic binding</strong>. The process of dynamic binding lets you substitute objects with identical interfaces at run-time. You probably know this process by another name, <strong>polymorphism</strong>.</p>
<p>Design patterns help you set up interfaces and use polymorphism in organizing your program. The more you look at the different principles of OOP, the more you can see how these principles are maintained and presented in design patterns. Key are ActionScript 3.0 pure interfaces and abstract classes. All operations in an interface are abstract, but an abstract class can defer some or all implementations to derived classes (subclasses.) Thus, the abstract class is more flexible than the pure interface, but both serve as interfaces</p>
<p><strong>Reducing Implementation Dependencies</strong></p>
<p>Gamma and his associates spell out two key benefits of manipulating objects solely in terms of the interface (of either pure interfaces or abstract classes).</p>
<blockquote><p>1. Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that clients expect. </p></blockquote>
<blockquote><p>2. Clients remain unaware of the classes that implement these objects. Clients only know about the abstract class(es) defining the interface. (GoF, p. 18) </p></blockquote>
<p>The whole purpose is to build functional structures but to keep them loose enough for change and reusability. Obviously you have to instantiate concrete classes, but by using the abstract class interface, (instead of the concrete classes’) you provide flexibility in the implementation. All design patterns in the <strong>Creational</strong> category [Factory Method,  Abstract Factory, Prototype, Builder and Singleton*] essentially <em>force</em> you to create your programs in a way that follows this principle. (*I have serious questions as to the Singleton’s ability to do anything very useful along these lines. See <a href="http://www.as3dp.com/2008/11/26/we-don’t-need-no-stinkin’-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming/"> this post</a> for details.)</p>
<p><strong>Implementation Lunch Bucket Rule</strong></p>
<p>While GoF’s statement of the principle is very clear once you understand what they mean by an <strong>interface</strong>, we might be able to re-word it for an even simpler rule to take to work.</p>
<blockquote><p>Type all objects to the interface of the parent class and never to the child class</p></blockquote>
<p>Of course the parent and child class share an interface, but the child class (subclass) has concrete implementations.</p>
<p>The best part about this rule is that it is automatically enforced in all creational patterns. So, besides having a simple rule to work with (and bring to work) you have plenty of patterns that automatically set up your program to follow this rule.</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%2F2009%2F02%2Fdesign-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20Program%20to%20an%20Interface%3B%20not%20an%20implementation" 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/2009/02/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0, Design Patterns and Polymorphism: What’s the Point?</title>
		<link>http://www.as3dp.com/2008/12/actionscript-30-design-patterns-and-polymorphism-whats-the-point/</link>
		<comments>http://www.as3dp.com/2008/12/actionscript-30-design-patterns-and-polymorphism-whats-the-point/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 13:18:08 +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[OOP]]></category>
		<category><![CDATA[Polymorphism]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=384</guid>
		<description><![CDATA[*Note to our gentle readers: This is one of those blog entries that probably raises more issues than it resolves. Further, it requests reader input. Consider yourself forewarned. Why Polymorphism? As just about everybody reading this blog knows, polymorphism is a foundational concept in object oriented programming (OOP). We devote several pages to polymorphism in [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>*Note to our gentle readers: This is one of those blog entries that probably raises more issues than it resolves. Further, it requests reader input. Consider yourself forewarned.</em></strong></p>
<p><strong>Why Polymorphism?</strong></p>
<p>As just about everybody reading this blog knows, <strong><em>polymorphism</em></strong> is a foundational concept in object oriented programming (<strong>OOP</strong>). We devote several pages to polymorphism in our introductory chapter showing what it can do and what it looks like using several examples. (In fact I think we did a pretty decent job) However, I still don’t have the same kind of intuitive sense for polymorphism as I do for encapsulation, inheritance and some of the other concepts that make up OOP and more importantly, design patterns.</p>
<p>I went to my bookcase and found lots of discussion of polymorphism, and on the Web I found lots more. What’s missing (for me at least) is a visceral connection between polymorphism and design patterns. We can all talk about overloading methods, using interfaces to achieve polymorphism and provide all sorts of examples. But that’s not the same thing as smoothly working in polymorphism as a thought pattern when programming. I wondered if anyone else had the same minor intellectual itch that I do, and lo and behold in the Wikipedia entry on <em>type polymorphism</em> I found the following note:</p>
<blockquote><p>
All or part of this article may be confusing or unclear.
</p>
</blockquote>
<p>Such a notice is another way of saying it doesn’t make a lick of sense. This is not to say that the explanation is inaccurate. Technically, it’s very accurate as far as I could see. It’s just not very clear.</p>
<p><span id="more-384"></span></p>
<p>Searching further, I found more confusion about polymorphism. On one Java blog, a participant asked about the different kinds of polymorphism, and another responded that there was only one kind. However, you can quickly find lots of type referenced polymorphism, but whether the different kinds are important to OOP or not is muddled. </p>
<p><strong>How Can You Confuse a Pointer? Easy!!</strong></p>
<p>An important feature of understanding polymorphism is understanding <strong>pointers</strong>. Well, I’ve never had a problem with pointers (or so I thought) until I came across this line in some university notes,</p>
<blockquote><p>Some people claim that Java has no pointers… Not true!<br />
All objects are accessed through <em>references</em>, which are automatically de-referenced pointers</p>
</blockquote>
<p>Right after I read that, my brain caught on fire and smoke billowed out of what’s left of the hair on my head. “What the…?” First, I wanted to know who those people were who claimed that Java has no pointers, and second what do the same people think about pointers in ActionScript 3.0. (Maybe I’ll just gather up my de-referenced pointers and go play solitaire.) Okay, so we need clarification on pointers.</p>
<p>All exasperation aside, none other than <a href="http://java.sun.com/docs/white/langenv/Simple.doc2.html">Sun says that Java has no pointers</a>. What do they mean by that? On the other hand, the Flex ActionScript 3.0 docs indicate that,</p>
<blockquote><p>An object may contain pointers to other objects that themselves contain pointers to even more objects. </p>
</blockquote>
<p>Oddly, that makes perfect sense to me, but what ActionScript 3.0 has by way of pointers that Java does not have is a bit confusing. Why should I care? (Or why should <em>you</em> care?) Here’s why: A lot of the discussions about polymorphism make a reference to pointers, and many of those discussions are in Java contexts. So you see in the Java articles, “blah, blah, blah, pointers, blah, blah, polymorphism…” and up comes the big question mark and “What the…?” How can Java programmers talk about pointers and polymorphism when Sun Microsystems says, <em>We doan need no stinkin’ pointers!</em>? (There’s that phrase again—if you have no clue where it came from, you’re in for a treat when you watch the classic film, <em>The Treasure of Sierra Madre</em>.)</p>
<p><strong>Here’s the Deal</strong></p>
<p>Given the orientation of this blog (ActionScript 3.0 Design Patterns), we really ought to be able to nail the jelly to the tree—polymorphism being the jelly in this case. This is where you come in:</p>
<blockquote><p>Write a nice clear explanation of polymorphism so that a first-time OOP novice can understand it on an intuitive level as it relates to ActionScript 3.0 and  design patterns.</p>
</blockquote>
<p>We had our shot at it in Chapter 1, and while we didn’t blow it, we stuck to the technical side. We’d like to see something that has a visceral link between design patterns, polymorphism and ActionScript 3.0. These are the rules:</p>
<ul>
<li>Do not make it about you. This is for a bright person who may not have a huge technical background like you do.</li>
<li>No mention of Java. (Yes….I know I did, but that was <em>entre nous</em> and not for someone learning ActionScript design patterns and OOP for the first time.)</li>
<li>Explain pointers, polymorphism and their relationship to design patterns.</li>
<li>This is not an exercise in showing us how smart you are. Everyone who reads this blog is smart; so we know you’re smart. It’s about clarity so we don’t end up with one of those <strong>“This may be confusing”</strong> warning labels.</li>
<li>Provide an ActionScript 3.0 example. The example should show how polymorphism and design patterns are related.</li>
<li>No paraphrasing. Your explanation should be in your own words to someone you may eventually work with and needs a nice clear explanation.</li>
</ul>
<p>Now I don’t mind those comments that begin, “Bill, you’re as dumb as a box of rocks…”, but for this particular post, I hope to see something from <em>you</em> that you believe will help others. If you send in some rambling comment indicating that you’re a really serious programmer and know more than God, we’ll probably defer putting it on the blog. This is for others and clearly so. So I don’t care whether you learned design patterns day before yesterday or you’re an old hand. All I want is a nice clear explanation of why those learning design patterns and OOP should care about polymorphism and how it helps to become a better programmer.</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%2F12%2Factionscript-30-design-patterns-and-polymorphism-whats-the-point%2F&amp;title=ActionScript%203.0%2C%20Design%20Patterns%20and%20Polymorphism%3A%20What%E2%80%99s%20the%20Point%3F" id="wpa2a_6"><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/12/actionscript-30-design-patterns-and-polymorphism-whats-the-point/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

