<?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; Creates</title>
	<atom:link href="http://www.as3dp.com/category/class-relations/creates/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>ActionsScript 3.0 Design Pattern Relations Part IV: Creation</title>
		<link>http://www.as3dp.com/2010/02/actionsscript-3-0-design-pattern-relations-part-iv-creation/</link>
		<comments>http://www.as3dp.com/2010/02/actionsscript-3-0-design-pattern-relations-part-iv-creation/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 02:26:33 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Class Relations]]></category>
		<category><![CDATA[Creates]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2685</guid>
		<description><![CDATA[One of the least discussed relations in Design Patterns is where one participant creates an instance of another. Basically, the pattern calls for one class to instantiate another class. This relationship is indicated by a broken line with an arrowhead pointing to the class that has been instantiated. (The Participant Relations diagram above shows Class [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2788" class="wp-caption alignnone" style="width: 411px"><img src="http://www.as3dp.com/wp-content/uploads/2010/02/dpUMLcreatesRev.png" alt="Creates Relationship" title="dpUMLcreatesRev" width="401" height="398" class="size-full wp-image-2788" /><p class="wp-caption-text">Creates Relationship</p></div>One of the least discussed  relations in Design Patterns is where one participant <strong>creates</strong> an instance of another. Basically, the pattern calls for one class to instantiate another class. This relationship is indicated by a <strong>broken line with an arrowhead</strong> pointing to the class that has been instantiated. (The Participant Relations diagram above shows <strong>Class C </strong>instantiating (creates) <strong>Class F</strong> with the red broken line and arrow.) The <em>creates relationship</em> is found in only five design patterns:</p>
<ul>
<li><a href="http://www.as3dp.com/category/design-patterns/abstract-factory-pattern/">Abstract Factory</a></li>
<li><a href="http://www.as3dp.com/category/design-patterns/builder/">Builder</a></li>
<li><a href="http://www.as3dp.com/category/design-patterns/factory-method/">Factory Method</a></li>
<li><a href="http://cdn.oreilly.com/digitalmedia/9780596528461/ActionScript3DesignPattern_ch07.pdf">Command</a></li>
<li><a href="http://www.as3dp.com/category/design-patterns/factory-method/">Memento</a></li>
</ul>
<p>This post examines how design patterns incorporating a <em>creates relationship</em> uses that relationship to make a loosely structured design to complete one or more kinds of tasks.<br />
<span id="more-2685"></span><br />
<strong>Uses of Creates</strong></p>
<p>In virtually any OOP programming, some sort of <em>creates relationship</em> exists; sometimes no more than a Client instantiating an instance of a class. However, when dealing with design patterns, we are looking for a specific purpose in terms of the overall design for the use of a creates relationship. For example, in the Memento pattern, the Originator instantiates a Memento object with the following 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('p2685code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p26852"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2685code2"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createMemento <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Memento
<span style="color: #66cc66;">&#123;</span>
       <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Creating memento with current state&quot;</span><span style="color: #66cc66;">&#41;</span>;
       <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Memento<span style="color: #66cc66;">&#40;</span>mstate<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the Memento pattern, the Memento class has an aggregate relationship with the Caretaker participant which stores the current state in an array (in our example.) In looking closely at the relationship between the Originator and the Memento, when the Originator instantiates an instance of the Memento, it is really acting as a setter—it sets a specified state as the memento, which in turn is stored by the Caretaker when called upon. Effectively, the process decouples the Originator from the Caretaker.</p>
<p><strong>Factory Work</strong></p>
<p>In thinking of the <em>create relationship</em>, one of the Factory designs most likely comes to mind. Not surprisingly, both the Abstract Factory and Factory Method include a create relationship. The Concrete Creator class has a create relationship with the Product class. The whole concept of a <em><strong>factory</strong></em> is one where something is created, and that&#8217;s exactly where we&#8217;d expect to find a create relationship.</p>
<p>One apparent exception is the Flyweight Design Pattern. No create relationship is seen in the Class Diagram, but in looking at the Factory participant in the design, you will find that elements in an associative array are made up of instantiated ConcreteFlyweight instances. (Associative arrays in ActionScript 3.0 are created as Objects and the elements are the Object properties.) The more complex multiple aggregation masks the create relationship.</p>
<p><strong>The Uses-A Relationship</strong></p>
<p>Of all of the ways to describe what happens with the dashed line and arrow, I prefer the <strong>uses-a</strong> description. This is consistent with <strong>has-a</strong> (composition) and <strong>is-a</strong> (inheritance) relationships. It&#8217;s easy to say that <em>Participant A <strong>uses a</strong> Participant B object through instantiation</em>. GoF simply call it the <em>creates relationship</em> (p. 364), and let it go at that. Scott Myers in his book <em>Effective C++</em> uses the term <strong>is-implemented-in-terms-of</strong> to reference such a relationship. Myers points out, <em>IS-IMPLEMENTED-IN-TERMS-OF is a relationship between classes that exists in the implementation domain</em>. The <strong>implementation domain</strong> is contrasted with the<strong> application domain</strong> where you have has-a relationships (composition.)</p>
<p>In any event, this concludes our series on relationships between design pattern participants. We hope it helps you better understand what all of the symbols mean, and while the lines of demarcation between types of relationships may become blurred, at least you have the basics so that you can better understand a design pattern&#8217;s class diagram and what&#8217;s going on in a pattern. Start looking at your own code in terms of these relationships, and <em>Who knows?</em>, maybe you&#8217;ll discover you&#8217;ve been using one that you didn&#8217;t realize before.</p>
<p>Your valuable comments are welcomed as always, along with questions, insights and examples.</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%2F02%2Factionsscript-3-0-design-pattern-relations-part-iv-creation%2F&amp;title=ActionsScript%203.0%20Design%20Pattern%20Relations%20Part%20IV%3A%20Creation" 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/2010/02/actionsscript-3-0-design-pattern-relations-part-iv-creation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

