<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id7668322">
  <name>State Design Pattern</name>
  <metadata>
  <md:version>1.2</md:version>
  <md:created>2008/06/26 14:54:12 GMT-5</md:created>
  <md:revised>2008/07/22 11:10:55.535 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <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: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: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:email>dxnguyen@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <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: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>
</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 type="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>
    <para id="id6920723">But does the user of the Sky class care about the stratregy?     </para>
    <section id="id-643598580808">
      <name>The State Design Pattern is a fully encapsulated, self-modifying Strategy Design Pattern.</name>
      
      <figure id="id7956658">
        <media type="image/png" src="graphics1.png">
          <param name="height" value="388"/>
          <param name="width" value="925"/>
        </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 <cnxn document="m17227">Null Object Pattern</cnxn>. </para>
      <para id="id7716239">Notice these things about the pattern:</para>
      <list type="enumerated" id="id7908515">
        <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>
