<?xml version="1.0" encoding="utf-8"?>
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:q="http://cnx.rice.edu/qml/1.0" id="id7668322" module-id="" cnxml-version="0.6">
  <title>State Design Pattern</title>
  <metadata xmlns:md="http://cnx.rice.edu/mdml/0.4">
  <!-- WARNING! The 'metadata' section is read only. Do not edit below.
       Changes to the metadata section in the source will not be saved. -->
  <md:content-id>m17047</md:content-id>
  <md:title>State Design Pattern</md:title>
  <md:version>1.3</md:version>
  <md:created>2008/06/26 14:54:12 GMT-5</md:created>
  <md:revised>2009/07/01 13:47:19.673 GMT-5</md:revised>
  <md:authorlist>
    <md:author id="swong">
        <md:firstname>Stephen</md:firstname>
        <md:surname>Wong</md:surname>
        <md:fullname>Stephen Wong</md:fullname>
        <md:email>swong@rice.edu</md:email>
    </md:author>
    <md:author id="dxnguyen">
        <md:firstname>Dung</md:firstname>
        <md:othername>X.</md:othername>
        <md:surname>Nguyen</md:surname>
        <md:fullname>Dung Nguyen</md:fullname>
        <md:email>dxnguyen@rice.edu</md:email>
    </md:author>
  </md:authorlist>
  <md:maintainerlist>
    <md:maintainer id="swong">
        <md:firstname>Stephen</md:firstname>
        <md:surname>Wong</md:surname>
        <md:fullname>Stephen Wong</md:fullname>
        <md:email>swong@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="dxnguyen">
        <md:firstname>Dung</md:firstname>
        <md:othername>X.</md:othername>
        <md:surname>Nguyen</md:surname>
        <md:fullname>Dung Nguyen</md:fullname>
        <md:email>dxnguyen@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  <md:license href="http://creativecommons.org/licenses/by/2.0/"/>
  <md:licensorlist>
    <md:licensor id="swong">
        <md:firstname>Stephen</md:firstname>
        <md:surname>Wong</md:surname>
        <md:fullname>Stephen Wong</md:fullname>
        <md:email>swong@rice.edu</md:email>
    </md:licensor>
    <md:licensor id="dxnguyen">
        <md:firstname>Dung</md:firstname>
        <md:othername>X.</md:othername>
        <md:surname>Nguyen</md:surname>
        <md:fullname>Dung Nguyen</md:fullname>
        <md:email>dxnguyen@rice.edu</md:email>
    </md:licensor>
  </md:licensorlist>
  <md:keywordlist>
    <md:keyword>design</md:keyword>
    <md:keyword>dynamic reclassification</md:keyword>
    <md:keyword>encapsulation</md:keyword>
    <md:keyword>object oriented</md:keyword>
    <md:keyword>OOD</md:keyword>
    <md:keyword>OOP</md:keyword>
    <md:keyword>pattern</md:keyword>
    <md:keyword>programming</md:keyword>
    <md:keyword>state</md:keyword>
  </md:keywordlist>
  <md:subjectlist>
    <md:subject>Science and Technology</md:subject>
  </md:subjectlist>
  <md:abstract>The State Design Pattern models objects that changes state, i.e. change behavior as a result of what has happened to them.   This is also called "dynamic reclassification".</md:abstract>
  <md:language>en</md:language>
  <!-- WARNING! The 'metadata' section is read only. Do not edit above.
       Changes to the metadata section in the source will not be saved. -->
</metadata>

<content>
    <para id="id6390313">Objects are often discussed in terms of having a "state" that describes their exact conditions in a given time, based upon the values of their properties. The particular values of the properties affect the object's behavior. For instance, one can say that the exact behavior of an object's <code>getColor()</code> method is different if the "color" property of the given object is set to "blue" instead of "red" because <code>getColor() </code>returns a different value in the two situations.</para>
    <para id="id7893135">Furthermore, the object may make decisions at run time as to exactly what to do dependent upon the values its properties possess. For instance, if the sky is blue <code>(sky.setColor(Color.blue))</code>, then the sun should be visible.</para>
    <para id="id8291824">
      <code id="id3075733" display="block">
public boolean sunIsVisible() {
    if(getColor()==Color.blue) {
        return true;
    }
    else {
        return false;
    }
}</code>
    </para>
    <para id="id7093542">One issue with the above solution is that it is a hard-coded logic solution, not an architected solution. The sky does not intrinsically behave a certain way if it is blue, but rather it should figure out what to do in that situation.</para>
    <para id="id7961263">Wouldn't it be better if the sky intrinsically acted properly if it were blue? One could imagine two objects: a <code>SkyBlue</code> and a <code>SkyNonBlue</code>. The <code>SkyBlue</code> class' <code>sunIsVisible()</code> method would always return true while the <code>SkyNonBlue </code>version would always return false.</para>
    <para id="id6725492">What one needs now is the ability for a sky object to dynamically (i.e. at run time) change its class to/from <code>SkyBlue</code> and <code>SkyNonBlue</code>. What we'd like to accomplish is called "<emphasis>dynamic reclassification</emphasis>". </para>
    <para id="id7885517">We've seen code that does change its specific behavior depending on what particular strategy was installed. So, the <code>setColor()</code> method could install a strategy that would always return true if its <code>sunIsVisible() </code>method were to be called.</para>
    <exercise id="eip-521"><problem id="eip-631">
		<para id="eip-911">
    But does the user of the Sky class care about the stratregy?
  </para>
	</problem>
	<solution id="eip-269">
		<para id="eip-531">
    Of course not. The user only cares that it does its job.
  </para>
	</solution>
</exercise>
    <section id="id-643598580808">
      <title>The State Design Pattern is a fully encapsulated, self-modifying Strategy Design Pattern.</title>
      
      <figure id="id7956658"><media id="id2378047" alt=""><image src="graphics1.png" mime-type="image/png" height="372" width="888"/></media>
	
<caption>UML Class Diagram of the State Design Pattern</caption></figure>
      <para id="id8223411">One design pattern that is used very often in conjunction with the state pattern is the <link document="m17227">Null Object Pattern</link>. </para>
      <para id="id7716239">Notice these things about the pattern:</para>
      <list id="id7908515" list-type="enumerated">
        <item>Any methods whose behaviors depend on the state of the object are simply delegated on in to the state, and handled there. Thus you will see the same methods in the context as in the states. Since the states are separate objects from the context, all the properties of the context need to have accessor methods that are at least package visible. </item>
        <item>The "<emphasis>Context</emphasis>" object needs to add a "<emphasis>set</emphasis>" accessor method so the states can modify which state is the active state. This method would be package visible so as to encapsulate the behavior away from the sight of the user.</item>
      </list>
    </section>
  </content>
</document>
