<?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; OOP</title>
	<atom:link href="http://www.as3dp.com/category/oop/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>Parallel Programming on the Radar for Adobe</title>
		<link>http://www.as3dp.com/2011/04/parallel-programming-on-the-radar-for-adobe/</link>
		<comments>http://www.as3dp.com/2011/04/parallel-programming-on-the-radar-for-adobe/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 10:04:49 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Getters and Setters]]></category>
		<category><![CDATA[Multi-threaded Coding]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5270</guid>
		<description><![CDATA[In digging around for information on parallel programming I came across an Adobe post by Kevin Goldsmith from last September (2010). In the post Kevin encourages &#8220;academics&#8221; to heed the advice from Intel&#8217;s legions to &#8220;go parallel&#8221; citing the same article by Paul Steinberg I did in the previous post. This kind of post is [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5274" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/04/radar.png" alt="Where&#039;s the Beef?" title="radar" width="250" height="241" class="size-full wp-image-5274" /><p class="wp-caption-text">Where's the Beef?</p></div>In digging around for information on parallel programming  I came across an Adobe post by <a href="http://blogs.adobe.com/kevin-goldsmith/2010/08/parallelism-and-education-navigating-through-a-sea-of-cores-idf-panel-91310.html">Kevin Goldsmith</a> from last September (2010). In the post Kevin encourages &#8220;academics&#8221; to heed the advice from Intel&#8217;s legions to &#8220;go parallel&#8221; citing the same article by Paul Steinberg I did in the previous post. This kind of post is most encouraging for those who are interested in parallel programming. What is maddenly frustrating about Kevin&#8217;s article is that while  lots of tools are available  from Microsoft&#8217;s .NET framework, we don&#8217;t see anything like the tools or language structures or libraries or anything else that would allow ActionScript 3.0 developers to heed the advice that Kevin so freely gives. He acts as though Flash Builder can even do some simple multithreading.</p>
<p>Particularly galling was Kevin&#8217;s pontification exhorting &#8220;academics&#8221; to get cracking with parallel programming (citing Paul Steinberg) :</p>
<blockquote><p> If academia treats parallel programming and data structures like a specialized field of study, walled off into a couple courses and ignored by the general curricula, it will be doing a disservice to its students.</p></blockquote>
<p>First of all universities do teach parallel programming. A new colleague of mine who just received his computer science Ph.D. from Georgia Tech (a southern version of MIT or India&#8217;s IIT) knew all about parallel programming; so, it is something that already is taught in &#8220;academia.&#8221; (<em>That&#8217;s where the concepts were invented.</em>)</p>
<p>Second, if we heeded Kevin&#8217;s advice, we&#8217;d drop the fledgling ActionScript 3.0 courses you can find here and there in universities and go C# big time. That&#8217;s because C# (along with Visual Studio 2010) support parallel programming and other than &#8220;my own team’s Pixel Bender in Flash&#8221; that Kevin mentions, there&#8217;s very little support for parallel programming from Adobe—at least support that&#8217;s visible and usable like it is with C# and the .NET framework.</p>
<p>Third, while there&#8217;s a brisk discussion at OOPSLA (SPLASH) meetings of parallel programming, there&#8217;s no one from Adobe there. (I&#8217;ve given several presentations using ActionScript 3.0 in design patterns at these meetings but have never met anyone from Adobe—lots from Microsoft though.) So, while MAX is dandy for the Adobe fanboys, and Intel Developers Forums are great for rallying Intel multicore solutions for hardware, the software giants are at OOPSLA where design patterns were invented. You&#8217;ll find (like me) that if you&#8217;re the dumbest person at a conference, you can learn a lot.</p>
<p>Let me apologize for this mid-week rant, but like a lot of us who are huge supporters of ActionScript 3.0, it&#8217;s very frustrating to see languages like C# and Java (and even Python) support parallel programming and then read a post like Kevin&#8217;s telling us to &#8220;hop to it&#8221; while not having the developer tools to do so. Even worse, if we took Kevin&#8217;s advice seriously, we&#8217;d probably use C# and C++ on the .NET framework and kiss ActionScript 3.0 goodbye.</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%2F2011%2F04%2Fparallel-programming-on-the-radar-for-adobe%2F&amp;title=Parallel%20Programming%20on%20the%20Radar%20for%20Adobe" 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/2011/04/parallel-programming-on-the-radar-for-adobe/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Getters and Setters: Who&#039;s coming down Your Chimney?</title>
		<link>http://www.as3dp.com/2010/12/actionscript-3-0-getters-and-setters-whos-coming-down-your-chimney/</link>
		<comments>http://www.as3dp.com/2010/12/actionscript-3-0-getters-and-setters-whos-coming-down-your-chimney/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 19:28:30 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Getters and Setters]]></category>
		<category><![CDATA[accessors]]></category>
		<category><![CDATA[getters setters]]></category>
		<category><![CDATA[mutators]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4207</guid>
		<description><![CDATA[Chimneys Allow Access The other day I was working on some getters and setters to capture and encapsulate data passed from an input source. I&#8217;m most cognizant of using getters and setters for incoming data so that I have these nice encapsulated chunks of coded properties. The reason behind encapsulation is to keep properties from [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4215" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/santa.png" alt="Chimney allows access to house object" title="santa" width="250" height="313" class="size-full wp-image-4215" /><p class="wp-caption-text">Chimney allows access to house object</p></div><strong>Chimneys Allow Access</strong></p>
<p>The other day I was working on some getters and setters to capture and encapsulate data passed from an input source. I&#8217;m most cognizant of using getters and setters for incoming data so that I have these nice encapsulated chunks of coded properties. The reason behind encapsulation is to keep properties from getting changed by other elements in a program unwittingly. So getters and setters became a recommended way of going about setting up data. (The terms <em>mutators</em> (setters)  and <em>accessors</em> (getters) are often used to describe the same thing, but I can&#8217;t think of mutators without images of zombies, so I just use <em>getters</em> and <em>setters</em> like everyone else—conformist that I am.)</p>
<p>To be honest,  most of the programs that I write using design patterns don&#8217;t have getters and setters <em>per se</em>, and I rarely use the ActionScript 3.0 <strong>get</strong> and <strong>set</strong> methods. Typically, a well-designed program has no need for explicit getters and setters. Nevertheless, on page 20 of our book, Chandima and I dutifully  write about getters and setters as being a mundane aspect of OOP and provide several examples. However, over the years, I&#8217;ve seen a growing number of articles about the dark side of getters and setters. The issue is around what getters and setters were meant to do and how they have been misused.</p>
<p>If you think of a house as an object, and the doors as public access, both the opportunities and problems with object access become more apparent. Also, you have places where entry is possible but not intended—such as down the chimney.  Anyway, I was moved to pen a season-appropriate missive to shed some light on the issue:</p>
<p><strong>The Night Before OOP-SLA</strong></p>
<p>&#8216;Twas the night before OOPSLA, when all through the watt<br />
Not a coder was stirring, not even a bot;<br />
The downloads were placed on the server with care,<br />
In hopes that St. Singleton soon would be there;<br />
The young geeks were nestled all snug by their screens,<br />
While visions of killer apps arose from their dreams;<br />
And mamma with her iMac, and I with my Dell,<br />
Had just settled down to write code for a spell,<br />
When out on the net there arose such a clatter,<br />
I sprang from my chair to see what was the matter.<br />
Away to the keyboard I cranked up my Flash,<br />
Tore open the folders and emptied the trash.<br />
The apps on the iPod all came alive<br />
Gave the lustre of update to objects on-drive,<br />
When, what on my screen came clear in my glasses,<br />
But a miniature program, and eight tiny classes,<br />
With a little old hacker, so fat like a pork,<br />
I knew in a moment it must be St. Dork.<br />
More rapid than skip lists his coursers they came,<br />
And he whistled, and shouted, and called them by name;<br />
&#8220;Now, Factory! now, Context! now, Product and State!<br />
On, Flyweight! on Proxy! on, Builder, Template!<br />
To the top of the dock! to the top of the sprawl!<br />
Now refractor! refractor! refractor all!&#8221;<br />
As bits wrapped in packets over the routers do fly<br />
When they meet with an obstacle they return to retry,<br />
So up to the server the coursers they flew,<br />
With program dependencies, errors, and St. Singleton too.<br />
And then, in a twinkling, I picked up my mug<br />
Full of java to seek out each little bug.<br />
As I drew in my mouse, and was clicking around,<br />
Down the accessor St. Singleton came with a bound.<br />
He was messing with properties and methods to boot,<br />
My encapsued objects were wrecked to the root;<br />
A bundle of types he had flung on his back,<br />
And he looked like a newbie overloading the stack.<br />
His eyes &#8212; how they glazed! his pimples how icky!<br />
His cheeks were like pizzas, his nose was still sticky!<br />
His droll little mouth had precious few graces,<br />
And when he smiled I was blinded by braces;<br />
The stump of a pencil shown up from his pocket protector,<br />
He stumbled around like Clouseau the inspector;<br />
He had a broad face and was fat as a pie,<br />
When he walked he looked like the Comic Book Guy.<br />
He was chubby and plump, a right jolly old geek,<br />
And I laughed when he tried to give my pattern a tweak;<br />
A wink of his eye and a blast of hot gas,<br />
Soon gave me to know that this guy was crass<br />
He spoke not a word, but started hacking his work,<br />
And filled all the memory; then turned like a jerk,<br />
And laying his finger straight up his nose,<br />
And giving a burp, up the access he rose;<br />
He sprang to his program, to his team issued a command,<br />
And the whole thing crashed like a wave on the sand,<br />
But I heard him exclaim, ere he limped out of sight,</p>
<blockquote><p>&#8220;Happy Coding to all and to all a good night.&#8221;</p></blockquote>
<p>Happy Holidays from Bill and Chandima&#8230;</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%2F2010%2F12%2Factionscript-3-0-getters-and-setters-whos-coming-down-your-chimney%2F&amp;title=ActionScript%203.0%20Getters%20and%20Setters%3A%20Who%26%23039%3Bs%20coming%20down%20Your%20Chimney%3F" 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/2010/12/actionscript-3-0-getters-and-setters-whos-coming-down-your-chimney/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>SPLASH Dispatch: OOPSLA&#039;s new Name</title>
		<link>http://www.as3dp.com/2010/10/splash-dispatch-oopslas-new-name/</link>
		<comments>http://www.as3dp.com/2010/10/splash-dispatch-oopslas-new-name/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 11:11:50 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3887</guid>
		<description><![CDATA[I just got back from the SPLASH (OOPSLA) conference in Reno. To be honest, I wasn&#8217;t looking forward to it because I had to present a paper to the Educational Symposium, and just about everybody there had a Ph.D. in computer science and I was going to be eaten alive. Besides, the usual group I [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3886" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/10/splash.png" alt="OOPSLA Conference" title="splash" width="250" height="113" class="size-full wp-image-3886" /><p class="wp-caption-text">OOPSLA Conference</p></div> I just got back from the SPLASH (OOPSLA) conference in Reno. To be honest, I wasn&#8217;t looking forward to it because I had to present a paper to the Educational Symposium, and just about everybody there had a Ph.D. in computer science  and I was going to be eaten alive. Besides, the usual group I do things with (<em>Killer Examples</em>) had decided not to hold their usual session and  I didn&#8217;t know anyone in the Educational Symposium. To give you an idea of the session I was in; here are some topics:</p>
<ul>
<li>The impending ordinariness of teaching concurrent programming</li>
<li>Mutation Analysis vs. Code Coverage in Automated Assessment of Students&#8217; Testing Skills</li>
<li>Compiler Construction With a Dash of Concurrency and an Embedded Twist </li>
<li>Learning OOP with Dynamic Languages: Adding Concrete Strategies to a PHP Strategy Design Pattern</li>
<li>Manifesto: a New Educational Programming Language</li>
</ul>
<p>When someone is talking about the &#8220;ordinariness&#8221; of &#8220;concurrent programming&#8221; and I had no idea what <em>concurrent programming</em> was, I was a little rattled. However, everything was so interesting that I soon forgot about my own concerns and really got into these other topics. The big deal that hit me between the eyes was concurrent and parallel programming. (One undergraduate student had a presentation on CUDA, which is used in learning concurrent programming—it took him three hours to crank out &#8220;Hello World!&#8221;) When my turn came, it worked out well, and I survived and even had a good time. The presentation after mine was by three guys who were discussing the process of developing a new language called <em>Grace</em>. SPLASH has a humbling effect. (Click the link below and learn how I met one of the Gang of Four.)<br />
<span id="more-3887"></span><br />
<strong>Parallel/Concurrent Programming</strong></p>
<p>For an introduction to Parallel programming and Design Patterns I was advised to look up what Ralph Johnson (one of the Gang of Four) had to say about it. I found the <a href="http://www.infoq.com/interviews/Ralph-Johnson-Parallel-Programming-Patterns">link</a> to a talk Johnson had and now I&#8217;m hoping to see an ActionScirpt implementation. If anyone has some tips on any ActionScript 3.0 efforts to introduce concurrent/parallel programming into Flex (Flash Builder) and ActionScript I&#8217;d be very interested. Maybe there&#8217;s some way to jury-rig a multi-threaded program in ActionScript 3.0. If anyone has more information on this topic or a reference, please pass it along. For all I know, there might be something for concurrent programming with ActionScript 3.0 already.</p>
<p><strong>Rumblings</strong></p>
<p>One of the more persistent themes that I picked up at the conference was an ongoing dissatisfaction with Java. It seems to be the dominant language taught in CS programs, and as it has evolved, it has become more cumbersome more difficult to teach, the libraries have multiplied and the demands keep getting greater. Oddly, those same reasons were given for replacing C++ with something leaner, meaner and less cumbersome—Java. I wonder if ActionScript will grow to the point where it becomes so bloated as to become a <em>Jabba the Hutt</em> language. I hope not.</p>
<p>I had the pleasure of meeting <a href="http://www.as3dp.com/2010/05/05/a-funny-thing-happened-on-the-way-to-skip-lists-the-actionscript-3-0-binary-search-algorithm/">William Pugh, author of skip lists</a>. Like most creative thinkers, Bill has moved on to new challenges but seemed happy to hear that his work on skip list was appreciated.</p>
<p>One Canadian study that cropped found that learning programming languages in CS programs was fairly well-balanced between those who had no experience in programming and those who had at least some. One supposition was that many with programming experience had to unlearn bad habits and relearn OOP programming and problem solving. So for those of you struggling with OOP with little programming experience, you may be better off than those who have had do-it-yourself training in programming. You don&#8217;t have to unlearn bad habits.</p>
<p><strong>My 4am Chat with Ralph Johnson</strong></p>
<p>For some years now, I&#8217;ve been an early riser—about 4am most days. At the SPLASH conference, which is three time-zones later, it felt like midday getting up at 4am. However, old habits die hard, and I was sitting in the conference area where the wifi was strongest preparing this post for this blog. Other than a cleaning crew, I was the only person there. However, shortly after beginning to write the post, another guy walks in with a cup of what I thought might be coffee and asked him if some were available, and he said it&#8217;d be ready about 6am and came over to chat. It turned out that he too was an early riser, and I found out that he was from the University of Illinois. So I inquired whether he knew Ralph Johnson of the Gang of Four. He said, &#8220;I <em>am</em> Ralph Johnson.&#8221; It turns out that Johnson in addition to being the habitual early riser is quite the raconteur, and for the next couple of hours I learned that his interest in parallel programming went back  his doctoral dissertation and how SmallTalk was almost accidentally used in presenting  design patterns&#8211;plus more tidbits about programming, inventions and personalities in computing than I can begin to remember. What struck me most was that he didn&#8217;t seem to be selling his ideas and certainly not design patterns. Design patterns are for creating reusable code and some people used them and others didn&#8217;t. Right now he&#8217;s interested in  <a href="http://www.infoq.com/interviews/Ralph-Johnson-Parallel-Programming-Patterns">patterns for parallel programming</a>, and his current focus is in that area. Anyway, that&#8217;s why this post was delayed. When I started writing it, one of the Gang of Four happened to come along.</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%2F2010%2F10%2Fsplash-dispatch-oopslas-new-name%2F&amp;title=SPLASH%20Dispatch%3A%20OOPSLA%26%23039%3Bs%20new%20Name" 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/2010/10/splash-dispatch-oopslas-new-name/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>&quot;That&#039;s Not the Way I&#039;d Do It&quot;: Justifiable Homicide in 32 States</title>
		<link>http://www.as3dp.com/2010/01/thats-not-the-way-id-do-it-justifiable-homicide-in-32-states/</link>
		<comments>http://www.as3dp.com/2010/01/thats-not-the-way-id-do-it-justifiable-homicide-in-32-states/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:43:46 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2352</guid>
		<description><![CDATA[The other day I was worrying about a comment another programmer had made. After looking at some code, he twisted up his face and said, &#8220;That&#8217;s not the way I&#8217;d do it.&#8221; He went on to tell me how he&#8217;d do it. After thinking about it a couple of days I realized that the way [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2359" class="wp-caption alignleft" style="width: 235px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/tntwidi.png" alt="That&#039;s Not The Way I&#039;d Do It" title="tntwidi" width="225" height="271" class="size-full wp-image-2359" /><p class="wp-caption-text">That's Not The Way I'd Do It</p></div>The other day I was worrying about a comment another programmer had made. After looking at some code, he twisted up his face and said,</p>
<blockquote><p>&#8220;That&#8217;s not the way I&#8217;d do it.&#8221;</p></blockquote>
<p>He went on to tell me <em>how he&#8217;d do it</em>. After thinking about it a couple of days I realized that the way he&#8217;d do it, while not wrong, wasn&#8217;t any better than what was originally there. In grousing about it, the chairman of the Computer Science Department remarked,</p>
<blockquote><p> Yeah, that&#8217;s just like a programmer.</p></blockquote>
<p>While that made me feel better, it was also aggravating. Out of sheer habit, what compels programmers to be so petty? I don&#8217;t mind a <em>little</em> one-upmanship, but some discussions need to be about <em>finding a better solution</em> and not forever trying to tell someone how smart you are. (By the way, this isn&#8217;t the first time I&#8217;ve mentioned this problem in a post, but I&#8217;d like this to be an invitational rant and get some reader comments.)</p>
<p>I&#8217;ve worked with people in a lot of different fields, and while some pettiness can be found in just about all professions, <strong><em>We&#8217;re Number One!</em></strong> when it comes to the <em>That&#8217;s not the way I&#8217;d do it</em> (TNTWIDI—pronounced <em><strong>tin-widi</strong></em> with a short &#8220;i&#8221;) syndrome.<br />
<span id="more-2352"></span><br />
<strong>Those of us who live in Glass Houses&#8230;.</strong></p>
<p>For someone who writes a blog about how to program better using OOP and Design Patterns, I&#8217;m on pretty thin ice when it comes to grumbling about telling people how to program. Furthermore, most of the comments we get are extremely helpful—let&#8217;s face it, we&#8217;ve got a pretty smart readership. (That&#8217;s due to the topic—not necessarily Chandima and me!) However, at the same time, we get snippish little missives now and again that reflect TNTWIDI, and because we take all comments seriously (unless they&#8217;re trying to peddle something), we find it a little aggravating to waste time on what amounts to a bad habit among programmers.</p>
<p>At the same time that we get TNTWIDI, the great majority of comments we get are excellent. For example, in a series of recent interchanges with Timo concerning the Flyweight pattern, we more or less concluded that Timo may have stumbled upon a new design pattern. Likewise, Alan had pointed out a mess I made of certain agile concepts and resulted in my changing a post. Chandima&#8217;s posts on Pure MVC has led to a huge number of great comments and given us direction for post content. So, while TNTWIDI is a problem, let&#8217;s remember that most of the comments we get are great.</p>
<p><strong>Helpful Comments: Bomb Defusing 101</strong></p>
<p>Imagine you&#8217;re working with a number of people to solve problems in defusing bombs. Some of the people have a lot of experience defusing bombs and have studied the chemistry and engineering of bomb-making extensively. Others less so, and still others have no experience and have read little about the topic. Now everybody in the group wants to diffuse bombs successfully. If someone makes a TNTWIDI comment, however, they have to use their method to defuse the bomb.</p>
<p>Such a system would do two things: The TNTWIDI&#8217;s would get blown up in short order, and the rest of us would think carefully before we shot off our mouths just to show the group how smart we were. However, there&#8217;s a danger in such a practice—it would silence the less experienced. For example, a novice member of the group may ask, &#8220;What about a mercury fuse?&#8221; Suppose, no one in the bomb defusing group had considered that and a mercury fuse was exactly what a particular bomb used. They&#8217;d get blown up because they limited discussion.</p>
<p><strong>Let&#8217;s All Get Better</strong></p>
<p>Not too long ago I read an article in which an MIT professor of computer programming/engineering remarked that a certain process that saved a few cycles in the time it took to execute a program was a waste of time. The reason was that it took a huge number of man-hours of programming to save a few milliseconds. In other words the savings would be so small that no one using the program would notice. However, it might take an extra week or month to create such a program. In short, the argument was that many are still acting like we&#8217;re using 8-bit processors and 48k of RAM when we&#8217;re working with 64-bit processors and RAM measured in gigabytes.</p>
<p>Those particular remarks resonated with me because design patterns focus on human development as much as they do on computer processing. In fact, other than the Flyweight, I cannot think of a single design pattern that speeds up processing. What many fail to understand about design patterns, and why I believe they are so difficult, is that <strong>the focus is on relationships between classes</strong>. That is the reason that the symbols in the <strong>class diagrams</strong> are all about describing relationships. If you compare a flowchart with a class diagram, instead of flow control, you see a description of class relationships—or put otherwise—<em>the relationship between participants</em> (those classes, interfaces and subclasses that make up the pattern.)</p>
<p>If our discussions can better focus on relationships, and if we think of relationships when attempting to understand a particular design pattern, we&#8217;ll be on the right track. This does not mean that we should not discuss coding. Far from it.  Rather, when discussing coding we need to focus on how the code sets up or expresses a given relationship. We need to ask,</p>
<blockquote><p>Does this code best reflect a given relationship?</p></blockquote>
<p>You may well ask, <em>How do we determine whether the code reflects the relationship?</em> If that question came to mind; then you&#8217;re on the right track.</p>
<p><strong>Let&#8217;s hear from you</strong></p>
<p>What are your thoughts about moving ahead in 2010 and beyond with design pattern. Let&#8217;s hear from you! Don&#8217;t worry, there&#8217;s no possible TNTWIDI for this kind of post!</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%2F2010%2F01%2Fthats-not-the-way-id-do-it-justifiable-homicide-in-32-states%2F&amp;title=%26quot%3BThat%26%23039%3Bs%20Not%20the%20Way%20I%26%23039%3Bd%20Do%20It%26quot%3B%3A%20Justifiable%20Homicide%20in%2032%20States" id="wpa2a_8"><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/2010/01/thats-not-the-way-id-do-it-justifiable-homicide-in-32-states/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>OOPSLA 2009: Hope to See Some ActionScripters!</title>
		<link>http://www.as3dp.com/2009/10/oopsla-2009-hope-to-see-some-actionscripters/</link>
		<comments>http://www.as3dp.com/2009/10/oopsla-2009-hope-to-see-some-actionscripters/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 19:18:27 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1936</guid>
		<description><![CDATA[Tomorrow morning bright and early I&#8217;m off to Orlando, Florida for the 2009 Object Oriented Programming and System Languages Association (OOPSLA) annual meetings. I&#8217;ll be making an ActionScript 3.0 presentation in a workshop on Sunday. Our workshop is named &#8220;Good Examples for Exposing Bad Practice&#8221; and meets in Pastoral 3 from 8:30-5:00 (Oct 25) Mine [...]]]></description>
			<content:encoded><![CDATA[<p>Tomorrow morning bright and early I&#8217;m off to Orlando, Florida for the 2009 Object Oriented Programming and System Languages Association (OOPSLA) annual meetings. I&#8217;ll be making an ActionScript 3.0 presentation in a workshop on Sunday. Our workshop is named &#8220;Good Examples for Exposing Bad Practice&#8221; and meets in Pastoral 3 from 8:30-5:00 (Oct 25) Mine is based on the &#8216;Wrong Way Warrior&#8221;; so it should be familiar.  On Monday, I&#8217;ll be at the Educators Symposium all day, and I would really like to meet other ActionScripters who might be at the conference</p>
<p>Here are some other speakers you might want to hear/meet:<br />
<strong>1. Miško Hevery</strong><br />
 <em>Automatic Dependency Injection In The Land Of Dynamic Languages</em></p>
<p><strong>2. Barbara Liskov</strong> (Liskov Principle)<br />
 <em>Keynote Speaker</em></p>
<p><strong>3. Ralph Johnson</strong> (GoF)<br />
 <em>Regrowing a Language: Refactoring Tools Allow Programming Languages to Evolve</em></p>
<p>Anyway, just in case any of you will be there, drop by one of the sessions. Everyone&#8217;s been very accepting of ActionScript 3.0, and it&#8217;d be fun to chat with some fellow ActionScripters!</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%2F10%2Foopsla-2009-hope-to-see-some-actionscripters%2F&amp;title=OOPSLA%202009%3A%20Hope%20to%20See%20Some%20ActionScripters%21" id="wpa2a_10"><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/10/oopsla-2009-hope-to-see-some-actionscripters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mansions in the Slum: The Case of Beautiful Algorithms and Disappointing Designs</title>
		<link>http://www.as3dp.com/2009/10/mansions-in-the-slum-the-case-of-beautiful-algorithms-and-disappointing-designs/</link>
		<comments>http://www.as3dp.com/2009/10/mansions-in-the-slum-the-case-of-beautiful-algorithms-and-disappointing-designs/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 14:28:13 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Algorithms]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1920</guid>
		<description><![CDATA[Some years back when I was flying with Flying Samaritans, I was invited to a party for a fellow pilot in Beverly Hills, California. Since the party involved a good deal of celebration, I had to stay over—no way I was going to fly back to San Diego that evening. The house we stayed in [...]]]></description>
			<content:encoded><![CDATA[<p>Some years back when I was flying with Flying Samaritans, I was invited to a party for a fellow pilot in Beverly Hills, California. Since the party involved a good deal of celebration, I had to stay over—no way I was going to fly back to San Diego that evening. The house we stayed in was a classic early California-Spanish design; a beauty in every way. The next morning, bright and early, I went for a walk along well-tended streets where no pothole or crack in the pavement dared to show itself. The streets were immaculate, as were the sidewalks and everything between the houses along and off Beverly Drive.<br />
<span id="more-1920"></span><br />
<strong>A Humpty Dumpty Site</strong></p>
<p>The other day, I was helping a group who was given a Web site to revive with Flash. The original site was done with PHP, and as I looked at the code, I was struck by the fact that the developer had written some beautiful algorithms in PHP and demonstrated a masterly control over PHP syntax. At the same time, I was struck by the fact that this Humpty Dumpty Web site couldn&#8217;t be revised by all the kings horses and all the kings men. A change in one part caused another part to fold and crumble. It was a true house of cards, albeit made with some beautiful cards.</p>
<p>That&#8217;s when the metaphor of <em>Mansions in the Slum</em> popped in my head. A lot of developers want to be better at what they develop, and yet while their algorithms shimmer and glow (like a big gorgeous mansion), the relations between the classes and their encapsulated algorithms are cluttered with junk, weeds or are wholly non-existent.  When first encountering <strong>Design Patterns</strong> a lot of developers reasonably ask, <em>How do design patterns make my programs run faster and better?</em>. With few exceptions, such as the <em>Flyweight Pattern</em>, they answer is,<br />
<blockquote>They don&#8217;t.</p></blockquote>
<p> Design patterns help clear the junk and weeds out of the streets between the classes that have been so painstaking developed. Further, design patterns facilitate application maintenance. If you need a little maintenance work in Beverly Hills, it&#8217;s easy to get the repair crew in there to do the work, and the house next door isn&#8217;t going to collapse when the repair is complete. The community is kept in good shape by good maintenance and a good system of communication. If for some reason, no maintenance was able to get to the homes in Beverly Hills, in short order, the place would deteriorate. However, because of the attention given to the streets that connect all of the mansions in  Beverly Hills, we can expect it to be an area of well-maintained homes for the foreseeable future.</p>
<p>So when designing an application, Web site or other program, don&#8217;t just think in terms of building great classes with slick algorithms. Think in terms of creating a <em>community of classes</em> where every class can easily relate to other classes but maintain a loose relationship. Not only will that help you maintain your work, but it will help you appreciate and understand what design patterns are all about. The fact that the patterns already have the class relations laid out for you just makes it all that much easier.</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%2F10%2Fmansions-in-the-slum-the-case-of-beautiful-algorithms-and-disappointing-designs%2F&amp;title=Mansions%20in%20the%20Slum%3A%20The%20Case%20of%20Beautiful%20Algorithms%20and%20Disappointing%20Designs" id="wpa2a_12"><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/10/mansions-in-the-slum-the-case-of-beautiful-algorithms-and-disappointing-designs/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OOP for Artists: The Empowerment of ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/09/oop-for-artists-the-empowerment-of-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/09/oop-for-artists-the-empowerment-of-actionscript-30/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 02:26:18 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[OOP for Artists]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1496</guid>
		<description><![CDATA[In a recent post I voiced my admiration for artists, designers and animators but noted that they seem to have been left out in the cold with ActionScript 3.0. I added a little helper statement not in the ActionScript 3.0 documentation—MovieClip.addFrameScript(). The idea was to encourage artists not to be too hasty in giving up [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/09/ooprosie150.png" alt="OOP for Artists" title="OOP for Artists" width="150" height="194" class="alignleft size-full wp-image-1627" /></p>
<p> In a <a href="http://www.as3dp.com/2009/08/25/artists-animators-and-actionscript-30/">recent post I voiced my admiration for artists</a>, designers and animators but noted that they seem to have been left out in the cold with ActionScript 3.0. I added a little <em>helper statement</em> not in the ActionScript 3.0 documentation—<strong>MovieClip.addFrameScript()</strong>. The idea was to encourage artists not to be too hasty in giving up on coding altogether.</p>
<p>Quite frankly, I was surprised by the number of comments we received on that post. I didn&#8217;t think artists bothered with our kind of discussions, and was more than a little  gratified to find that some of our readers identified with the issues discussed. So  I started thinking about a series of posts for helping artists.</p>
<p>I didn&#8217;t want to do a &#8220;dumbed down&#8221; ActionScript 3.0 for artists; so I opted for an approach that would cover the same principles that we&#8217;ve discussed throughout the life of this blog. However, I would move more deliberately and touch more bases—especially the basics of OOP. Further, I decided to use video and take advantage of the new Quicktime Player that comes with Screen Sharing. So, I created a simple class to start things off, and put it in an .f4v file (H.264 format) and you can download it by clicking the download button: <a href="http://nemo.mwd.hartford.edu/~wsanders/oopart/"><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/09/smalldownload.jpg" alt="smalldownload" title="smalldownload" width="150" height="50" class="alignnone size-full wp-image-1908" /><br />
</a> You will need an <a href="http://www.adobe.com/products/mediaplayer/">Adobe Media Player</a> that is free to download. I did not include any .fla files because I&#8217;d have to put in at least two because some have  CS3 and others CS4; so you&#8217;ll have to use your own .fla files. Each video is short and will play full screen using the Adobe Media Player. The only thing I need is feedback to let me know whether this kind of thing is helpful or not. I will be focusing on graphics and loading graphics, but I welcome ideas from one and all.</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%2F09%2Foop-for-artists-the-empowerment-of-actionscript-30%2F&amp;title=OOP%20for%20Artists%3A%20The%20Empowerment%20of%20ActionScript%203.0" id="wpa2a_14"><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/09/oop-for-artists-the-empowerment-of-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Artists, Animators and ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/08/artists-animators-and-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/08/artists-animators-and-actionscript-30/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 21:44:25 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1380</guid>
		<description><![CDATA[Artists and Graphic Designers For me, graphic designers and artists are angels. No matter how I try, I can only get so far in graphic design. Tools like clip art, templates, and Kuler help me achieve not awful , but that’s it. (I can even screw up clip art.) So, for anything serious, I’ve got [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Artists and Graphic Designers</strong></p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/designer-295x300.png" alt="designer" title="designer" width="295" height="300" class="alignleft size-medium wp-image-1381" /></p>
<p>For me, graphic designers and artists are angels. No matter how I try, I can only get so far in graphic design. Tools like clip art, templates, and Kuler help me achieve <em>not awful </em>, but that’s it. (I can even screw up clip art.) So, for anything serious, I’ve got to work with graphic artists.  That’s no problem—I like working with angels.</p>
<p>Some graphic artist have made the transition to some version of ActionScript, but with ActionScript 3.0 most complained that they were getting left behind. Early Flash had few ActionScript options and a system for entering code that didn’t require any programming background at all. With ActionScript 2.0, things got better for developers, but designers started voicing concern over increased complexity. With ActionScript 3.0 and the loss of the ability to put code into buttons and MovieClip objects directly, some graphic artists became furious with Flash over what they saw as a betrayal. It was like a carload of kids on the way to do something fun ditched the artists and designers on the roadside.<br />
<span id="more-1380"></span><br />
Other than a handful of developer/designers, most designers aren’t going to become competent with ActionScript 3.0, let alone OOP or design patterns—just like I’m never going to be a competent designer or artist. So I was thinking an interesting project for this blog would be to develop some “Clip Code” for artists. However, I’m not sure what kind of code they’d need. Most of the “Clip Art” that I use is from photographs, but while I know the kind of artwork I need, I don’t know what kind code graphic designers need.</p>
<p>In the same way that I don’t want to have shoddy graphics or photographs (I can do that myself), the “Clip Code” I have in mind is grounded in good but simple design patterns and OOP that artists can use. So, I’d like to throw that one out to the readers of this blog for ideas of what would be useful to graphic designers.</p>
<p><strong>Animation and Dante’s 10th Circle of ActionScript Hell</strong></p>
<p>We all remember from our Italian Literature course that Dante Alighieri’s <em>Divine Comedy</em> describes hell as having nine circles—the last circle being reserved for treason. Were Dante an ActionScript developer, he’d have a 10th Circle for those animators who spread 50 little ActionScript code blocks all over the timeline in different frames and on different layers. (In the last mention of Dante’s inferno on this blog, we only got to the 9th circle.)</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/animator-295x300.png" alt="animator" title="animator" width="295" height="300" class="alignleft size-medium wp-image-1382" /></p>
<p>Don’t get me wrong. I love animation, and in fact I got Flash initially so that I could do animations on the Web. When first working with Flash, I ran out and got Tony White’s <em>The Animator’s Workbook: Step-by-Step Techniques of Drawn Animation</em>, and I was delighted with what Flash could do following traditional animation techniques. (Joe Cartoon is still one of my heroes.)</p>
<p>What drives me nuts is using animations for some little effect that will be forgotten after one or two views that screw up an entire application. I’m convinced that animation is the <em>sine qua non</em> of Flash, and so I would never suggest that it be reduced in any way. Rather, I think that animation in movie clips needs to be controlled outside of the confines of code embedded in frames and the timeline. For example, pressing the green Play button shows a simple movie clip added to an existing fire-the-weapon animation:</p>
<p><a href="http://www.sandlight.net/testBench/weapon/"  target="_blank"><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /><br />
</a></p>
<p>The relevant code for the operation is the following:</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('p1380code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13802"><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
</pre></td><td class="code" id="p1380code2"><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;">media</span>.<span style="color: #0066CC;">Sound</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #006600;">SoundChannel</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</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;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</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> m249:<span style="color: #0066CC;">MovieClip</span>=<span style="color: #000000; font-weight: bold;">new</span> M249<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> mc:<span style="color: #0066CC;">MovieClip</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> weapon:SoundChannel;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;M249.mp3&quot;</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>
			m249.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,fireWeapon<span style="color: #66cc66;">&#41;</span>;
			m249.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">50</span>,m249.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">60</span>;
			addChild<span style="color: #66cc66;">&#40;</span>m249<span style="color: #66cc66;">&#41;</span>;
			m249.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</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> fireWeapon<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>
			playOnce<span style="color: #66cc66;">&#40;</span>m249<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> urlReq:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> gun:<span style="color: #0066CC;">Sound</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			gun.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlReq<span style="color: #66cc66;">&#41;</span>;
			weapon = gun.<span style="color: #0066CC;">play</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> playOnce<span style="color: #66cc66;">&#40;</span>mc:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>=mc;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>.<span style="color: #006600;">addFrameScript</span><span style="color: #66cc66;">&#40;</span>mc.<span style="color: #006600;">totalFrames</span>-<span style="color: #cc66cc;">1</span>,reset<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> reset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</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>The  <strong>m249</strong> instance is of a movie clip that uses a shape tween to simulate muzzle flashes. (The sound routine was borrowed from <a href="http://www.as3dp.com/2009/08/19/wrong-way-warrior-getting-flexibility-with-design-patterns—part-ii/">another application</a>.)</p>
<p>Initially, the problem I encountered, and I am sure all Flash animators encounter, is the requirement of keeping at least <em>some code</em> on the timeline. After a few false starts, I was able to control it with relatively simple functions with <em>no timeline code</em>. The above code simply represents the possibility of allowing animators to do whatever they want while not worrying about code.</p>
<p>Thanks to Steve Mathews and Colin Holgate who introduced me to the undocumented <strong>MovieClip.addFrameScript()</strong>, I was able to stop the looping movie clip after a single iteration. My initial solution used <strong>Event.ENTER_FRAME</strong>, but I liked that offered up by Steve and Colin better. Anyway, there’s a good post on <strong>addFrameScript()</strong> at <a href= "http://troyworks.com/blog/2007/09/22/as3-movieclipaddframescript/"> Troy Gardner’s blog</a> if you’d like to learn more about it. Like all undocumented code, I’m often at a loss to understand why it’s undocumented.</p>
<p>The huge advantage of an approach that is not in anyway tied to the timeline is <em>re-usability</em>. If all of the coding is external to the development of the movie clip with the animation, there’s less worry about getting tangled up in unknown code lurking on the timeline. You can reuse the animating movie clips with the assurance that what you see is what you get and you can add code without tangling it up with code tucked away in some unseen layer on an unseen frame.</p>
<p>For complex animations, planning is essential, and I’d like to find out what animators would find useful included in the examples and projects examined on this blog. Like artist, animators work is some kind of context and I’d like to refactor animations so that <em>no timeline code</em> is included in the application of the animations.</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%2F08%2Fartists-animators-and-actionscript-30%2F&amp;title=Artists%2C%20Animators%20and%20ActionScript%203.0" id="wpa2a_16"><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/08/artists-animators-and-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: The Open/Closed Principle</title>
		<link>http://www.as3dp.com/2009/03/design-pattern-principles-for-actionscript-30-the-openclosed-principle/</link>
		<comments>http://www.as3dp.com/2009/03/design-pattern-principles-for-actionscript-30-the-openclosed-principle/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 15:37:13 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=836</guid>
		<description><![CDATA[In the little AIR menu with the 10 principles one of the clearest is the Open/Closed Principle. At one time this principle suggested that all updates be created using an implementation or extension of virtually any class. That could get tricky, especially if someone understood that to mean implementing an update or extending a subclass. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-760" title="bucketrule" src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule.png" alt="bucketrule" width="150" height="133" /><br />
In the little <a href="http://www.as3dp.com/2009/02/26/oop-designs-pattern-principles-ready-for-work"> AIR menu with the 10 principles</a> one of the clearest is the <strong>Open/Closed Principle</strong>. At one time this principle suggested that all updates be created using an implementation or extension of virtually any class. That could get tricky, especially if someone understood that to mean implementing an update or extending a subclass. However, later, the extension came to mean the extension of an <em>abstract base class</em>. In other words the interface is extended but never modified. Given these caveats, we can understand the basic principle as it is now understood:</p>
<blockquote><p>Classes should be open for extension but closed for modification.</p></blockquote>
<p><strong>Easy to Take to Work</strong></p>
<p>(<strong>Note:</strong> In talking about a program and changes, we’re not including the Client class. It just makes requests, and you can add requests and change them all you want in the Client.)</p>
<p>The idea that when you want to change a program, the only way you are able to make changes is by extension may seem a little restrictive. However, what the principle is really doing is providing a way to make changes without having to rewrite the whole program. The dictum, <em>New behaviors are only available through extension</em> <strong>should not</strong> be phrased in a way to make it sound like it&#8217;s tying your hands. Rather, it should say something like,</p>
<blockquote><p>Hey! The Open/Closed principle makes it easy to add new behaviors without having to mess up your whole program.</p></blockquote>
<p><span id="more-836"></span><br />
Looking at the principle in this new light, we can see how it works with a simple example. Suppose you’re doing a lot of work with Shape class objects. Right away you need circles and rectangles, but you might need other shapes later. So to get started, you set up your classes that you know you’ll need, but you want to leave the door open for easy extension. To get started you create an abstract class that leaves an abstract method to add different shape types. The following script is your initial starting point:</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('p836code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8369"><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
</pre></td><td class="code" id="p836code9"><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;">Shape</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;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Asbstract class do not instantiate</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IOpenClosed <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> shape:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&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> setShape<span style="color: #66cc66;">&#40;</span>fillCol:uint,borderCol:uint,lw:<span style="color: #0066CC;">Number</span>,rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fillCol<span style="color: #66cc66;">&#41;</span>;
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span>lw,borderCol<span style="color: #66cc66;">&#41;</span>;
			makeShape<span style="color: #66cc66;">&#40;</span>rw,h,c<span style="color: #66cc66;">&#41;</span>;
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>shape<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 will notice that the makeShape() function in the setShape() method only has three parameters. That’s because the first two parameters are the x and y positions. These will be set to 0 using a literal. The stage position is placed using an instance of the shape.</p>
<p>Next, two little classes extend the abstract class to make the circle and rectangle.  First the circle-making class extends the abstract class and then overrides the makeShape() function to add the graphics method for creating a circle .</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('p836code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83610"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p836code10"><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> OCround <span style="color: #0066CC;">extends</span> IOpenClosed
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, rw<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>Next, we do the same thing for a rectangle.</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('p836code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83611"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p836code11"><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> OCrectangle <span style="color: #0066CC;">extends</span> IOpenClosed
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, rw, h<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>Okay, we’re all done. We’ll create a Client class to test it:</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('p836code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83612"><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
</pre></td><td class="code" id="p836code12"><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: #808080; font-style: italic;">//Parameters for setShape are:</span>
	<span style="color: #808080; font-style: italic;">//setShape(fillCol:uint,borderCol:uint,lw:Number,rw:Number,h:Number,c:Number):void</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> rec:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCrectangle<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> cir:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCround<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> rndrec:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCroundrec<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>
			rec.<span style="color: #006600;">setShape</span><span style="color: #66cc66;">&#40;</span>0xC03000,0x32331D,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			rec.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>, rec.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rec<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			cir.<span style="color: #006600;">setShape</span><span style="color: #66cc66;">&#40;</span>0x787746,0x40411E,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			cir.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>, cir.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">200</span>;
			addChild<span style="color: #66cc66;">&#40;</span>cir<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>When tested, we can see that we get our shapes as shown in Figure 1.<br />
<img class="alignnone size-full wp-image-837" title="oc1" src="http://www.as3dp.com/wp-content/uploads/2009/03/oc1.png" alt="oc1" width="183" height="253" /></p>
<p><em><strong>Figure 1:</strong> Two shapes created with common interface</em></p>
<p>Obviously not rocket science, but it’s a handy tool for setting up different kinds of shapes.</p>
<p><strong>What about Rounded Rectangles? </strong></p>
<p>This part is the important part as far as both the Open/Closed principle and design patterns are concerned. Suppose you decide you need a rounded rectangle. With the Open/Closed principle in mind, you <em>cannot modify the code</em> but you can add to it through extension. So, let’s add a rounded rectangle:</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('p836code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83613"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p836code13"><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> OCroundrec <span style="color: #0066CC;">extends</span> IOpenClosed
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, rw,h,c<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 wasn’t hard. Add the following lines to the Client, and let’s test it:</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('p836code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83614"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p836code14"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rndrec:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCroundrec<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
….
<span style="color: #006600;">rndrec</span>.<span style="color: #006600;">setShape</span><span style="color: #66cc66;">&#40;</span>0x32331D,0xC03000,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">24</span><span style="color: #66cc66;">&#41;</span>
rndrec.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>, rndrec.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
addChild<span style="color: #66cc66;">&#40;</span>rndrec<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Figure 2 shows that a rounded rectangle has joined the circle and the rectangle on the stage:<br />
<img class="alignnone size-full wp-image-838" title="oc2" src="http://www.as3dp.com/wp-content/uploads/2009/03/oc2.png" alt="oc2" width="304" height="241" /><br />
<em><strong>Figure 2: </strong> A third shape is created through extension.</em></p>
<p>As you can see, while it was more work to set it up initially, it’s a lot less work to create new shapes. Also, requesting the shapes in the clients is very simple as well.</p>
<p><strong>Into the Lunch Bucket</strong></p>
<p>Here’s a handy and simple principle that you can see reflected in many different design patterns. For example, the Decorator, Composite, and Builder offer change through extension. So, to add this principle to your lunch bucket, add a class that creates ellipses. If you can do that, you can both understand the principle and apply it. That means it&#8217;s ready for work. Figure 3 shows the missing ellipse:</p>
<p><img class="alignnone size-full wp-image-848" title="oc3" src="http://www.as3dp.com/wp-content/uploads/2009/03/oc3.png" alt="oc3" width="311" height="342" /><br />
<em><strong>Figure 3: </strong>Test your understanding by adding an ellipse to the mix.</em></p>
<p>One caveat to using this principle is one of the first design pattern principles: <em>favor composition over inheritance</em>. That doesn’t mean you cannot create new elements for your program using extension—clearly some design patterns do so. Rather, just keep in mind that the principles in both OOP and design patterns work in conjunction with one another and you need to realize that not all OOP solutions are grounded in a single principle.</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%2F03%2Fdesign-pattern-principles-for-actionscript-30-the-openclosed-principle%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20The%20Open%2FClosed%20Principle" id="wpa2a_18"><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/03/design-pattern-principles-for-actionscript-30-the-openclosed-principle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: The Liskov Substitution Principle</title>
		<link>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/</link>
		<comments>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 12:26:00 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=659</guid>
		<description><![CDATA[Gentle Reader: Now that we’ve worked through all of the design patterns in ActionScript 3.0 from GoF (well, Builder is still in the works, but that’ll be available soon), now would be a good time start going through the principles underlying design patterns. This will be the first in that series. The 1987 OOPSLA keynote [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule2.png" alt="bucketrule2" title="bucketrule2" width="150" height="133" class="alignleft size-full wp-image-764" /><em><strong>Gentle Reader</strong>: Now that we’ve worked through all of the design patterns in ActionScript 3.0 from GoF (well, <strong>Builder</strong> is still in the works, but that’ll be available soon), now would be a good time start going through the principles underlying design patterns. This will be the first in that series.</em></p>
<p>The 1987 OOPSLA keynote address by Barbara Liskov contained what has become known as the <em>Liskov Substitution Principle (LSP)</em>. Essentially, the principle holds that</p>
<blockquote><p>If a Client is using a reference to a base class it should be able to <strong>substitute</strong> a derived class for it without affecting the program’s operation </p></blockquote>
<p>(Actually Dr. Liskov said something more like:</p>
<blockquote><p>If for each object <strong>o1</strong> of type <strong>S</strong> there is an object <strong>o2</strong> of type <strong>T</strong> such that for all programs <strong>P</strong> defined in terms of <strong>T</strong>, the behavior of <strong>P</strong> is unchanged when <strong>o1</strong> is substituted for <strong>o2</strong> then <strong>S</strong> is a subtype of <strong>T</strong>.</p></blockquote>
<p>but I’m not about to…)</p>
<p>If the Client has an object of a certain type, it should be able to substitute a derived object of the same type. For example, suppose you have an <em>abstract</em> class named <em><strong>Automobile</strong></em> and from that class you have subclasses of <strong>Ford</strong>, <strong>Toyota</strong>, and <strong>Peugeot</strong>. Your Client has an object, <strong>myAuto:Automobile</strong>. The <strong>myAuto</strong> object can be any of the subclasses, and if a substitution is made for any one of them everything keeps on working without a problem and the change is unknown to the Client. So if you substitute a Ford for a Toyota, the Client can work with the objects without having to adjust for the change. What’s more, if you want to add a <strong>Fiat</strong> class as a subclass of <strong>Automobile</strong>, the <strong>myAuto</strong> object handles it with nary a whimper.</p>
<p>The one caveat is that the subclasses must all honor the contractual conditions of the parent class. So, any methods in the parent class must be functioning in the subclass (aka, <em>derived class</em>.)</p>
<p>Now you may be thinking, <em>So what?</em> If you’re at all familiar with other principles of OOP and Design Patterns, this principle may sound vaguely familiar, but what is the importance of this concept/principle/idea? It is this: Because the Client is unaware of the concrete class that the object may implement, the structure is far more resilient. Not only can the same structure be reused, it can be changed, updated and generally fiddled with without easily breaking anything. (Think of adding more car manufacturers to the <strong>Automobile</strong> class.) As far as the Client is concerned, as long as the interface rules are followed with the object, everything is hunky-dory.<br />
<span id="more-659"></span><br />
<strong>A Simple Practice</strong></p>
<p>As a principle, the Liskov Substitution Principle is easy to carry around in your pocket. If you write the following line in your Client (or even your Factory) you’re doing something wrong:</p>
<p><strong><code>myObj:SubClassA = new SubClassA();</code></strong></p>
<p>You know that another subclass of the same base class cannot be substituted for SubClassA. If you attempt to substitute SubClassB for myObj, it will fail. Now, instead, all you have to remember is to write,</p>
<p><strong><code>myObj:ParentClass = new SubClass();</code></strong></p>
<p>That’s (just about) it! Well, one more thing. The parent class needs to be an <em>abstract class or an interface. </em><strong>Now</strong>, that’s it! How difficult is that to remember?</p>
<p><strong>LSP in the Weird World</strong></p>
<p>In looking at examples of the LSP principle, the two that pop up the most are the rectangle/square and ellipse/circle conundrums. We’ll use the rectangle/square example and try to provide two different alternatives to illustrate programming with LSP and without LSP.<br />
Beginning with the geometric observation, <em>all squares are also rectangles</em>, we’re likely to create a structure like that shown in Figure 1:</p>
<p style="text-align: center;">
<img class="size-full wp-image-660 aligncenter" title="rec1" src="http://www.as3dp.com/wp-content/uploads/2009/02/rec1.png" alt="rec1" width="335" height="242" /></p>
<p><em><strong>Figure 1:</strong> Square is a child of Rectangle</em></p>
<p>After all, we can envision a square as nothing more than a rectangle with equal sides. So an operation such as,</p>
<p><strong><code>makeRectangle(w,h); </code></strong></p>
<p>is an effective way to make either a square or rectangle. We add the Square class simply to examine the LS Principle; otherwise creating rectangles or squares would be accomplished using the <strong>makeRectangle(w,h) </strong> method making sure that w and h are equal when making squares. The inheritance path clearly indicates that a Square object <em>IS A</em> Rectangle. (That is, the two have an <em>IS A</em> relationship.) However, because Rectangle is a concrete class if I substitute Rectangle for Square, I’m going to have to change the type. I cannot substitute an instance of Rectangle for an instance of Square. This violates the Liskov Substitution Principle.<br />
Now, consider Figure 2. Instead of having Square as a child of Rectangle, both Square and Rectangle are children of the abstract class <em>Boxes</em>.</p>
<p style="text-align: center;">
<img class="size-full wp-image-661 aligncenter" title="rec2" src="http://www.as3dp.com/wp-content/uploads/2009/02/rec2.png" alt="rec2" width="435" height="243" /></p>
<p><em><br />
<strong>Figure 2: </strong>Abstract Class is Parent of both Squares and Rectangles</em></p>
<p>If I have an instance of Boxes, I can substitute either Square or Rectangle for the instance. In looking at an example, the Client is where we can expect to see the substitution principle at work. In the following example, the Client has a single object (<strong>subThis</strong>) typed as <strong>Boxes</strong>. However, that single object can gingerly create either a square or rectangle without the Client knowing the difference. It is designed to illustrate LSP.</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('p659code22'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65922"><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
</pre></td><td class="code" id="p659code22"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> fl<span style="color: #000066; font-weight: bold;">.</span>controls<span style="color: #000066; font-weight: bold;">.</span>Button<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Client <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> subThis<span style="color: #000066; font-weight: bold;">:</span>Boxes<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> rec<span style="color: #000066; font-weight: bold;">:</span>Button=<span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> squ<span style="color: #000066; font-weight: bold;">:</span>Button=<span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Client<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//Constructer adds buttons</span>
			rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span>
			rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">65</span><span style="color: #000066; font-weight: bold;">;</span>
			rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span>doRec<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			rec<span style="color: #000066; font-weight: bold;">.</span>label = <span style="color: #990000;">&quot;Rectangle&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>rec<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">170</span><span style="color: #000066; font-weight: bold;">,</span>squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span>
			squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">65</span><span style="color: #000066; font-weight: bold;">;</span>
			squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span>doSquare<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			squ<span style="color: #000066; font-weight: bold;">.</span>label = <span style="color: #990000;">&quot;Square&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>squ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doSquare<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Square<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doRec<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Rect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The Client has two methods to create either a square or rectangle. However, both methods use the object <strong>subThis</strong> typed as <strong>Boxes</strong>. Using Boxes.createBox() (subThis.createBox()) the Client substitutes the Square and Rectangle for the Boxes object. Everything works smoothly. The way the program is set up when one is used, the box image changes from a square to a rectangle (or vice versa), but if you wanted, you could create as many as you want and splash them all over the stage. Figure 3 shows what you will see in the current setup.<br />
<img class="alignleft size-full wp-image-663" title="squarerec" src="http://www.as3dp.com/wp-content/uploads/2009/02/squarerec.png" alt="squarerec" width="600" height="199" /><br />
<em><strong>Figure 3: </strong> Substituting Square and Rectangle Subclasses for Boxes Object</em></p>
<p>With this simple program, you should be able to see the ramifications for larger programs. In fact, the more your program grows, the more important LSP becomes. If you <em>know</em> that you can substitute <em>any</em> derived class for a base class, you are less likely to have unpleasant surprises. As a program grows and changes, you will have more modules, more objects, and more methods that need to interact in a program. The LSP helps keep those relations functional.</p>
<p><strong>The Base Class</strong></p>
<p>After working with the LS Principle, I wondered,</p>
<blockquote><p>…is there any reason to create a concrete base class? </p></blockquote>
<p>The answer is <strong>no</strong>.</p>
<blockquote><p>Not a single design pattern has a structure with a concrete parent class.</p></blockquote>
<p>What do I ever need a concrete parent class for? I can load up a parent class with functionality that can be used with subclasses, but I need some kind of wiggle room for the subclasses to extend the base class. Following the open/closed principle (“Open for extension. Closed for modification”), I can extend with added functionality from a superclass but I don’t want to modify it.</p>
<p>The following base class (Boxes) has a single method and imports the essential Sprite class needed for the graphics. The concrete classes override the function but they both inherit the Sprite class embedded in the base 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('p659code23'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65923"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p659code23"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">//abstract class</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Boxes <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//Abstract content</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In an odd choice, ActionScript 3.0 has Graphic methods for a circle (drawCircle) and an ellipse (drawEllipse), but not for a square &#8211;only a rectangle (drawRect). In order to create an equivalent square and rectangle pair, I used the new Player 10 ActionScript 3.0 drawPath() method. (If you’re using Flash CS3 or versions of Flex prior to 3.2, alternative Square and Rectangle classes can be found at the end of this post.) In looking at the concrete classes, there’s not much difference other than the second parameter in the Square class is a dummy variable that is happy with number you stick there. The first parameter (h) is passed to a side variable (s) and only that single value determines the width and heights, which, of course, must be equal. First, you have the Rect 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('p659code24'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65924"><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="p659code24"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Rect <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCmds<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCoord<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			recCmds = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//x</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//y</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = w<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = w<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span> = h<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">7</span><span style="color: #000000;">&#93;</span> = h<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">9</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x990000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span>drawPath<span style="color: #000000;">&#40;</span>recCmds<span style="color: #000066; font-weight: bold;">,</span> recCoord<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Next, the Square class is similar, but notice the “dummy” label for the second parameter.</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('p659code25'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65925"><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
</pre></td><td class="code" id="p659code25"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Square <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCmds<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCoord<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> s<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>dummy<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			s = w<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCmds = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//x</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//y</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">7</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">9</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x009900<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span>drawPath<span style="color: #000000;">&#40;</span>recCmds<span style="color: #000066; font-weight: bold;">,</span> recCoord<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, no rocket science is involved in either the drawing classes and their base class or the Client class. A single method is used to generate either, and the only real difference between the child classes is the enforcement of the equal width and height in the Square class.</p>
<p><strong>LSP Lunch Bucket Rule</strong></p>
<p>What can we take to work with the Liskov Substitution Principle? It’s nice to know and understand why the principle is in place, but who needs to have all of the implied tentacles of the principle rattling around in your head—especially considering the way Liskov stated it. We know the principle as <em>you should be able to substitute any derived class from a base class where an instance of the base class exists.</em> After further examination, the only way to do that is to use an abstract class or interface for all of your inheritance and implementations. Put more succinctly, the <strong>Lunch Bucket Rule</strong> is,</p>
<blockquote><p>Only subclass from abstract classes. Do not inherit from concrete classes.</p></blockquote>
<p>That’s easy enough to remember. Put that in your head and see if it helps move an OOP and Design Pattern agenda along at work.<br />
\- &#8211; - &#8211; - &#8211; - &#8211; - &#8211; - &#8212; &#8211; - &#8211; - &#8211; - &#8211; - &#8211; - &#8212; &#8211; - &#8211; - &#8211; - &#8211; - &#8211; - &#8212; &#8211; - &#8211; - \</p>
<p><strong>Flex client and Rect and Square Classes for Flash CS3 and Flex prior to 3.2</strong></p>
<p>First, if you&#8217;re using Flex, you might want to use the following mxml program for your client (Ignore the Client.as class if you do):</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('p659code26'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65926"><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
</pre></td><td class="code" id="p659code26"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;?</span>xml <span style="color: #004993;">version</span>=<span style="color: #990000;">&quot;1.0&quot;</span> encoding=<span style="color: #990000;">&quot;utf-8&quot;</span><span style="color: #000066; font-weight: bold;">?&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Application xmlns<span style="color: #000066; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #990000;">&quot;absolute&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Script<span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;!</span><span style="color: #000000;">&#91;</span>CDATA<span style="color: #000000;">&#91;</span>
		<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> subThis<span style="color: #000066; font-weight: bold;">:</span>Boxes<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doSquare<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> subThis <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">contains</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Square<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doRec<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> subThis <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">contains</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Rect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>Script<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button <span style="color: #004993;">x</span>=<span style="color: #990000;">&quot;100&quot;</span> <span style="color: #004993;">y</span>=<span style="color: #990000;">&quot;76&quot;</span> label=<span style="color: #990000;">&quot;Square&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;doSquare()&quot;</span> id=<span style="color: #990000;">&quot;squ&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button <span style="color: #004993;">x</span>=<span style="color: #990000;">&quot;200&quot;</span> <span style="color: #004993;">y</span>=<span style="color: #990000;">&quot;76&quot;</span> label=<span style="color: #990000;">&quot;Rectangle&quot;</span> id=<span style="color: #990000;">&quot;rec&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;doRec()&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>Application<span style="color: #000066; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>Here’s the Rect 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('p659code27'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65927"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p659code27"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Rect <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span> <span style="color: #000000;">&#40;</span>0x990000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>w<span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And now the Square 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('p659code28'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65928"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p659code28"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Square <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> s<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>dummy<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			s=w<span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span> <span style="color: #000000;">&#40;</span>0x009900<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>s<span style="color: #000066; font-weight: bold;">,</span>s<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><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-the-liskov-substitution-principle%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20The%20Liskov%20Substitution%20Principle" id="wpa2a_20"><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-the-liskov-substitution-principle/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

