{"id":25,"date":"2011-03-23T22:29:09","date_gmt":"2011-03-23T22:29:09","guid":{"rendered":"http:\/\/matthewwells.org.uk\/blog\/?p=25"},"modified":"2013-10-14T06:32:17","modified_gmt":"2013-10-14T06:32:17","slug":"one-minute-patterns-gof-state","status":"publish","type":"post","link":"http:\/\/matthewwells.org.uk\/blog\/2011\/03\/one-minute-patterns-gof-state\/","title":{"rendered":"One Minute Patterns GOF State"},"content":{"rendered":"<p>Here&#8217;s the somewhat maligned GOF State Pattern.  This allows us to localise state specific behaviour into a single subclass for each state. The State interface defines the behaviour that each concrete State class must implement. Here&#8217;s a simple example. (We&#8217;re all familiar with traffic lights!)<br \/>\n<a href=\"http:\/\/matthewwells.org.uk\/blog\/2011\/03\/one-minute-patterns-gof-state\/state-pattern-2\/\" rel=\"attachment wp-att-43\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/matthewwells.org.uk\/blog\/wp-content\/uploads\/2011\/03\/State-Pattern1.png\" alt=\"State Pattern Diagram\" title=\"State Pattern\" width=\"567\" height=\"387\" class=\"alignnone size-full wp-image-43\" srcset=\"http:\/\/matthewwells.org.uk\/blog\/wp-content\/uploads\/2011\/03\/State-Pattern1.png 567w, http:\/\/matthewwells.org.uk\/blog\/wp-content\/uploads\/2011\/03\/State-Pattern1-300x204.png 300w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/a><br \/>\nThat&#8217;s it! What a state!<br \/>\nGot more than one minute? <!--more-->OK, lets go into a bit more detail. The context object only ever references a single concrete State object to which it delegates state specific activity.  This ensures that state transitions are atomic.  The transitions between states can be managed by the Context, or (my preference) this control can be delegated to the State objects themselves.<\/p>\n<p>How about some code.<br \/>\nHere&#8217;s the State interface.<\/p>\n<pre class=\"code\">\r\npublic interface TrafficLightState {\r\n  public TrafficLightState transition();\r\n  public Light[] illuminate();\r\n}\r\n<\/pre>\n<p>Here&#8217;s the an example of a concrete State class, representing the Stop state. See how it defines the following state in its transition method. The illuminate method defines the state specific behaviour, in this case which lights should be turned on.<\/p>\n<pre class=\"code\">\r\npublic class StopState implements TrafficLightState {\r\n\r\n  public TrafficLightState transition()  {\r\n    return new ReadyGoState();\r\n  }\r\n  \r\n  public Light[] illuminate() {\r\n      return new Light[]{Light.red, Light.greenWalkingMan};\r\n  }\r\n}\r\n<\/pre>\n<p>And here&#8217;s the context class.<\/p>\n<pre class=\"code\">\r\npublic class TrafficLightContext {\r\n  private TrafficLightState state;\r\n\r\n  public TrafficLightContext()  {\r\n    state = new StopState();\r\n  }\r\n  \r\n  public void run()  {\r\n    for (;;)  {\r\n    try{\r\n      for (Light light : state.illuminate()){\r\n        System.out.print(light.name()+\" \");\r\n      }\r\n      Thread.sleep(2000);\r\n      state = state.transition();\r\n    }\r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n    }\r\n  }\r\n  \r\n  public static void main(String[] args)  {\r\n    TrafficLightContext context = new TrafficLightContext();\r\n    context.run();\r\n  }\r\n}\r\n<\/pre>\n<p>Alternatively, the State objects could be passed a reference to the Context object so that they could access contextual data and modify the Context&#8217;s State at whatever point is appropriate.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the somewhat maligned GOF State Pattern. This allows us to localise state specific behaviour into a single subclass for each state. The State interface defines the behaviour that each concrete State class must implement. Here&#8217;s a simple example. (We&#8217;re all familiar with traffic lights!) That&#8217;s it! What a state! Got more than one minute?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13,7,8,9],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-architecture","category-gof","category-one-minute","category-patterns","category-patterns-one-minute"],"_links":{"self":[{"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":24,"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":53,"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions\/53"}],"wp:attachment":[{"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/matthewwells.org.uk\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}