<?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; Abstract Factory</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/abstract-factory-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript 3.0 Saturated Abstract Factory 6: The Abstractory</title>
		<link>http://www.as3dp.com/2011/01/actionscript-3-0-saturated-abstract-factory-6-the-abstractory/</link>
		<comments>http://www.as3dp.com/2011/01/actionscript-3-0-saturated-abstract-factory-6-the-abstractory/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 16:31:10 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Factory]]></category>
		<category><![CDATA[Saturated Abstract Factory]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4320</guid>
		<description><![CDATA[Every Family Needs a Factory In the last installment of the saturated Abstract Factory, you saw that unlike the Factory Method where a concrete factory instantiates a single concrete product, the Abstract Factory instantiates a family of products. In this final post in the Abstract Factory saturation series, you will see how the factories (creational [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4319" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/01/AbFactory6.png" alt="Bringing together Abstract Factories and Families of Products" title="AbFactory6" width="250" height="250" class="size-full wp-image-4319" /><p class="wp-caption-text">Bringing together Abstract Factories and Families of Products</p></div> <strong>Every Family Needs a Factory</strong></p>
<p>In the last installment of the saturated Abstract Factory, you saw that unlike the Factory Method where a concrete factory instantiates a single concrete product, the Abstract Factory instantiates a <em>family of products</em>. In this final post in the Abstract Factory saturation series, you will see how the factories (creational classes) create more than a single product. Each of the concrete creational classes instantiate two or more concrete products that have different product interfaces but are &#8220;joined in instantiation&#8221; by the individual concrete factories that share a common interface. In effect, the family planning is done by the Abstract Factory interface, and not the Product interface. Click the factory button to play the final product of this series and the download button to get all of the files we&#8217;ll be looking at:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/abfac/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/factoryBtn.png" alt="factoryBtn" title="factoryBtn" width="125" height="60" class="alignleft size-full wp-image-4179" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/abfac/abfacfiles.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>Before continuing take another look at the full class diagram of the Abstract Factory design pattern in Figure 1:<br />
<div id="attachment_3977" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/AbFactoryDG500.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Abstract Factory Class Diagram" title="AbFactoryDG500" width="500" height="234" class="size-full wp-image-3977" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Abstract Factory Class Diagram</em></p></div></p>
<p>First, consider the way that the Abstract Factory interface contains methods for instantiating more than a single product. Each concrete factory instantiates a minimum of two products. It is like the Factory Method, but each concrete factory only instantiates a single product in the Factory Method. The Abstract Factory creates Multiple products.</p>
<p><div id="attachment_4341" class="wp-caption alignright" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/01/ganesha.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Think of Ganesha when considering the role of the busy Client&lt;/em&gt;" title="ganesha" width="250" height="276" class="size-full wp-image-4341" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Think of Ganesha when considering the role of the busy Client</em></p></div>Second, consider the role of the Client. The Client is included in the class diagram as a full-fledged participant, and it holds references to the abstract factory interface and to the abstract product interfaces. It is not unlike the Hindu god, Ganesha who has four arms to handle all of the different client references and is also the Remover of Obstacles—and we all know there are plenty of obstacles in learning design patterns. Importantly, the Client holds references to the interfaces of both the factory and the products. By only referencing the interfaces, the Client is loosely bound to the factory and products thereby allowing for easy update and change. While it looks a bit complex, the pattern actually simplifies a complex process.<br />
<span id="more-4320"></span></p>
<p><strong>Gathering the Files</strong></p>
<p>In order to work with a full scale (yet modest) Abstract Factory design pattern, I found that it helps to look at the files that are part of the pattern in a class diagram. Figure 3 shows the classes and interfaces to be used.<br />
<div id="attachment_4357" class="wp-caption alignnone" style="width: 505px"><img src="http://www.as3dp.com/wp-content/uploads/2011/01/FileDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt;Abstract Factory File Diagram&lt;/em&gt;" title="FileDiagram" width="495" height="321" class="size-full wp-image-4357" /><p class="wp-caption-text"><em><strong>Figure 3:</strong>Abstract Factory File Diagram</em></p></div> Just in case you&#8217;ve looked at the other Abstract Factory example that you can find on this blog, you should know that instead of abstract classes, this version uses interfaces for both the factory and product participants.</p>
<p><strong>The Product Families</strong></p>
<p>Continuing from previous posts in this series, the product families are made up of a &#8220;rounded&#8221; family and a &#8220;blocky&#8221; family. Each family consists of graphic (IProduct) and text (ITextProd)  interfaces and their accompanying concrete implementations. The following listings has both interfaces and all of the concrete classes:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4320code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p43204"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</pre></td><td class="code" id="p4320code4"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Interface for graphic product</span>
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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IProduct
&nbsp;
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> productSpecs<span style="color: #66cc66;">&#40;</span>clr:uint,stroke:uint<span style="color: #66cc66;">&#41;</span>:Shape;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Curvey implementation</span>
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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CircleProduct <span style="color: #0066CC;">implements</span> IProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> perfectCircle:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape  ;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> productSpecs<span style="color: #66cc66;">&#40;</span>clr:uint,stroke:uint<span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			perfectCircle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>clr<span style="color: #66cc66;">&#41;</span>;
			perfectCircle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span>stroke,0xF6E497<span style="color: #66cc66;">&#41;</span>;
			perfectCircle.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span>;
			perfectCircle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> perfectCircle;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Blocky implementation</span>
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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BlockProduct <span style="color: #0066CC;">implements</span> IProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> perfectBlock:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> productSpecs<span style="color: #66cc66;">&#40;</span>clr:uint,stroke:uint<span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			perfectBlock.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>clr<span style="color: #66cc66;">&#41;</span>;
			perfectBlock.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span>stroke, 0xB9121B<span style="color: #66cc66;">&#41;</span>;
			perfectBlock.<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>, <span style="color: #cc66cc;">150</span>, <span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span>;
			perfectBlock.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> perfectBlock;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Interface for Text element</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ITextProd
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> doGraphicText<span style="color: #66cc66;">&#40;</span>clr:uint,fsize:uint,msg:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Curvey Text</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CirText <span style="color: #0066CC;">implements</span> ITextProd
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><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> doGraphicText<span style="color: #66cc66;">&#40;</span>clr:uint,fsize:uint,msg:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
		<span style="color: #66cc66;">&#123;</span>
			txtFld.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			txtFormat.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Comic Sans MS&quot;</span>;
                        txtFormat.<span style="color: #0066CC;">color</span> = clr;
                        txtFormat.<span style="color: #0066CC;">size</span> = fsize;
			txtFld.<span style="color: #006600;">defaultTextFormat</span> = txtFormat;
			txtFld.<span style="color: #0066CC;">text</span>=msg;
			<span style="color: #b1b100;">return</span> txtFld;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Blocky Text</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BlockText <span style="color: #0066CC;">implements</span> ITextProd
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><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> doGraphicText<span style="color: #66cc66;">&#40;</span>clr:uint,fsize:uint,msg:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
		<span style="color: #66cc66;">&#123;</span>
			txtFld.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			txtFormat.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
                        txtFormat.<span style="color: #0066CC;">color</span> = clr;
                        txtFormat.<span style="color: #0066CC;">size</span> = fsize;
			txtFld.<span style="color: #006600;">defaultTextFormat</span> = txtFormat;
			txtFld.<span style="color: #0066CC;">text</span>=msg;
			<span style="color: #b1b100;">return</span> txtFld;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, nothing much has changed in the products other than some of the colors in the strokes. The family of the products remain constant.</p>
<p><strong>The Factories</strong></p>
<p>The factories have changed in important ways. Instead of returning TextField and Shape objects, they return the concrete products typed through their interfaces. This change is important because they Client holds references to the product interfaces and not the concrete returns from the classes. So with this arrangement, the Client can request a whole product from the factory and not just the returns. The following three objects make up the Abstract Factory interface and implementations:</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('p4320code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p43205"><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="p4320code5"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IAbfactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> createGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IProduct;
		<span style="color: #000000; font-weight: bold;">function</span> createText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ITextProd;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Factory implementation that instantiates the &quot;Round Family&quot; of products</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RoundFactory <span style="color: #0066CC;">implements</span> IAbfactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grProd:IProduct;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtProd:ITextProd;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IProduct
		<span style="color: #66cc66;">&#123;</span>
			grProd=<span style="color: #000000; font-weight: bold;">new</span> CircleProduct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> grProd;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ITextProd
		<span style="color: #66cc66;">&#123;</span>
			txtProd=<span style="color: #000000; font-weight: bold;">new</span> CirText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> txtProd;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Factory implementation that instantiates the &quot;Blocky Family&quot; of products</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BlockFactory <span style="color: #0066CC;">implements</span> IAbfactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grProd:IProduct;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtProd:ITextProd;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IProduct
		<span style="color: #66cc66;">&#123;</span>
			grProd=<span style="color: #000000; font-weight: bold;">new</span> BlockProduct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> grProd;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ITextProd
		<span style="color: #66cc66;">&#123;</span>
			txtProd=<span style="color: #000000; font-weight: bold;">new</span> BlockText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> txtProd;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice that all of the returns from the two concrete factory classes are interfaces and not concrete products. When it comes time to make changes, the loosely bound objects won&#8217;t complain where the alternations follow the interfaces but are different from the concrete implementations.</p>
<p><strong>The Big Time Client</strong></p>
<p>The humble client is an integral part of the Abstract Factory design pattern. It holds a reference to<em> all of the interfaces</em> in the design. In setting up the Client class I had to (seriously) tinker with the factory classes. I wanted to hold a reference to the product interfaces in the classes, and so I wanted the factories to return objects typed as interfaces rather than tightly bind them to concrete classes.</p>
<p>None of the concrete implementations of the product classes have constructor methods; and so when a concrete product typed to the interface in the Client is instantiated by the factories, there are no surprises. To get something concrete from the products I was able to use the Client objects&#8217;s methods that held a reference to the product interfaces.  The following listing shows the Client class with a minimum UI:</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('p4320code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p43206"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
</pre></td><td class="code" id="p4320code6"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//The Mighty Client</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Client ref to Abstract Factory</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> prodFamily:IAbfactory;
&nbsp;
		<span style="color: #808080; font-style: italic;">//Client ref to Abstract Products</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cirProduct:IProduct;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cirText:ITextProd;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> blockProduct:IProduct;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> blockText:ITextProd;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> holder:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<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> txtstore:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<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> tempGr:Shape;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tempTxt:<span style="color: #0066CC;">TextField</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//UI</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnRnd:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnBlock:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&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>
			addUI<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> curvey<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>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>tempGr<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				holder.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>tempGr<span style="color: #66cc66;">&#41;</span>;
				txtstore.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>tempTxt<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			prodFamily=<span style="color: #000000; font-weight: bold;">new</span> RoundFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			cirProduct=prodFamily.<span style="color: #006600;">createGraphic</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cirText=prodFamily.<span style="color: #006600;">createText</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			holder.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,holder.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			tempGr=cirProduct.<span style="color: #006600;">productSpecs</span><span style="color: #66cc66;">&#40;</span>0xb9121b,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
			holder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>tempGr<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>holder<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			txtstore.<span style="color: #006600;">x</span>=holder.<span style="color: #006600;">x</span>+<span style="color: #cc66cc;">5</span>,txtstore.<span style="color: #006600;">y</span>=holder.<span style="color: #006600;">y</span>+<span style="color: #cc66cc;">50</span>;
			tempTxt=cirText.<span style="color: #006600;">doGraphicText</span><span style="color: #66cc66;">&#40;</span>0xF6E497,<span style="color: #cc66cc;">18</span>,<span style="color: #ff0000;">&quot;Abstract Curve&quot;</span><span style="color: #66cc66;">&#41;</span>;
			txtstore.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>tempTxt<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtstore<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> blocky<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>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>tempGr<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				holder.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>tempGr<span style="color: #66cc66;">&#41;</span>;
				txtstore.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>tempTxt<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			prodFamily=<span style="color: #000000; font-weight: bold;">new</span> BlockFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			blockProduct=prodFamily.<span style="color: #006600;">createGraphic</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			blockText=prodFamily.<span style="color: #006600;">createText</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			holder.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,holder.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			tempGr=blockProduct.<span style="color: #006600;">productSpecs</span><span style="color: #66cc66;">&#40;</span>0xF6E497,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
			holder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>tempGr<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>holder<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			txtstore.<span style="color: #006600;">x</span>=holder.<span style="color: #006600;">x</span>+<span style="color: #cc66cc;">5</span>,txtstore.<span style="color: #006600;">y</span>=holder.<span style="color: #006600;">y</span>+<span style="color: #cc66cc;">50</span>;
			tempTxt=blockText.<span style="color: #006600;">doGraphicText</span><span style="color: #66cc66;">&#40;</span>0xb9121b,<span style="color: #cc66cc;">16</span>,<span style="color: #ff0000;">&quot;Abstract Block&quot;</span><span style="color: #66cc66;">&#41;</span>;
			txtstore.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>tempTxt<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtstore<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> addUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//UI</span>
			btnRnd.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,curvey<span style="color: #66cc66;">&#41;</span>;
			btnRnd.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Curves&quot;</span>;
			btnRnd.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>,btnRnd.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnRnd<span style="color: #66cc66;">&#41;</span>;
			btnBlock.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,blocky<span style="color: #66cc66;">&#41;</span>;
			btnBlock.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Blocks&quot;</span>;
			btnBlock.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>,btnBlock.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">40</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnBlock<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 about fills out our agenda on the Abstract Factory. As mentioned at one point, this design gave me the heebie-jeebies, but after wrestling it to the ground (again) I like it more and more. The key to understanding the pattern and successfully using this pattern lie in the following elements:</p>
<ul>
<li>Think of products as Familes of Products</li>
<li>A Concrete Factory for Each Product Family</li>
<li>The Client only references Interfaces (including abstract classes.)</li>
<li>Return product objects to the Client; not product results</li>
</ul>
<p>If you focus of those three features, I think you&#8217;ll find it to be much easier to use and understand.</p>
<p><strong>Some Afterthoughts</strong></p>
<p>I spent a lot of time with the Gang of Four&#8217;s book, and one thing that struck me was the fact that the very first design pattern presented after the introductory materials is the Abstract Factory. Imagine some poor soul who wants to learn design patterns and decides that the wisest course of action is to start at the beginning and work his way through sequentially. The image that follows is the guy curled up in a corner with his thumb in his mouth shaking uncontrollably. While the first two chapters provide a logical sequence and the general grouping of the patterns into Creational, Structural and Behavioral makes sense, everything else is alphabetical. Just a newbie&#8217;s luck that the first pattern is a bit challenging. The Adapter or Template Method patterns are much gentler beginnings.</p>
<p>While adding new families of products is pretty simple using the Abstract Factory, adding new products themselves is not. In the example in this post, the families are made up of a &#8220;round&#8221; and &#8220;block&#8221; styles that rely on images and text. To add a &#8220;futuristic&#8221; style that includes an image and text would be pretty easy. However, if we want to extend a product family to another element, the Abstract Factory takes some tinkering. That&#8217;s because the Abstract Factory sets the products that can be included&#8211;ours had the IProduct and ITextProd. Adding another product such as a UI would require changing the Abstract Factory and all of it&#8217;s child classes.</p>
<p>I don&#8217;t see it as difficult as GoF make it out to be. Most of the coding is in the products, and while adding another product to the factories involves some effort, it&#8217;s not a whole lot. More importantly, you just have to tweak the factories and not have to touch the products. Further, the new products won&#8217;t get in the way of the old ones.</p>
<p>Sooner or later, Flash and Flash Builder need to be part of the smart phone mix. Several have been working on Android and even a few have been able to use ActionScript 3.0 with iPhone/iPad apps. If ever there were multiple families of products it&#8217;s in the arena of small mobile apps and ones that present well on monitor screens approaching the size of drive-in theaters. The Abstract Factory seems like an ideal tool for making apps that fill the bill for different &#8220;families&#8221; of products—small mobile, tablet-size, laptop and fill-the-wall screens.</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%2F01%2Factionscript-3-0-saturated-abstract-factory-6-the-abstractory%2F&amp;title=ActionScript%203.0%20Saturated%20Abstract%20Factory%206%3A%20The%20Abstractory" 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/01/actionscript-3-0-saturated-abstract-factory-6-the-abstractory/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Saturated Abstract Factory 2: What&#039;s Wrong with Inheritance?</title>
		<link>http://www.as3dp.com/2010/11/actionscript-3-0-saturated-abstract-factory-2-whats-wrong-with-inheritance/</link>
		<comments>http://www.as3dp.com/2010/11/actionscript-3-0-saturated-abstract-factory-2-whats-wrong-with-inheritance/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 21:58:14 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Factory]]></category>
		<category><![CDATA[Saturated Abstract Factory]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4031</guid>
		<description><![CDATA[Doing Composition The essence of the Abstract Factory Design Pattern is to create compositions for the Client. In the class diagram shown in Part I of the Saturated Abstract Factory series, the Client looks like a juggler pulling in different parts that are composed into a functioning component. (&#8220;Components&#8221; in this context are objects created [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3993" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/AbFactory2.png" alt="Favor Composition over Inheritance" title="AbFactory2" width="250" height="250" class="size-full wp-image-3993" /><p class="wp-caption-text">Favor Composition over Inheritance</p></div><strong>Doing Composition</strong><br />
The essence of the Abstract Factory Design Pattern is to create compositions for the Client. In the class diagram shown in Part I of the Saturated Abstract Factory series, the Client looks like a juggler pulling in different parts that are composed into a functioning component. (&#8220;Components&#8221; in this context are objects created using composition.) In order to understand and appreciate composition, we need to start with that fundamental OOP concept <em>inheritance</em>.</p>
<p><strong>Inheritance in OOP</strong></p>
<p>Ever since The Gang of Four put forth the pithy dictum,</p>
<blockquote><p>Favor object composition over class inheritance,</p></blockquote>
<p>OOP programmers have decidedly shifted in the direction of creating programs using composition more than inheritance. Further, any inheritance is fairly shallow with an abstract class as a parent that doubles as an interface.</p>
<p>However, inheritance is a core concept in OOP, and it&#8217;s not something that should be dismissed lightly. Further, in going through the design pattern catalog that Gamma <em>et al</em> have compiled, you will see a lot of inheritance in the class diagrams. Lest we not throw out something useful, let&#8217;s look at a simple example of using inheritance. (You may want to download the files and look at the static outcome first using the buttons below.)<br />
<a href="http://www.sandlight.com/inheritance/inheritance.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignleft size-full wp-image-2020" /></a><a href="http://www.sandlight.com/inheritance/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/play.jpg" alt="play" title="play" width="106" height="46" class="alignnone size-full wp-image-2255" /></a></p>
<p><strong>The Architect</strong></p>
<p>Suppose I want to create an architect program that I can use to create different shapes. In fiddling around I create a class with Sprite and Shape properties along with a method that builds and returns a rectangle. In order not to embarrass myself too much, I make an effort to use a decent color scheme; so I visit <a href="http://kuler.adobe.com">kuler.adobe.com</a> and select a color pattern named <em>moderne interieur</em>. I then create a base class I name Architect shown in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4031code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p403111"><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="p4031code11"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Colors: F5F4E1,D6C9B5,B4AA97,D44917,82877A</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Architect <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> foundation:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> archGraphic:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> buildBox<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,wy:<span style="color: #0066CC;">Number</span>,
		          len:<span style="color: #0066CC;">Number</span>,hi:<span style="color: #0066CC;">Number</span>,
		          fill:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,0x82877A<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>ex, wy, len, hi<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			foundation.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>archGraphic<span style="color: #66cc66;">&#41;</span>;
                        <span style="color: #b1b100;">return</span> foundation;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Anticipating that I may want to extend the class, I made the main properties protected so that any child classes can inherit them and preserve as much encapsulation as possible. However, the rectangle-building method must be public so that it can be used by clients outside the class. Further the line around the rectangle is hard-coded to a given width and color, but otherwise, it&#8217;s fairly flexible. All of the other dimensions of my rectangle are entered through a parameter. Finally, the image is stuffed into a Sprite object and then returned.<br />
<span id="more-4031"></span><br />
<strong>Extending the Architect</strong></p>
<p>An architect does not live by rectangles alone; so we&#8217;ll add an ellipse. However, instead of starting from scratch or adding more methods, as OOP programmers, we can just extend the Architect to a child class that includes a method to create ellipses. The best part is that if we extend the Architect class, we&#8217;ll also get the Sprite properties because Architect extended Sprite and we&#8217;ll inherit the protected properties as well as the method for making rectangles.</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('p4031code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p403112"><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
</pre></td><td class="code" id="p4031code12"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Colors: F5F4E1,D6C9B5,B4AA97,D44917,82877A</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArchEllipse <span style="color: #0066CC;">extends</span> Architect
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEllipse<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,wy:<span style="color: #0066CC;">Number</span>,
		          len:<span style="color: #0066CC;">Number</span>,hi:<span style="color: #0066CC;">Number</span>,
		          fill:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,0x82877A<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span><span style="color: #66cc66;">&#40;</span>ex, wy, len, hi<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			foundation.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>archGraphic<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> foundation;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now, my child class, <strong>ArchEllipse</strong>, can do everything the parent class does and, in addition, create ellipses. All I had to do was to extend the parent and add a single method. The <strong>buildEllipse()</strong> method is able to use the same parameters as the <strong>buildBox()</strong> method in the <strong>Architect</strong> class. That was so easy, that I decided to extend the <strong>ArchEllipse</strong> to add a class to build triangles; so the next class simply extends ArchEllipse as the following shows:</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('p4031code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p403113"><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
</pre></td><td class="code" id="p4031code13"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Colors: F5F4E1,D6C9B5,B4AA97,D44917,82877A</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArchTriangle <span style="color: #0066CC;">extends</span> ArchEllipse
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildTriangle<span style="color: #66cc66;">&#40;</span>fill:uint,
		         startX:<span style="color: #0066CC;">Number</span>,startY:<span style="color: #0066CC;">Number</span>,
		         nextX:<span style="color: #0066CC;">Number</span>,nextY:<span style="color: #0066CC;">Number</span>,
		         lastX:<span style="color: #0066CC;">Number</span>,lastY:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawTriangles</span><span style="color: #66cc66;">&#40;</span>Vector.<span style="color: #66cc66;">&lt;</span>number<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span> startX,startY,  nextX,nextY,  lastX,lastY<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			foundation.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>archGraphic<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> foundation;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now, the class, ArchTriangle, can do everything its parent classes can do, plus it can create triangles. To test the class, instead of adding yet another child class with a constructor function, we can just make a Client class to request it.</p>
<p><strong>The Client</strong></p>
<p>With a simple client class, all that&#8217;s required is to instantiate a single instance of the ArchTriangle, and then invoke all of the methods it has plus two inherited from its parent classes. The following shows the Client code:</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('p4031code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p403114"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p4031code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Colors: F5F4E1,D6C9B5,B4AA97,D44917,82877A</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</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> arch:ArchTriangle=<span style="color: #000000; font-weight: bold;">new</span> ArchTriangle<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>
			addChild<span style="color: #66cc66;">&#40;</span>arch.<span style="color: #006600;">buildBox</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">400</span>,<span style="color: #cc66cc;">300</span>,0xD44917<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>arch.<span style="color: #006600;">buildEllipse</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">75</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,0xF5F4E1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>arch.<span style="color: #006600;">buildTriangle</span><span style="color: #66cc66;">&#40;</span>0xD6C9B5,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>If I put everything into a class diagram, I can see that the Client looks like it&#8217;s dealing off the bottom on the deck; however, it&#8217;s just instantiating the child class that has all the goodies. Figure 1 shows the class diagram, and you can see that it uses nothing but concrete classes:<br />
<div id="attachment_4042" class="wp-caption alignnone" style="width: 421px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/Inheritance.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Concrete Inheritance&lt;/em&gt;" title="Inheritance" width="411" height="423" class="size-full wp-image-4042" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Concrete Inheritance</em></p></div></p>
<p>Clearly, in its current form, the program is not too useful. However, all you&#8217;d have to do is to change the Client so that it includes some kind of input and buttons to add any shape in any color, size and location you want. You can use components from either Flash Builder 4 (Flex) or Flash Professional in the client to make it easy.</p>
<p><strong>Make a Dynamic Client with User Input</strong></p>
<p>Go ahead and create a Client that will turn this into an architect program that you can use to design a house—or just rearrange the furniture in your apartment. All you need is an input box for the different parameters in the different objects. (The same input can be used for the boxes and ellipses.) See how easy it is.</p>
<p>Also, while you&#8217;re at it, why not extend the ArchTriangle class with a ArchRounded class to make rounded rectangles with fill gradations. This would give you experience with using inheritance and seeing if this kind of OOP programming that can be sustained. Send in your creation in the comments section and we&#8217;ll put it up for illustrating creative roles for the client.</p>
<p>In the next installment, we&#8217;ll see about doing the same thing with components.</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%2F11%2Factionscript-3-0-saturated-abstract-factory-2-whats-wrong-with-inheritance%2F&amp;title=ActionScript%203.0%20Saturated%20Abstract%20Factory%202%3A%20What%26%23039%3Bs%20Wrong%20with%20Inheritance%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/11/actionscript-3-0-saturated-abstract-factory-2-whats-wrong-with-inheritance/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Saturated Abstract Factory 1: Wholesale Creation</title>
		<link>http://www.as3dp.com/2010/11/saturated-abstract-factory-1-wholesale-creation/</link>
		<comments>http://www.as3dp.com/2010/11/saturated-abstract-factory-1-wholesale-creation/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 00:30:12 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Factory]]></category>
		<category><![CDATA[Saturated Abstract Factory]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3958</guid>
		<description><![CDATA[The Creational Patterns To understand the Abstract Factory design pattern, we need to begin by understanding the whole category of Creational Patterns and what the Gang of Four means by creation. On pages 63-64 of our book and pages 81-85 of GoF, you will find more details about creational patterns, and here I&#8217;d like to [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3957" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/AbFactory1.png" alt="This is the First in the Abstract Factory Saturation Series" title="AbFactory1" width="250" height="250" class="size-full wp-image-3957" /><p class="wp-caption-text">This is the First in the Abstract Factory Saturation Series</p></div><strong>The Creational Patterns</strong></p>
<p>To understand the Abstract Factory design pattern, we need to begin by understanding the whole category of <strong>Creational Patterns</strong> and what the Gang of Four means by <em>creation</em>. On pages 63-64 of our book and pages 81-85 of GoF, you will find more details about creational patterns, and here I&#8217;d like to just touch on some of the key points. Also, Chapter 2 of our book, is an excellent chapter (written by Chandima) on the Factory Method Pattern that introduces the whole concept of <em>factory</em>. And of course you may want to look at <a href="http://www.as3dp.com/2009/01/25/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/">our original post on the Abstract Factory</a> design pattern.</p>
<p><strong>When You Depend on Object Composition</strong></p>
<p>One of the first and recurrent principles of design patterns is to <em>favor composition over inheritance</em>. As your programs come to depend more on object composition, you need to think more about creating components required for a task. To a large extent, the creational patterns are designed for composing. GoF note that as the emphasis shifts from inheritance towards composition, other changes occur.</p>
<blockquote><p>&#8230;emphasis shifts away from hard-coding a fixed set of behaviors towards defining a smaller set of fundamental behaviors that can be composed into any number of more complex ones. Thus creating objects with particular behaviors requires more than simply instantiating a class. pg. 81</p></blockquote>
<p>The result of this shift can be found in two recurring themes in creational patterns:</p>
<ul>
<li>Creational patterns encapsulate knowledge about which concrete classes the system uses.</li>
<li>Creational patterns hide how instances of these classes are created and put together.</li>
</ul>
<p>If loose coupling is the overriding theme of design patterns; then creational patterns show us how to create loosely coupled objects and use them in a composition rich application.<br />
<span id="more-3958"></span></p>
<p><strong>An Overview of the Abstract Factory</strong></p>
<p>In our first Saturated series installment, we examined how the Strategy pattern encapsulated families of algorithms. The Abstract Factory provides</p>
<blockquote><p>&#8230;an interface for creating families of related or dependent objects without specifying their concrete classes.(p.87).</p></blockquote>
<p>To see how this is done, we&#8217;ll start with a class diagram and a discussion of the pattern participants. Figure 1 shows the class diagram:<br />
<div id="attachment_3977" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/AbFactoryDG500.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Abstract Factory Class Diagram" title="AbFactoryDG500" width="500" height="234" class="size-full wp-image-3977" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Abstract Factory Class Diagram</em></p></div><br />
On some previous post, I mentioned that the Abstract Factory class diagram gives me the <em>heebie jeebies</em>, and it still does. Just look at it! The Client is part of the pattern and holds references to the Abstract Factory interface<em> and</em> Abstract Products simultaneously. <em>Sheeeze!</em> Then just look at how the Concrete Factories implement concrete products from each of the Abstract Product interfaces. They look like ant tracks at a picnic.</p>
<p><strong>Getting a Handle on the Abstract Factory</strong></p>
<p>To best understand the Abstract Factory we need to break it down and examine each of its participants.</p>
<ol>
<li><strong>AbstractFactory</strong>: This is an interface that holds operations that create abstract product objects. This is where the concept of abstract factory comes from. The concrete factories that actually create the product object implement this interface.</li>
<li><strong>ConcreteFactory</strong>: The factories implement the operations to create concrete product objects. By creating the products used by the Client, these factories maintain loose coupling between the Client and the Product.</li>
<li><strong>AbstractProduct</strong>: Most design patterns have single interfaces for different part of the the pattern. However, the Abstract Factor design pattern has multiple interfaces for the products. This is where the composition takes place as the concrete factories implement multiple products for the Client.</li>
<li><strong>ConcreteProduct</strong>: Each product is an implementation of an interface (AbstractProduct) that structures the product type.</li>
<li><strong>Client</strong>: Makes all requests only through the abstract interfaces. Here you will find good examples of programming to an interface instead of an implementation.</li>
</ol>
<p>Now in looking at the class diagram in Figure1, we can see a little more clearly what role each participant plays. However, in the next installment of the Abstract Factory, I&#8217;d like to back off a bit and examine exactly what it means to create a composed object. We&#8217;ll take a simple example of composition and then see how a factory can create an instance of the composed object for use by a Client. In the meantime, take a look at our first post on the Abstract Factory to provide a context for the rest of this series.</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%2F11%2Fsaturated-abstract-factory-1-wholesale-creation%2F&amp;title=ActionScript%203.0%20Saturated%20Abstract%20Factory%201%3A%20Wholesale%20Creation" 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/11/saturated-abstract-factory-1-wholesale-creation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Abstract Factory Design Pattern: Multiple Products and Factories</title>
		<link>http://www.as3dp.com/2009/01/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/</link>
		<comments>http://www.as3dp.com/2009/01/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:00:24 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Abstract Factory]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=575</guid>
		<description><![CDATA[This is one of the few design patterns that I worked up directly from the class diagram and from concepts in GoF. Normally, I like to look at some examples, done in Java or C#, but not this time. As you will see in Figure 1, the pattern appears to be fairly daunting, but I [...]]]></description>
			<content:encoded><![CDATA[<p><!--StartFragment--></p>
<p class="MsoNormal">This is one of the few design patterns that I worked up directly from the class diagram and from concepts in GoF. Normally, I like to look at some examples, done in Java or C#, but not this time. As you will see in Figure 1, the pattern appears to be fairly daunting, but I found it to be eminently practical, and it seemed to be a direct response to questions that I had about the Factory Method design pattern (See Chapter 2 for an in-depth explanation of the Factory Method.) You can download the entire example <a href="http://nemo.mwd.hartford.edu/~wsanders/blog/AbstractFac.zip">here </a>before continuing if you wish.</p>
<p class="MsoNormal"> Let me start with the gist of the example from GoF and provide something more concrete that’s likely to be a typical kind of issue Flash and Flex developers deal with. Imagine a project where your designers have created general templates for a business site and another for a game site. Their templates include a SWF background and a set of buttons for a UI. The buttons are wholly programmed and require nothing in the Library, and so using them for either Flash or Flex is fairly simple.</p>
<p class="MsoNormal"> You want to keep your design loose, and so you decide that a factory will be helpful. However, clearly you will need a factory to create instances of both buttons and the background template. Further, you want your products to derive from an abstract class to give you as much flexibility as possible. In the example here, you will need an abstract product for buttons and another for backgrounds. You also want your factory abstract enough to make requests for sets of objects from the different products. For example, you want your factory to deliver both a set of buttons and a background that are matching pairs. You don’t want a set of buttons for a game site with a background for a business site, but rather you want the buttons to match your background—business buttons with a business background and game buttons with a game background. This is a job for the Abstract Factory.</p>
<p class="MsoNormal"> Figure 1 shows the class diagram. In looking at the “create” lines (dashed lines), think of them as working with matched sets. The Client requests a <strong>business set</strong>; and it gets both a business product for buttons and another product for background. So while the diagram may look busy, it really is doing something that makes sense on a basic level. That is, the design is geared to <strong>sets</strong>; of products with factories that <em>create the requested sets rather than individual objects</em>.</p>
<p class="MsoNormal"><img class="alignleft size-full wp-image-584" title="abfactory66" src="http://www.as3dp.com/wp-content/uploads/2009/01/abfactory66.png" alt="abfactory66" width="513" height="256" /></p>
<p class="MsoNormal"> </p>
<p><em><strong>Figure 1: </strong> Abstract Factory Class Diagram</em><br />
Note that Figure 1 shows that both concrete factories create instances from each of the child classes of the two abstract product classes. You can very quickly see the practicality of this when you substitute some concrete elements for the more general conceptual names.<br />
<span id="more-575"></span><br />
<strong>Buttons and Backgrounds</strong></p>
<p>A simple project to use as an example is a program that loads a buttons-background pair. The buttons are made using the <strong>SimpleButton</strong> class, and the backgrounds are SWF files created using Flash. Figure 2 shows the class diagram for the sample project:<br />
<img class="alignleft size-full wp-image-579" title="abfactoryexample" src="http://www.as3dp.com/wp-content/uploads/2009/01/abfactoryexample.png" alt="abfactoryexample" width="622" height="310" /><br />
<em><strong>Figure 2: </strong> Example of Abstract Factory </em><br />
The two button classes (RoundBtn and RecBtn) dip into a buttons folder where they pick up the buttons. These other classes not in the diagram can be thought of as <em>helper</em> or <em>utility</em> classes. Likewise, the two SWF files might be considered <em>utility objects</em> in the mix. Figure 3 shows the actual files and folders arranged in the class diagram.<br />
<img class="alignleft size-full wp-image-580" title="abfacfile" src="http://www.as3dp.com/wp-content/uploads/2009/01/abfacfile.png" alt="abfacfile" width="618" height="451" /><br />
<em><strong>Figure 3: </strong> File Diagram of Abstract Factory </em></p>
<p>No lines go to either the buttons folder or the two .swf files. They’re really not part of the pattern but rather resources (<em>helpers if you wish</em>) for the different participants. <em>If we did add lines, they’d be dashed ones</em> to indicate that the product subclasses  create instances of the helpers.</p>
<p><strong>The Client</strong></p>
<p>The <strong>Client</strong> is a busy object in this design. It holds references not only to the abstract factory (<strong>AbFactory</strong>) it also holds references to <em>all</em> abstract product classes. The important point here is that by holding references to an interface (the abstract classes), the <strong>Client</strong> doesn’t get bogged down with specific dependencies. If any of the concrete classes change, the client’s references can handle it. Let’s take a look at the code for this key participant in the pattern:</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('p575code19'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57519"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code" id="p575code19"><pre class="actionscript" style="font-family:monospace;"><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>;
&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> busFactory:AbFactory;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameFactory:AbFactory;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn1:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn2:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn3:ProductA;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameWall:ProductB;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> busWall:ProductB;
&nbsp;
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnX:<span style="color: #0066CC;">int</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>
&nbsp;
			busFactory=<span style="color: #000000; font-weight: bold;">new</span> BusinessFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			btnX=<span style="color: #cc66cc;">20.9</span>;
			busWall=busFactory.<span style="color: #006600;">createProductB</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>busWall<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btn1=busFactory.<span style="color: #006600;">createProductA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Accounts&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn1<span style="color: #66cc66;">&#41;</span>;
			btn1.<span style="color: #006600;">x</span>=btnX, btn1.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">224</span>;
&nbsp;
			btn2=busFactory.<span style="color: #006600;">createProductA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Factory&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2<span style="color: #66cc66;">&#41;</span>;
			btn2.<span style="color: #006600;">x</span>=btnX, btn2.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">272</span>;
&nbsp;
			btn3=busFactory.<span style="color: #006600;">createProductA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Production&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn3<span style="color: #66cc66;">&#41;</span>;
			btn3.<span style="color: #006600;">x</span>=btnX, btn3.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">320</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Move block comment symbols to change</span>
                       <span style="color: #808080; font-style: italic;">// from business to game</span>
<span style="color: #808080; font-style: italic;">/*
			gameFactory=new GameFactory();
			btnX=18;
			gameWall=gameFactory.createProductB();
			addChild(gameWall);
&nbsp;
			btn1=gameFactory.createProductA(&quot;Fire&quot;);
			addChild(btn1);
			btn1.x=btnX, btn1.y=224;
&nbsp;
			btn2=gameFactory.createProductA(&quot;Move&quot;);
			addChild(btn2);
			btn2.x=btnX, btn2.y=272;
&nbsp;
			btn3=gameFactory.createProductA(&quot;Hide&quot;);
			addChild(btn3);
			btn3.x=btnX, btn3.y=320;
&nbsp;
			// Don’t forget to move close comment block symbol
*/</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 first item to note is how the Client class holds references to both the abstract factory and abstract product:<br />
<code><br />
private var busFactory:<strong>AbFactory</strong>;<br />
private var gameFactory: <strong>AbFactory</strong>;<br />
…<br />
private var btn1: <strong>ProductA</strong>;<br />
private var btn2: <strong>ProductA</strong>;<br />
private var btn3: <strong>ProductA</strong>;<br />
…<br />
private var gameWall: <strong>ProductB</strong>;<br />
private var busWall: <strong>ProductB</strong>;<br />
</code></p>
<p>In this particular example, the choices for a business or game site are separated by commenting out one of the sites. In this case, the business site appears. Of course you can add a UI that would call up one or the other, but this approach seemed a bit clearer. To see one or the other, just change the position of the comment characters (/* and */). So using the default comment characters, when you test the program, Figure 4 shows what you’ll see initially:</p>
<p> <img class="alignleft size-full wp-image-581" title="business" src="http://www.as3dp.com/wp-content/uploads/2009/01/business.png" alt="business" width="550" height="400" /></p>
<p><em><strong>Figure 4: </strong> Business Site and Buttons </em></p>
<p>By moving the comment symbols to block out the business options and opening the game options, you’ll see an entirely different background and button set. Figure 5 shows the dramatic difference.<br />
<img class="alignleft size-full wp-image-582" title="game" src="http://www.as3dp.com/wp-content/uploads/2009/01/game.png" alt="game" width="550" height="400" /></p>
<p><em><strong>Figure 5: </strong> Game Site and Buttons </em></p>
<p>By comparing Figures 4 and 5, you can see an entirely different set of buttons and background. The square in the middle rotates to further distinguish the game products from the business ones. (I know, gratuitous use of animation. Sorry.)</p>
<p><strong>The Abstract and Concrete Factories</strong></p>
<p>The abstract factory allows far more flexibility than a concrete factory. Because the backgrounds and buttons are clearly different kinds of products, it makes perfect sense to have different kinds of factories to generate the products. The utility of the abstract factory can be seen in the different kinds of concrete factories. As you will see, the concrete factories are only slightly different. Each must create an instance of all products classes (<strong>ProductA</strong> and <strong>ProductB</strong> in the example), but because the concrete classes are different they must create different concrete class instances. In this example, The <strong>GameFactory</strong> and <strong>BusinessFactory</strong> classes create different buttons and backgrounds and yet each is a child of the abstract factory class. The following listing shows the factory classes:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p575code20'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57520"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code" id="p575code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract Factory</span>
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;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbFactory <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:ProductA
		<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> createProductB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ProductB
		<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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Game Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameFactory <span style="color: #0066CC;">extends</span> AbFactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameBtn:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameWall:ProductB;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:ProductA
		<span style="color: #66cc66;">&#123;</span>
			gameBtn = <span style="color: #000000; font-weight: bold;">new</span> RoundBtn<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> gameBtn;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ProductB
		<span style="color: #66cc66;">&#123;</span>
			gameWall=<span style="color: #000000; font-weight: bold;">new</span> GameStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> gameWall;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Business Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessFactory <span style="color: #0066CC;">extends</span> AbFactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> businessBtn:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> businessWall:ProductB;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:ProductA
		<span style="color: #66cc66;">&#123;</span>
			businessBtn = <span style="color: #000000; font-weight: bold;">new</span> RecBtn<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> businessBtn;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ProductB
		<span style="color: #66cc66;">&#123;</span>
			businessWall=<span style="color: #000000; font-weight: bold;">new</span> BusinessStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> businessWall;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, each child factory returns its own unique set of buttons and background as shown in Figures 4 and 5 above.</p>
<p><strong>The Abstract Products and their Children</strong></p>
<p>The design pattern shows two abstract products, but you can think of them as <em>at least</em> two products. You can have as many products as you want, but for each abstract product, you need a concrete factory. To help keep everything clear, the abstract product classes were named <strong>ProductA</strong> and <strong>ProductB</strong>. The “A” products are buttons and the “B” products are backgrounds. As you can see in the following listings, each is different. Both include abstract methods, but <strong>ProductB</strong> has a heavily parameterized method, <strong>setWallName</strong> to add flexibility to the placement of a page header. To help keep everything clear, first you will see the abstract product and then the two concrete children classes for each.</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('p575code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57521"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
</pre></td><td class="code" id="p575code21"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Product A: The buttons</span>
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;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProductA <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> setUpA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Round Button concrete product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> buttons.<span style="color: #006600;">BtnStateEllipse</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RoundBtn <span style="color: #0066CC;">extends</span> ProductA
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnStateEllipse:BtnStateEllipse;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RoundBtn<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpA<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btnStateEllipse = <span style="color: #000000; font-weight: bold;">new</span> BtnStateEllipse<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnStateEllipse<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Rectangle button concrete product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> buttons.<span style="color: #006600;">BtnState</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RecBtn <span style="color: #0066CC;">extends</span> ProductA
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnState:BtnState;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RecBtn<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpA<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btnState = <span style="color: #000000; font-weight: bold;">new</span> BtnState<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnState<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Product B: The Backgrounds</span>
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;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProductB <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setWallName<span style="color: #66cc66;">&#40;</span>wall:<span style="color: #0066CC;">String</span>,sz:<span style="color: #0066CC;">int</span>,hor:<span style="color: #0066CC;">int</span>,vert:<span style="color: #0066CC;">int</span>,txtField:<span style="color: #0066CC;">TextField</span>,format:<span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtField.<span style="color: #0066CC;">text</span>=wall;
			txtField.<span style="color: #006600;">x</span>=hor, txtField.<span style="color: #006600;">y</span>=vert;
			txtField.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #006600;">CENTER</span>;
			format.<span style="color: #0066CC;">size</span>=sz;
			format.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			txtField.<span style="color: #0066CC;">setTextFormat</span><span style="color: #66cc66;">&#40;</span>format<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtField<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Game Background</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameStyle <span style="color: #0066CC;">extends</span> ProductB
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtField:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><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> GameStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setWallName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Game Time!&quot;</span>,<span style="color: #cc66cc;">24</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">30</span>,txtField,format<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">url</span>=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;GameStyle.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Business background</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessStyle <span style="color: #0066CC;">extends</span> ProductB
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtField:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><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> BusinessStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setWallName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Serious Business&quot;</span>,<span style="color: #cc66cc;">24</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">30</span>,txtField,format<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">url</span>=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;BusinessStyle.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>By requesting the game products, the program returns both the rectangle shape buttons and the game background, and likewise with a request to the business products returns the business set. The separate concrete factories keep everything loose and the pattern has very few dependencies.</p>
<p><strong>The Helpers</strong></p>
<p>The RoundBtn class imports an ellipse button from the buttons package:</p>
<p><strong>import buttons.BtnStateEllipse; </strong></p>
<p>and the RecBtn class imports a rectangle button from the buttons package:</p>
<p><strong>import buttons.BtnState; </strong></p>
<p>So in creating the button scripts, the package indicates the buttons folder. The following scripts are found in the buttons folder. These helper classes are reusable just like the participants in a design pattern. However, because literals are used in these for the colors used with the button states, their flexibility is limited to the particular style and color scheme in the buttons.</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('p575code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57522"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
</pre></td><td class="code" id="p575code22"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Button style for games</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//Ellipse 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;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BtnBaseOval <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> btnLabel:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnWidth:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkground:Shape;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnBaseOval <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span>:uint,color2:uint,btnLabelText:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnLabel=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			btnLabel.<span style="color: #0066CC;">text</span>=btnLabelText;
			btnLabel.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">5</span>;
			btnLabel.<span style="color: #0066CC;">textColor</span>=0x24241c
			btnLabel.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #006600;">CENTER</span>;
&nbsp;
			format=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Arial Black&quot;</span><span style="color: #66cc66;">&#41;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">12</span>;
			btnLabel.<span style="color: #0066CC;">setTextFormat</span> <span style="color: #66cc66;">&#40;</span>format<span style="color: #66cc66;">&#41;</span>;
			btnWidth=<span style="color: #cc66cc;">90</span>;
&nbsp;
			bkground=<span style="color: #000000; font-weight: bold;">new</span> Shape;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span><span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span> <span style="color: #66cc66;">&#40;</span>.25,color2<span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,btnWidth,<span style="color: #cc66cc;">18</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addChild <span style="color: #66cc66;">&#40;</span>bkground<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>btnLabel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Button states and colors game buttons</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BtnStateEllipse <span style="color: #0066CC;">extends</span> SimpleButton
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnStateEllipse <span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			upState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseOval<span style="color: #66cc66;">&#40;</span>0x698c3a, 0x218894,txt<span style="color: #66cc66;">&#41;</span>;
			downState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseOval<span style="color: #66cc66;">&#40;</span>0xc7c68d,0x4d0005, txt<span style="color: #66cc66;">&#41;</span>;
			overState= <span style="color: #000000; font-weight: bold;">new</span> BtnBaseOval<span style="color: #66cc66;">&#40;</span>0x48f23,0x218894,txt<span style="color: #66cc66;">&#41;</span>;
			hitTestState=upState;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Button style for Business</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Set up rectangular 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;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BtnBaseRec <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> btnLabel:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnWidth:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkground:Shape;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnBaseRec <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span>:uint,color2:uint,btnLabelText:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnLabel=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> ;
			btnLabel.<span style="color: #0066CC;">text</span>=btnLabelText;
			btnLabel.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">5</span>;
			btnLabel.<span style="color: #0066CC;">textColor</span>=0x24241c;
			btnLabel.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
&nbsp;
			format=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Verdana&quot;</span><span style="color: #66cc66;">&#41;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">12</span>;
			btnLabel.<span style="color: #0066CC;">setTextFormat</span> <span style="color: #66cc66;">&#40;</span>format<span style="color: #66cc66;">&#41;</span>;
			btnWidth=<span style="color: #cc66cc;">90</span>;
&nbsp;
			bkground=<span style="color: #000000; font-weight: bold;">new</span> Shape;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span><span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span> <span style="color: #66cc66;">&#40;</span>.25,color2<span style="color: #66cc66;">&#41;</span>;
			bkground.<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>,btnWidth,<span style="color: #cc66cc;">18</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addChild <span style="color: #66cc66;">&#40;</span>bkground<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>btnLabel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Button states and colors business buttons</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BtnState <span style="color: #0066CC;">extends</span> SimpleButton
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnState <span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			upState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseRec<span style="color: #66cc66;">&#40;</span>0xa9b890, 0x24241c,txt<span style="color: #66cc66;">&#41;</span>;
			downState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseRec<span style="color: #66cc66;">&#40;</span>0xffff7f8,0xcd1722, txt<span style="color: #66cc66;">&#41;</span>;
			overState= <span style="color: #000000; font-weight: bold;">new</span> BtnBaseRec <span style="color: #66cc66;">&#40;</span>0xcd1722,0xa9b890,txt<span style="color: #66cc66;">&#41;</span>;
			hitTestState=upState;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In looking back over this example, keep in mind that these last set of classes, the helper classes, are not part of the pattern itself. Rather, they are used to make it easy to create buttons without resorting to either the button symbols made in Flash or Flex. However, if you’d prefer the factories to use the built-in button components rather than those in the helper classes, you will find it easy to do so. Just change the targets of creation in the concrete product classes where the buttons are assigned.</p>
<p><strong>When to Use the Abstract Factory</strong></p>
<p>The key element in the <strong>Abstract Factory Design Pattern</strong> is the idea of <em>families of related dependent objects</em>. After working with the pattern for a very short time, it offered up many solutions in my mind. None of us work with single programs—single projects at one time, certainly—but these projects are hardly single programs. They do indeed have certain parts that go together in one context but not another. So instead of having to work up an entire new program to work with families of these objects, this pattern allows you the convenience of working with one group or another depending on the project’s needs.</p>
<p><!--EndFragment--></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%2F01%2Factionscript-30-abstract-factory-design-pattern-multiple-products-and-factories%2F&amp;title=ActionScript%203.0%20Abstract%20Factory%20Design%20Pattern%3A%20Multiple%20Products%20and%20Factories" 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/2009/01/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

