<?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>The Grubbsian &#187; General Programming</title>
	<atom:link href="http://www.thegrubbsian.com/category/general-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thegrubbsian.com</link>
	<description>Writings on .NET, Ruby, JavaScript, HTML/CSS, Design, and all things Web.</description>
	<lastBuildDate>Sun, 01 Aug 2010 20:57:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>The Reusable Test Scope Pattern</title>
		<link>http://www.thegrubbsian.com/2009/08/22/the-reusable-test-scope-pattern/</link>
		<comments>http://www.thegrubbsian.com/2009/08/22/the-reusable-test-scope-pattern/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 00:31:27 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=251</guid>
		<description><![CDATA[I’m almost positive someone has come up with this pattern before, but it was a big help on a recent project so I thought I’d share it. The basic goal of the pattern is to make the setup and teardown work of test classes reusable. We found this to cause particular pain for integration tests [...]]]></description>
			<content:encoded><![CDATA[<p>I’m almost positive someone has come up with this pattern before, but it was a big help on a recent project so I thought I’d share it.  The basic goal of the pattern is to make the setup and teardown work of test classes reusable.  We found this to cause particular pain for integration tests written against a database where multiple dependent records needed to be created before the actual test could be performed.  The second goal was that these externalized bits of code would be composable so that tests requiring multiple setup resources could create just the right ones.</p>
<p>Before really describing the workflow of the pattern, here are the basic components of our implementation:</p>
<p><b>The ITestScope Interface</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ITestScope <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">void</span> SetupScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">void</span> TeardownScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>These two methods (SetupScope and TeardownScope) are where we move all of the test setup and cleanup logic.  Any state that needs to be maintained between setup and teardown (i.e. needs to be available to the test itself) can be stored in public properties or fields on the class implementing ITestScope.</p>
<p><b>The TestBase Class</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> TestBase <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> ScopeSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> abstract <span style="color: #0600FF;">void</span> ScopeTeardown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Requiring that all test classes inherit from TestBase insures that at least developers will be considering the setup and cleanup of their tests.  The TestBase.ScopeSetup method would include instantiating and calling the TestScope.SetupScope methods for any scopes that are needed in the test class&#8230;likewise for teardown.</p>
<p><b>The TestFactory Class</b></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> TestFactory <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TRepository GetRepository<span style="color: #008000;">&lt;</span>TRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> where TRepository <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TController GetController<span style="color: #008000;">&lt;</span>TController<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> where TController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This isn&#8217;t directly related to the scope pattern, but it&#8217;s invaluable as a point where you can use a DI container in the test project.  All repository or controllers (or other resources) can be obtained from here.</p>
<p><b>An Example</b></p>
<p>So here&#8217;s an example test class that might use this pattern.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerTestScope <span style="color: #008000;">:</span> ITestScope <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> ICustomerRepository _customerRepo<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Customer TestCustomer <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> CustomerTestScope<span style="color: #000000;">&#40;</span>ICustomerRepository customerRepo<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_customerRepo <span style="color: #008000;">=</span> customerRepo<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SetupScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		TestCustomer <span style="color: #008000;">=</span> _customerRepo.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// Other complex test setup logic</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TeardownScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_customerRepo.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>TestCustomer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008080; font-style: italic;">// The reverse of the complex setup logic</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>TestClass<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerRepositoryTests <span style="color: #008000;">:</span> TestBase <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> CustomerTestScope _customerScope<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> ICustomerRepository _customerRepo<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>TestInitialize<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ScopeSetup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		_customerRepo <span style="color: #008000;">=</span> TestFactory.<span style="color: #0000FF;">GetRepository</span><span style="color: #008000;">&lt;</span>ICustomerRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		_customerScope <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CustomerTestScope<span style="color: #000000;">&#40;</span>_customerRepo<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		_customerScope.<span style="color: #0000FF;">SetupScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SaveCustomer_ShouldRenameCustomer_WhenNewNameGiven<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
		_customerScope.<span style="color: #0000FF;">TestCustomer</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;New Name&quot;</span><span style="color: #008000;">;</span>
		_customerRepo.<span style="color: #0000FF;">Save</span><span style="color: #000000;">&#40;</span>_customerScope.<span style="color: #0000FF;">TestCustomer</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		var customer <span style="color: #008000;">=</span> _customerRepo.<span style="color: #0000FF;">GetById</span><span style="color: #000000;">&#40;</span>_customerScope.<span style="color: #0000FF;">TestCustomer</span>.<span style="color: #0000FF;">ID</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		Assert.<span style="color: #0000FF;">IsTrue</span><span style="color: #000000;">&#40;</span>customer.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;New Name&quot;</span>, 
			<span style="color: #666666;">&quot;SaveCustomer failed to update customer name.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#91;</span>TestCleanup<span style="color: #000000;">&#93;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ScopeTeardown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		_customerScope.<span style="color: #0000FF;">TeardownScope</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If additional scopes were needed then they could simple be added to the test class.  For example, if a ProductTestScope was also needed then it would be initialized just like the CustomerTestScope and torn down appropriately.  </p>
<p>Dependencies between scopes would be supplied via constructor parameters.  This does imply that the SetupScope methods be called in sequence and TeardownScope be called in reverse order.</p>
<p>That&#8217;s the pattern, pretty simple but now the logic for creating these setup conditions can be offloaded and reused in a simple way and we&#8217;ve made it easy to clean up that test data after the tests complete.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/08/22/the-reusable-test-scope-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thoughts on Legacy Code</title>
		<link>http://www.thegrubbsian.com/2009/08/01/thoughts-on-legacy-code/</link>
		<comments>http://www.thegrubbsian.com/2009/08/01/thoughts-on-legacy-code/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 19:57:31 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=223</guid>
		<description><![CDATA[I was listening to a recent Hanselminutes podcast episode in which Scott spoke with Michael Feathers of ObjectMentor on the topic of legacy code. They touched on a number of really effective techniques for approaching a legacy code-base and I wanted to echo some of their thoughts and add a few of my own. First, [...]]]></description>
			<content:encoded><![CDATA[<p>I was listening to a recent <a href="http://www.hanselminutes.com/">Hanselminutes</a> podcast <a href="http://www.hanselminutes.com/default.aspx?showID=183">episode</a> in which Scott spoke with <a href="http://www.michaelfeathers.com/">Michael Feathers</a> of <a href="http://www.objectmentor.com/">ObjectMentor</a> on the topic of <a href="http://en.wikipedia.org/wiki/Legacy_code">legacy code</a>.  They touched on a number of really effective techniques for approaching a legacy code-base and I wanted to echo some of their thoughts and add a few of my own.</p>
<p>First, the definition of legacy code (as was discussed in the show) is really much broader than the face value of the phrase itself.  In reality it&#8217;s harder to define &#8220;legacy code&#8221; than it is to know it when you see it.  Generally I know I&#8217;m dealing with legacy code when any of the following happen:</p>
<p>1. I&#8217;m fearful of making changes&#8230;the magic box where this code lives might get angry<br />
2. I&#8217;m confused and lost after more than a few clicks of &#8220;Go to Definition&#8221;<br />
3. I find few or no tests, or the quality of tests is poor<br />
4. There are tests but I get mixed or untrustworthy results from running them<br />
5. I have to write more lines of setup code to write a test than the lines of code that I want to test<br />
6. I can find nobody who will admit to understanding (or writing) this code</p>
<p>Doubtless there are more than just these indicators to tell you that you&#8217;re dealing with legacy code, but these are red-flags for sure.  So what are some high-level techniques that we can use to help bring legacy code into the present&#8230;well, here are a few:</p>
<p><b>Under The Rug</b><br />
We&#8217;ve all done it at home&#8230;there&#8217;s that last little bit of something that you missed with the broom.  So, you lift up the corner of the living room rug and kick it under there with your foot.  While you&#8217;re mom probably wouldn&#8217;t approve, sometimes with code this can be an acceptable approach &#8211; provided you acknowledge what you&#8217;re doing.  What I mean by sweeping it under the rug is that you wrap a section of &#8220;legacy code&#8221; in a bit of more modern, testable code.  In this way you can continue to use the legacy sections but a cleaner interface can be created for testing.</p>
<p><b>The Strangler Application</b><br />
This is a pattern that was introduced to me in the Hanselminutes podcast.  The basic premise is an extension of the &#8220;Under the Rug&#8221; concept.  But instead of just wrapping the code in a clean exterior, we actually take that opportunity to slowly &#8220;choke&#8221; out the old code.  </p>
<p>One technique that can be employed to slowly transform the codebase is using the &#8220;I&#8221; in SOLID&#8230;Interface Segregation.  Applying one or more interfaces to each legacy class allows you to incrementally swap out implementations with clean, new code.</p>
<p><b>Embrace Continual Design</b><br />
One thought that came out in the Hanselminutes episode was that design is never over.  And I think this is really important to realize.  Even in production/support mode, design is something that needs to be addressed.  When extending an application, are we setting it up for continued success and maintainability.  When re-factoring something or transforming it to un-legacy code, are we taking care that it won&#8217;t regress back into legacy code.  If a team is focused on continual design it&#8217;s less likely for code to become legacy in the first place.</p>
<p><b>Posthumous Test Coverage</b><br />
A third approach to legacy code is by testing the crap out of it.  One could even make the argument that this is a pre-requisite to the previous three approaches.  Spending a lot of time covering legacy code in tests has two major benefits.  First, it helps the developer really understand the intent of the code, it&#8217;s style, quirks, and patterns.  Secondly, it does what tests do best&#8230;helps make code changes safer.  If a codebase is adequately covered in tests then making changes becomes less scary and potential regression is reduced.</p>
<p><b>Forklift</b><br />
Lastly, there is one approach to legacy code that is rarely mentioned&#8230;and rightly so, it&#8217;s the most aggressive and can be the most costly.  Start over&#8230;sometimes it&#8217;s the best approach.</p>
<p>Once in my life I had to deal with a massive codebase that had been through many, many, cycles of attempted refactorings.  The years had left this poor application in a state of wild and unruly disrepair.  After discussing all the options with the client it became evident that actually starting over was the best (and in this case, cheapest) way to accomplish the goal.</p>
<p>Starting over can be daunting but if you attack it in a practical and measured way, and possibly mix it with the other techniques described here it can be liberating.  It&#8217;s important to have a good requirements phase when starting over, you can&#8217;t just look at legacy code and start re-writing it.  And you need to have all the trappings of a well staffed project in place as well, including strong QA.</p>
<p>~~</p>
<p>So those are some thoughts on legacy code.  As always, I&#8217;d appreciate hearing what everyone else thinks&#8230;what other techniques have you used?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/08/01/thoughts-on-legacy-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Server Block Formatting in Visual Studio</title>
		<link>http://www.thegrubbsian.com/2009/04/22/server-block-formatting-in-visual-studio/</link>
		<comments>http://www.thegrubbsian.com/2009/04/22/server-block-formatting-in-visual-studio/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 21:04:54 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=183</guid>
		<description><![CDATA[This is one of those more-for-me-than-you posts. If you&#8217;re working with ASP.NET MVC (or god help you ASP Classic) you&#8217;ll likely be annoyed by the way server blocks are formatted in Visual Studio. In particular closing curly braces seem to throw the formatting engine a real curve-ball. For example, you type something like this: &#60;% [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of those more-for-me-than-you posts.</p>
<p>If you&#8217;re working with ASP.NET MVC (or god help you ASP Classic) you&#8217;ll likely be annoyed by the way server blocks are formatted in Visual Studio.  In particular closing curly braces seem to throw the formatting engine a real curve-ball.  For example, you type something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span> <span style="color: #000000;">&#125;</span> <span style="color: #008000;">%&gt;</span></pre></div></div>

<p>But after slapping return, you ultimately get this lovely thing:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>
       <span style="color: #000000;">&#125;</span>
     <span style="color: #008000;">%&gt;</span></pre></div></div>

<p>Thanks Visual Studio!</p>
<p>So how do we fix this.  Well, you&#8217;d like it if hitting Ctrl+K+D took care of this for you, but apparently that ignores server blocks.  Next you&#8217;ll hunt and peck your way through the mounds of Visual Studio options in Tools > Options&#8230;but that search will ultimately prove fruitless.  Finally, you&#8217;ll turn to the humble macro&#8230;I know macro can be a dirty word but in this case it&#8217;s really your only option.</p>
<p>Here&#8217;s how this goes:</p>
<p>1. Mouse over to Tools > Macros > Macro IDE<br />
2. Notice that Visual Studio macros are in Visual Basic&#8230;really? I know&#8230;but go with it.<br />
3. Right-click &#8216;My Macros&#8217;<br />
4. Choose &#8216;Add New Item&#8217;<br />
5. Choose &#8216;Module&#8217; in the dialog<br />
6. Enter the following method in the newly created Module:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Public</span> Module FormatCurrentFile
&nbsp;
    <span style="color: #000080;">Sub</span> FixServerBlocks()
        <span style="color: #000080;">Dim</span> selection <span style="color: #000080;">As</span> TextSelection = DTE.ActiveDocument.Selection
        <span style="color: #000080;">Dim</span> fixed <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;&lt;% } %&gt;&quot;</span>
        <span style="color: #000080;">Dim</span> regex <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;\&lt;\%:Wh*\}:Wh*\%\&gt;&quot;</span>
        <span style="color: #000080;">While</span> selection.FindPattern(regex, vsFindOptions.vsFindOptionsRegularExpression)
            selection.ReplacePattern(regex, fixed, vsFindOptions.vsFindOptionsRegularExpression)
        <span style="color: #000080;">End</span> <span style="color: #000080;">While</span>
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">End</span> Module</pre></div></div>

<p>Now you can go through Tools > Options > Keyboard and map a key command to this macro&#8230;I chose Ctrl+K+C for example since it&#8217;s close to the other magic formatting keys that you&#8217;ll want to run right before the macro.  There you have it.</p>
<p><i>Thanks to some help from StackOverflow.com for this solution: <a href="http://stackoverflow.com/questions/720042">http://stackoverflow.com/questions/720042</a></i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/04/22/server-block-formatting-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Study in Patterns: The State Pattern</title>
		<link>http://www.thegrubbsian.com/2009/01/20/a-study-in-patterns-the-state-pattern/</link>
		<comments>http://www.thegrubbsian.com/2009/01/20/a-study-in-patterns-the-state-pattern/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 07:06:38 +0000</pubDate>
		<dc:creator>JC Grubbs</dc:creator>
				<category><![CDATA[General Programming]]></category>

		<guid isPermaLink="false">http://www.thegrubbsian.com/?p=102</guid>
		<description><![CDATA[I&#39;m a big fan of software development patterns. &#0160;They&#39;re an excellent way to get the ball rolling when you&#39;re faced with a puzzling design situation. &#0160;But sometimes they&#39;re difficult to grok and it can be confusing to know when and how to implement just the right pattern for the circumstance. &#0160;So to improve my own [...]]]></description>
			<content:encoded><![CDATA[<p>I&#39;m a big fan of software development patterns. &#0160;They&#39;re an excellent way to get the ball rolling when you&#39;re faced with a puzzling design situation. &#0160;But sometimes they&#39;re difficult to grok and it can be confusing to know when and how to implement just the right pattern for the circumstance. &#0160;So to improve my own skill and knowledge with patterns I&#39;m going to start a series of posts exploring some useful patterns &#8211; undoubtedly these posts will be as much for me as anyone who reads them.</p>
<p>One of the patterns I&#39;ve recently found useful is the State Pattern. &#0160;If you&#39;ve ever used an enumeration to define or transition an object&#39;s state, or written an elaborate switch statement with nested &quot;if&quot; blocks to modify an object&#39;s behavior depending on its state then the State Pattern might be right up your ally. &#0160;The GoF defines the State Pattern as:</p>
<blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><p><span style="color: #555555; font-size: 14px; line-height: 20px; ">A<span style="font-style: italic;">llow&#0160;an object to alter its behavior when its internal state changes. The object will appear to change its class.</span></span></p>
</blockquote>
<p>This definition is all well and good (and I might argue with the &quot;change its class&quot; business), but how would you actually implement such magic. &#0160;Let&#39;s use the following model as an example:</p>
<p><span style="color: #0000ff; text-decoration: underline;"><a href="http://jcgrubbs.typepad.com/.a/6a00e54ef127298833010536e05468970b-pi" style="display: block; "><img alt="State-pattern-class-diagram" border="0" class="at-xid-6a00e54ef127298833010536e05468970b selected " src="http://jcgrubbs.typepad.com/.a/6a00e54ef127298833010536e05468970b-pi" style="width: 575px; " title="State-pattern-class-diagram" /></a><br />
<br /></span></p>
<p>The basic premise is that we hide changes in our implementation of methods like &quot;Graduate&quot; and &quot;IssueTranscript&quot; behind the private field &quot;_state&quot; which is a reference to an IStudentState. &#0160;Our public methods Student.Graduate() and Student.IssueTranscript() call the implementations of _state.Graduate() and _state.IssueTranscript(). &#0160;If _state happens to be an instance of ApplicationState then IssueTranscript() will likely perform a different action than if _state was a GraduatedState.</p>
<p>Another thing you&#39;ll notice is the reference that the &quot;state&quot; classes have back to Student. &#0160;There are likely alternate implementations of this pattern that avoid this backward reference, but you might find that you need some context in your implementation methods within the state classes.</p>
<p>So you can begin to see how this can reduce the amount of complex switch/if logic you&#39;ll have. &#0160;It may seem like it adds complexity in that you have more classes to content with, but after using this pattern a few times you&#39;ll be grateful of the clean segregation of behaviors achieve with the State Pattern.</p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegrubbsian.com/2009/01/20/a-study-in-patterns-the-state-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
