<?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>Development in a Blink &#187; Design Patterns</title>
	<atom:link href="http://www.dougfinke.com/blog/index.php/category/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dougfinke.com/blog</link>
	<description>Researching the optimal; implementing the practical</description>
	<lastBuildDate>Fri, 03 May 2013 19:47:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Design Patterns in Dynamic Languages&#8211;PowerShell</title>
		<link>http://www.dougfinke.com/blog/index.php/2011/11/09/design-patterns-in-dynamic-languagespowershell/</link>
		<comments>http://www.dougfinke.com/blog/index.php/2011/11/09/design-patterns-in-dynamic-languagespowershell/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 01:06:47 +0000</pubDate>
		<dc:creator>Doug Finke</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Dynamic Languages]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Neal Ford]]></category>
		<category><![CDATA[Peter Norvig]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.dougfinke.com/blog/index.php/2011/11/09/design-patterns-in-dynamic-languagespowershell/</guid>
		<description><![CDATA[I was Googling for the Strategy Pattern and implementations done in dynamic languages and came across Neal Ford’s slides where he implements it in Groovy. Here are Peter Norvig’s slides, Design Patterns in Dynamic Languages, in it he states that 16 of the 23 GoF patterns have qualitatively simpler implementations in dynamic languages. Mr. Norvig [...]]]></description>
				<content:encoded><![CDATA[<p>I was Googling for the <a href="http://en.wikipedia.org/wiki/Strategy_pattern">Strategy Pattern</a> and implementations done in dynamic languages and came across <a href="http://www.hjug.org/present/Neal_Ford-Design_Patterns_in_Dynamic_Languages-slides.pdf">Neal Ford’s slides</a> where he implements it in <a href="http://groovy.codehaus.org/">Groovy</a>. Here are <a href="http://norvig.com/design-patterns/">Peter Norvig’s slides</a>, Design Patterns in Dynamic Languages, in it he states that 16 of the 23 <a href="http://en.wikipedia.org/wiki/Design_Patterns">GoF patterns</a> have qualitatively simpler implementations in dynamic languages. Mr. Norvig is Director of Research at Google.</p>
<p>One of Neal Ford’s slides captures the idea.</p>
<p><a href="http://www.dougfinke.com/blog/wp-content/uploads/2011/11/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto; padding-top: 0px" title="image" border="0" alt="image" src="http://www.dougfinke.com/blog/wp-content/uploads/2011/11/image_thumb.png" width="440" height="306" /></a></p>
<h3>The Strategy Pattern</h3>
<p>Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.</p>
<pre class="PowerShellColorizedScript"><span style="color: #00008b">function</span> <span style="color: #8a2be2">ql</span> <span style="color: #000000">{</span><span style="color: #ff4500">$args</span><span style="color: #000000">}</span>            
            
<span style="color: #00008b">function</span> <span style="color: #8a2be2">CalcByMult</span> <span style="color: #000000">{</span>            
    <span style="color: #00008b">param</span><span style="color: #000000">(</span><span style="color: #ff4500">$n</span><span style="color: #a9a9a9">,</span><span style="color: #ff4500">$m</span><span style="color: #000000">)</span>             
            
    <span style="color: #ff4500">$n</span><span style="color: #a9a9a9">*</span><span style="color: #ff4500">$m</span>            
<span style="color: #000000">}</span>            
            
<span style="color: #00008b">function</span> <span style="color: #8a2be2">CalcByManyAdds</span> <span style="color: #000000">{</span>            
    <span style="color: #00008b">param</span><span style="color: #000000">(</span><span style="color: #ff4500">$n</span><span style="color: #a9a9a9">,</span><span style="color: #ff4500">$m</span><span style="color: #000000">)</span>            
                
    <span style="color: #800080">1</span><span style="color: #a9a9a9">..</span><span style="color: #ff4500">$n</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">%</span> <span style="color: #000000">{</span><span style="color: #ff4500">$result</span> <span style="color: #a9a9a9">=</span> <span style="color: #800080">0</span><span style="color: #000000">}</span> <span style="color: #000000">{</span> <span style="color: #ff4500">$result</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$m</span> <span style="color: #000000">}</span> <span style="color: #000000">{</span><span style="color: #ff4500">$result</span><span style="color: #000000">}</span>            
<span style="color: #000000">}</span>            
            
            
<span style="color: #ff4500">$sampleData</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">@(</span>            
    <span style="color: #a9a9a9">,</span><span style="color: #000000">(</span><span style="color: #800080">3</span><span style="color: #a9a9a9">,</span><span style="color: #800080">4</span><span style="color: #a9a9a9">,</span><span style="color: #800080">12</span><span style="color: #000000">)</span>            
    <span style="color: #a9a9a9">,</span><span style="color: #000000">(</span><span style="color: #800080">5</span><span style="color: #a9a9a9">,</span><span style="color: #800080">-5</span><span style="color: #a9a9a9">,</span><span style="color: #800080">-25</span><span style="color: #000000">)</span>            
<span style="color: #000000">)</span>            
             
<span style="color: #ff4500">$strategies</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">ql</span> <span style="color: #8a2be2">CalcByMult</span> <span style="color: #8a2be2">CalcByManyAdds</span>            
             
<span style="color: #00008b">foreach</span><span style="color: #000000">(</span><span style="color: #ff4500">$Dataset</span> <span style="color: #00008b">in</span> <span style="color: #ff4500">$sampleData</span><span style="color: #000000">)</span> <span style="color: #000000">{</span>            
    <span style="color: #00008b">foreach</span><span style="color: #000000">(</span><span style="color: #ff4500">$strategy</span> <span style="color: #00008b">in</span> <span style="color: #ff4500">$strategies</span><span style="color: #000000">)</span> <span style="color: #000000">{</span>            
        <span style="color: #ff4500">$Dataset</span><span style="color: #a9a9a9">[</span><span style="color: #800080">2</span><span style="color: #a9a9a9">]</span> <span style="color: #a9a9a9">-eq</span> <span style="color: #000000">(</span><span style="color: #a9a9a9">&amp;</span> <span style="color: #ff4500">$strategy</span> <span style="color: #ff4500">$Dataset</span><span style="color: #a9a9a9">[</span><span style="color: #800080">0</span><span style="color: #a9a9a9">]</span> <span style="color: #ff4500">$Dataset</span><span style="color: #a9a9a9">[</span><span style="color: #800080">1</span><span style="color: #a9a9a9">]</span><span style="color: #000000">)</span>            
    <span style="color: #000000">}</span>            
<span style="color: #000000">}</span></pre>
<h3>Why Bother with Extra Structure?</h3>
<p>Here I am creating an array of PowerShell script blocks for the strategies, eliminating the ceremony of naming them.</p>
<pre class="PowerShellColorizedScript"><span style="color: #ff4500">$sampleData</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">@(</span>            
    <span style="color: #a9a9a9">,</span><span style="color: #000000">(</span><span style="color: #800080">3</span><span style="color: #a9a9a9">,</span><span style="color: #800080">4</span><span style="color: #a9a9a9">,</span><span style="color: #800080">12</span><span style="color: #000000">)</span>            
    <span style="color: #a9a9a9">,</span><span style="color: #000000">(</span><span style="color: #800080">5</span><span style="color: #a9a9a9">,</span><span style="color: #800080">-5</span><span style="color: #a9a9a9">,</span><span style="color: #800080">-25</span><span style="color: #000000">)</span>            
<span style="color: #000000">)</span>            
             
<span style="color: #ff4500">$strategies</span> <span style="color: #a9a9a9">=</span>             
<span style="color: #000000">{</span><span style="color: #00008b">param</span><span style="color: #000000">(</span><span style="color: #ff4500">$n</span><span style="color: #a9a9a9">,</span><span style="color: #ff4500">$m</span><span style="color: #000000">)</span> <span style="color: #ff4500">$n</span><span style="color: #a9a9a9">*</span><span style="color: #ff4500">$m</span><span style="color: #000000">}</span><span style="color: #a9a9a9">,</span>            
<span style="color: #000000">{</span>            
    <span style="color: #00008b">param</span><span style="color: #000000">(</span><span style="color: #ff4500">$n</span><span style="color: #a9a9a9">,</span><span style="color: #ff4500">$m</span><span style="color: #000000">)</span>            
    <span style="color: #800080">1</span><span style="color: #a9a9a9">..</span><span style="color: #ff4500">$n</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">%</span> <span style="color: #000000">{</span><span style="color: #ff4500">$result</span> <span style="color: #a9a9a9">=</span> <span style="color: #800080">0</span><span style="color: #000000">}</span> <span style="color: #000000">{</span> <span style="color: #ff4500">$result</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$m</span> <span style="color: #000000">}</span> <span style="color: #000000">{</span><span style="color: #ff4500">$result</span><span style="color: #000000">}</span>            
<span style="color: #000000">}</span>            
             
<span style="color: #00008b">foreach</span><span style="color: #000000">(</span><span style="color: #ff4500">$Dataset</span> <span style="color: #00008b">in</span> <span style="color: #ff4500">$sampleData</span><span style="color: #000000">)</span> <span style="color: #000000">{</span>            
    <span style="color: #00008b">foreach</span><span style="color: #000000">(</span><span style="color: #ff4500">$strategy</span> <span style="color: #00008b">in</span> <span style="color: #ff4500">$strategies</span><span style="color: #000000">)</span> <span style="color: #000000">{</span>            
        <span style="color: #ff4500">$Dataset</span><span style="color: #a9a9a9">[</span><span style="color: #800080">2</span><span style="color: #a9a9a9">]</span> <span style="color: #a9a9a9">-eq</span> <span style="color: #000000">(</span><span style="color: #a9a9a9">&amp;</span> <span style="color: #ff4500">$strategy</span> <span style="color: #ff4500">$Dataset</span><span style="color: #a9a9a9">[</span><span style="color: #800080">0</span><span style="color: #a9a9a9">]</span> <span style="color: #ff4500">$Dataset</span><span style="color: #a9a9a9">[</span><span style="color: #800080">1</span><span style="color: #a9a9a9">]</span><span style="color: #000000">)</span>            
    <span style="color: #000000">}</span>            
<span style="color: #000000">}</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dougfinke.com/blog/index.php/2011/11/09/design-patterns-in-dynamic-languagespowershell/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>You can do what in C#? &#8211; implicit operator</title>
		<link>http://www.dougfinke.com/blog/index.php/2009/09/14/you-can-do-what-in-c-implicit-operator/</link>
		<comments>http://www.dougfinke.com/blog/index.php/2009/09/14/you-can-do-what-in-c-implicit-operator/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 02:25:02 +0000</pubDate>
		<dc:creator>Doug Finke</dc:creator>
				<category><![CDATA[Builder Pattern]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[GoF]]></category>
		<category><![CDATA[Implicit Operator]]></category>

		<guid isPermaLink="false">http://dougfinke.com/blog/index.php/2009/09/14/you-can-do-what-in-c-implicit-operator/</guid>
		<description><![CDATA[This post Fluent Interface for System.Identity &#8211; Kind&#8217;s Expression Builder got me thinking about the GoF Builder Pattern and how to turn a legacy C# class with a constructor into a Fluent Interface with a Builder and Implicit Operator. Notice how DwellingBuilder returns a Dwelling. This is easily implemented through C#&#8217;s implicit operator. This is [...]]]></description>
				<content:encoded><![CDATA[</p>
<p>This post <a href="http://blogs.msdn.com/dave_langer/archive/2009/09/13/fluent-interface-for-system-identity-kind-s-expression-builder.aspx">Fluent Interface for System.Identity &ndash; Kind&rsquo;s Expression Builder</a> got me thinking about the <a href="http://en.wikipedia.org/wiki/Builder_pattern">GoF Builder Pattern</a> and how to turn a legacy C# class with a constructor into a <a href="http://www.martinfowler.com/dslwip/ExpressionBuilder.html">Fluent Interface</a> with a Builder and Implicit Operator.</p>
<p>Notice how DwellingBuilder returns a Dwelling. This is easily implemented through C#&rsquo;s implicit operator. This is the <em>Product </em>defined by the builder pattern.</p>
<h3>Using the Fluent Interface</h3>
<p><a href="http://dougfinke.com/uploadPictures/IdidntknowyoucoulddothatinCimplicitopera_12DDF/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://dougfinke.com/uploadPictures/IdidntknowyoucoulddothatinCimplicitopera_12DDF/image_thumb.png" width="380" height="81" /></a> </p>
<h3>Dwelling Builder</h3>
<pre class="PowerShellColorizedScript"><span style="color: #0000ff">public</span> <span style="color: #8a2be2">class</span> <span style="color: #8a2be2">DwellingBuilder</span>            
<span style="color: #000000">{</span>            
    <span style="color: #0000ff">int</span> <span style="color: #8a2be2">Bedrooms</span> <span style="color: #000000">{</span> <span style="color: #0000ff">get</span><span style="color: #000000">;</span> <span style="color: #0000ff">set</span><span style="color: #000000">;</span> <span style="color: #000000">}</span>            
    <span style="color: #0000ff">int</span> <span style="color: #8a2be2">Closets</span> <span style="color: #000000">{</span> <span style="color: #0000ff">get</span><span style="color: #000000">;</span> <span style="color: #0000ff">set</span><span style="color: #000000">;</span> <span style="color: #000000">}</span>            
    <span style="color: #0000ff">int</span> <span style="color: #8a2be2">Floor</span> <span style="color: #000000">{</span> <span style="color: #0000ff">get</span><span style="color: #000000">;</span> <span style="color: #0000ff">set</span><span style="color: #000000">;</span> <span style="color: #000000">}</span>            
            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">static</span> <span style="color: #8a2be2">implicit</span> <span style="color: #8a2be2">operator</span> <span style="color: #8a2be2">Dwelling</span><span style="color: #000000">(</span><span style="color: #0000ff">DwellingBuilder</span> <span style="color: #8a2be2">builder</span><span style="color: #000000">)</span>            
    <span style="color: #000000">{</span>            
        <span style="color: #00008b">return</span> <span style="color: #0000ff">new</span> <span style="color: #8a2be2">Dwelling</span><span style="color: #000000">(</span>            
          <span style="color: #0000ff">builder.Bedrooms</span><span style="color: #a9a9a9">,</span>             
          <span style="color: #8a2be2">builder.Closets</span><span style="color: #a9a9a9">,</span>             
          <span style="color: #8a2be2">builder.Floor</span>            
       <span style="color: #000000">)</span><span style="color: #000000">;</span>            
    <span style="color: #000000">}</span>            
            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">DwellingBuilder</span> <span style="color: #8a2be2">NumberOfBedrooms</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span> <span style="color: #8a2be2">p</span><span style="color: #000000">)</span>            
    <span style="color: #000000">{</span>            
        <span style="color: #0000ff">Bedrooms</span> <span style="color: #8a2be2">=</span> <span style="color: #8a2be2">p</span><span style="color: #000000">;</span>            
        <span style="color: #00008b">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">;</span>            
    <span style="color: #000000">}</span>            
            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">DwellingBuilder</span> <span style="color: #8a2be2">NumberOfClosets</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span> <span style="color: #8a2be2">p</span><span style="color: #000000">)</span>            
    <span style="color: #000000">{</span>            
        <span style="color: #0000ff">Closets</span> <span style="color: #8a2be2">=</span> <span style="color: #8a2be2">p</span><span style="color: #000000">;</span>            
        <span style="color: #00008b">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">;</span>            
    <span style="color: #000000">}</span>            
            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">DwellingBuilder</span> <span style="color: #8a2be2">OnFloor</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span> <span style="color: #8a2be2">p</span><span style="color: #000000">)</span>            
    <span style="color: #000000">{</span>            
        <span style="color: #0000ff">Floor</span> <span style="color: #8a2be2">=</span> <span style="color: #8a2be2">p</span><span style="color: #000000">;</span>            
        <span style="color: #00008b">return</span> <span style="color: #0000ff">this</span><span style="color: #000000">;</span>            
    <span style="color: #000000">}</span>            
<span style="color: #000000">}</span></pre>
<h3>Legacy Dwelling Class</h3>
<pre class="PowerShellColorizedScript"><span style="color: #0000ff">public</span> <span style="color: #8a2be2">class</span> <span style="color: #8a2be2">Dwelling</span>            
<span style="color: #000000">{</span>            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">int</span> <span style="color: #8a2be2">Bedrooms</span> <span style="color: #000000">{</span> <span style="color: #0000ff">get</span><span style="color: #000000">;</span> <span style="color: #0000ff">set</span><span style="color: #000000">;</span> <span style="color: #000000">}</span>            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">int</span> <span style="color: #8a2be2">Closets</span> <span style="color: #000000">{</span> <span style="color: #0000ff">get</span><span style="color: #000000">;</span> <span style="color: #0000ff">set</span><span style="color: #000000">;</span> <span style="color: #000000">}</span>            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">int</span> <span style="color: #8a2be2">Floor</span> <span style="color: #000000">{</span> <span style="color: #0000ff">get</span><span style="color: #000000">;</span> <span style="color: #0000ff">set</span><span style="color: #000000">;</span> <span style="color: #000000">}</span>            
            
    <span style="color: #0000ff">public</span> <span style="color: #8a2be2">Dwelling</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span> <span style="color: #8a2be2">bedrooms</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">int</span> <span style="color: #8a2be2">closets</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">int</span> <span style="color: #8a2be2">floor</span><span style="color: #000000">)</span>            
    <span style="color: #000000">{</span>            
        <span style="color: #0000ff">Bedrooms</span> <span style="color: #8a2be2">=</span> <span style="color: #8a2be2">bedrooms</span><span style="color: #000000">;</span>            
        <span style="color: #0000ff">Closets</span> <span style="color: #8a2be2">=</span> <span style="color: #8a2be2">closets</span><span style="color: #000000">;</span>            
        <span style="color: #0000ff">Floor</span> <span style="color: #8a2be2">=</span> <span style="color: #8a2be2">Floor</span><span style="color: #000000">;</span>            
    <span style="color: #000000">}</span>            
<span style="color: #000000">}</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dougfinke.com/blog/index.php/2009/09/14/you-can-do-what-in-c-implicit-operator/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Erich Gamma &#8211; How (7 years of) Eclipse Changed my Views on Software Development</title>
		<link>http://www.dougfinke.com/blog/index.php/2009/02/14/erich-gamma-how-7-years-of-eclipse-changed-my-views-on-software-development/</link>
		<comments>http://www.dougfinke.com/blog/index.php/2009/02/14/erich-gamma-how-7-years-of-eclipse-changed-my-views-on-software-development/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 13:55:40 +0000</pubDate>
		<dc:creator>Doug Finke</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Erich Gamma]]></category>

		<guid isPermaLink="false">http://dougfinke.com/blog/index.php/2009/02/14/erich-gamma-how-7-years-of-eclipse-changed-my-views-on-software-development/</guid>
		<description><![CDATA[http://www.infoq.com/presentations/Eclipse-Lessons-Erich-Gamma Erich Gamma&#160; is a co-author of&#160; Design Patterns: Elements of Reusable Object-Oriented Software]]></description>
				<content:encoded><![CDATA[<p><a title="http://www.infoq.com/presentations/Eclipse-Lessons-Erich-Gamma" href="http://www.infoq.com/presentations/Eclipse-Lessons-Erich-Gamma">http://www.infoq.com/presentations/Eclipse-Lessons-Erich-Gamma</a></p>
<p><b>Erich Gamma</b>&#160; is a co-author of&#160; <a href="http://en.wikipedia.org/wiki/Design_Patterns_(book)">Design Patterns: Elements of Reusable Object-Oriented Software</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougfinke.com/blog/index.php/2009/02/14/erich-gamma-how-7-years-of-eclipse-changed-my-views-on-software-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
