<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://codebetter.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Greg Young [MVP] - All Comments</title><link>http://codebetter.com/blogs/gregyoung/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Long story short: Fixed price &amp;amp; Agile | Mission Control</title><link>http://codebetter.com/blogs/gregyoung/archive/2006/07/07/147179.aspx#677711</link><pubDate>Sat, 13 Mar 2010 12:08:10 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:677711</guid><dc:creator>Long story short: Fixed price &amp; Agile | Mission Control</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Long story short: Fixed price &amp;amp; Agile | Mission Control&lt;/p&gt;
&lt;img src="http://codebetter.com/aggbug.aspx?PostID=677711" width="1" height="1"&gt;</description></item><item><title>re: CQRS and Event Sourcing</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/02/13/cqrs-and-event-sourcing.aspx#677593</link><pubDate>Fri, 12 Mar 2010 04:31:56 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:677593</guid><dc:creator>Joe Balfantz</dc:creator><description>&lt;p&gt;I&amp;#39;m warming up to CQRS and Event Sourcing (I nearly used a similar pattern two years ago), but I&amp;#39;m curious how you reconstitute a current snapshot of an object through an event log? &amp;nbsp;Here&amp;#39;s an explanation of my question: &amp;nbsp;&lt;a rel="nofollow" target="_new" href="http://jbalfantz.wordpress.com/2010/03/12/problem-with-event-sourcing-reconstituting-our-objects/"&gt;jbalfantz.wordpress.com/.../problem-with-event-sourcing-reconstituting-our-objects&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=677593" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669845</link><pubDate>Wed, 10 Mar 2010 15:29:43 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669845</guid><dc:creator>DM</dc:creator><description>&lt;p&gt;Thanks for the article Greg. I would also like to see the full solution if you get a chance to post it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669845" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669826</link><pubDate>Wed, 10 Mar 2010 12:47:15 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669826</guid><dc:creator>Remi Despres-Smyth</dc:creator><description>&lt;p&gt;Greg,&lt;/p&gt;
&lt;p&gt;Out of curiosity, can you give a scenario or two where you *would* use the state pattern then? &amp;nbsp;Maybe it&amp;#39;s just the type of problem I&amp;#39;ve had to work on, but I can&amp;#39;t see many cases where you wouldn&amp;#39;t need to throw in some states.&lt;/p&gt;
&lt;p&gt;Isn&amp;#39;t that the whole point of the pattern? Use state to help define the valid transitions between states - and if the invalid transitions don&amp;#39;t throw, what do they do?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Remi.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669826" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669805</link><pubDate>Wed, 10 Mar 2010 10:39:26 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669805</guid><dc:creator>Tim</dc:creator><description>&lt;p&gt;&amp;quot;its not like polymorphism would have worked before __anyway__.&amp;quot;&lt;/p&gt;
&lt;p&gt;There is no such word as &amp;#39;anyways&amp;#39;.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669805" width="1" height="1"&gt;</description></item><item><title>dotNet News </title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669740</link><pubDate>Tue, 09 Mar 2010 21:23:15 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669740</guid><dc:creator>Cadred (dotNET)</dc:creator><description>&lt;p&gt;dotNet News&lt;/p&gt;
&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669740" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669732</link><pubDate>Tue, 09 Mar 2010 20:30:03 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669732</guid><dc:creator>Sebastian</dc:creator><description>&lt;p&gt;Hey Greg, &lt;/p&gt;
&lt;p&gt;that&amp;#39;s exactly what I&amp;#39;ve been asked the other day for a job interview. I was asked how to implement a machine of throwing out a chewing gum each time I put a coin in. The traditional response was to implement the State pattern exactly as you mention it (WithCoin) (WithouCoin) and you are right, there are behaviour that doesn&amp;#39;t make sense at all for some state impl.&lt;/p&gt;
&lt;p&gt;What does the method &amp;quot;throwOutChewingGum()&amp;quot; means in a &amp;quot;WithoutCoin&amp;quot; State? Obviously this perfect suites for throwing a runtime &amp;quot;NotImplementedYetException&amp;quot; other than business exception.&lt;/p&gt;
&lt;p&gt;Well, in conclusion I gave roles to actors as ThrowOutMachine.throwOut(ThrowOutMachineContent, Money) where there&amp;#39;re State validations in the &amp;quot;ThrowOutMachine &amp;lt;-&amp;gt; ThrowOutMachineContent&amp;quot; collaboration that replaces the State pattern purpose.&lt;/p&gt;
&lt;p&gt;So I ask myself given your post, is State pattern really a good solution? Should be first look for a collaboration pattern such as Container - Content&lt;/p&gt;
&lt;p&gt;Yeah, ppl always tend to open the GoF book... I don&amp;#39;t know why, there&amp;#39;s always a better solution using tradicional OO.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Sebastian.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669732" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669729</link><pubDate>Tue, 09 Mar 2010 20:16:41 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669729</guid><dc:creator>Calum</dc:creator><description>&lt;p&gt;hi,&lt;/p&gt;
&lt;p&gt;Great article.&lt;/p&gt;
&lt;p&gt;Could you post the full code for the solution? &amp;nbsp;I would like to compare the bad code with the good code as I understand the theory but would like to see how it is done practically.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Calum&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669729" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669727</link><pubDate>Tue, 09 Mar 2010 20:02:56 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669727</guid><dc:creator>Sebastian</dc:creator><description>&lt;p&gt;Hey Greg, &lt;/p&gt;
&lt;p&gt;that&amp;#39;s exactly what I&amp;#39;ve been asked the other day for a job interview. I was asked how to implement a machine of throwing out a chewing gum each time I put a coin in. The traditional response was to implement the State pattern exactly as you mention it (WithCoin) (WithouCoin) and you are right, there are behaviour that doesn&amp;#39;t make sense at all for some state impl.&lt;/p&gt;
&lt;p&gt;What does the method &amp;quot;throwOutChewingGum()&amp;quot; means in a &amp;quot;WithoutCoin&amp;quot; State? Obviously this perfect suites for throwing a runtime &amp;quot;NotImplementedYetException&amp;quot; other than business exception.&lt;/p&gt;
&lt;p&gt;Well, in conclusion I gave roles to actors as ThrowOutMachine.throwOut(ThrowOutMachineContent, Money) where there&amp;#39;re State validations in the &amp;quot;ThrowOutMachine &amp;lt;-&amp;gt; ThrowOutMachineContent&amp;quot; collaboration that replaces the State pattern purpose.&lt;/p&gt;
&lt;p&gt;So I ask myself given your post, is State pattern really a good solution? Should be first look for a collaboration pattern such as Container - Content&lt;/p&gt;
&lt;p&gt;Yeah, ppl always tend to open the GoF book... I don&amp;#39;t know why, there&amp;#39;s always a better solution using tradicional OO.&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Sebastian.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669727" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669705</link><pubDate>Tue, 09 Mar 2010 16:34:52 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669705</guid><dc:creator>Nick</dc:creator><description>&lt;p&gt;So the Ubiquitous Language now speaks of 3 different Application classes as if they are separate? Conceptually, it&amp;#39;s always a single Application, with a unique Id, no? I think it might be clearer to think of it as a single stateful object.&lt;/p&gt;
&lt;p&gt;I agree State is wrongly applied here because in this case, all the implementations that just throw an exception aren&amp;#39;t really valid operations, they should probably never be called.&lt;/p&gt;
&lt;p&gt;In this case, I might rather just keep it to one class and do a check at the top of certain methods, throwing an exception if the method isn&amp;#39;t allowed.&lt;/p&gt;
&lt;p&gt;That way, you could keep it to&lt;/p&gt;
&lt;p&gt;1. retrieve the application by id&lt;/p&gt;
&lt;p&gt;2. perform the operation&lt;/p&gt;
&lt;p&gt;3. save the changes&lt;/p&gt;
&lt;p&gt;If the UI was implemented correctly, those exception throwing operations should never even be called, right?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669705" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669693</link><pubDate>Tue, 09 Mar 2010 15:49:03 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669693</guid><dc:creator>Balaji</dc:creator><description>&lt;p&gt;Canyou finish the example so that we can get the full taste of what you are saying&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669693" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669686</link><pubDate>Tue, 09 Mar 2010 15:11:36 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669686</guid><dc:creator>Phil B</dc:creator><description>&lt;p&gt;In the past I&amp;#39;ve handled this by adding properties such as CanApprove, CanDeny, etc. &amp;nbsp;It changes the client usage pattern so that they must check if an action can be done before attempting to execute it. &amp;nbsp;This takes care of the LSP violation because the client code should expect an exception when it calls a method whose corresponding Can* property is false. &amp;nbsp;I have always gotten a bit of a code smell from this solution though. &amp;nbsp;You make some good points here.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669686" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669672</link><pubDate>Tue, 09 Mar 2010 13:49:52 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669672</guid><dc:creator>Peter</dc:creator><description>&lt;p&gt;I&amp;#39;m glad you included the code in all its gory LSP-violating detail. &amp;nbsp;But I&amp;#39;m not sure I see what you&amp;#39;re proposing as the alternative. &amp;nbsp;Are the three classes derived from a common parent? &amp;nbsp;If not, how are they consumed? &amp;nbsp;What about common data for the application? &amp;nbsp;Do you see an accompanying information holder for that stuff, e.g., LoanApplicationData? &amp;nbsp;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669672" width="1" height="1"&gt;</description></item><item><title>re: State Pattern Misuse</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669663</link><pubDate>Tue, 09 Mar 2010 13:24:11 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669663</guid><dc:creator>Colin Jack</dc:creator><description>&lt;p&gt;&amp;quot;Do some of the data/behavior only make sense in certain states? If so use three separate classes. Going along with this, if we find “throws” in our state implementors we should realize through LSP that we are doing something bad. &amp;quot;&lt;/p&gt;
&lt;p&gt;Definitely agree it can be useful, but I do think its easily overused. Where there are a lot of states I&amp;#39;ve had success using separate classes each of which has 1 or more states.&lt;/p&gt;
&lt;p&gt;On LSP, my view is its a LSP violation if a subclass breaks the contract by throwing an exception, if the exception is part of the contract of the base class then it isn&amp;#39;t.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669663" width="1" height="1"&gt;</description></item><item><title>Interesting Finds: March 9, 2010</title><link>http://codebetter.com/blogs/gregyoung/archive/2010/03/09/state-pattern-misuse.aspx#669652</link><pubDate>Tue, 09 Mar 2010 12:35:37 GMT</pubDate><guid isPermaLink="false">d21fbbc9-c112-4f32-ad14-95939a2c53d4:669652</guid><dc:creator>Jason Haley</dc:creator><description>&lt;p&gt;Interesting Finds: March 9, 2010&lt;/p&gt;
&lt;img src="http://codebetter.com/aggbug.aspx?PostID=669652" width="1" height="1"&gt;</description></item></channel></rss>