Tuesday, March 10, 2009

State Design Pattern and Schrodinger's Cat

In Programming Without Ifs: Challenge Two, our state diagram modeled that of a stopwatch. We found what varied (the behavior in response to a button click) and abstracted into an interface and named the method 'execute'. However because we wanted to avoid the use of conditional logic to decide what to do depending on which button was selected, we created more states to encapsulate the specific behavior we wanted. For example, when the stopwatch was in the 'RUNNING' state it could transition to either a 'HOLD' state or a 'STOPPED' state depending on whether the HOLD or the START/STOP button was subsequently pressed. Thus, we created two states, RunningToStopped and RunningToHold to represent the respective behaviors. How is this like the famous Schrodinger's Cat problem?

A fundamental property of quantum theory is superposition of states which mathematically expresses a system as a combination of many different states. When we go to take a measurement of the system, we collapse the wave function (expressed as a superposition of many states) into one of the possible states. But before we made the measurement, we have no way of knowing what state the system is in. Schrodinger's Cat was a thought experiment (gedanken) designed to show the strangeness of quantum behavior. I liken the above described application of the State Design Pattern to Schrodinger's Cat for the following reason. When the stopwatch is running it is actually in both the RunningToHold and RunningToStopped states simultaneously and we don't know which one it is until we hit either the 'HOLD' or the 'STOP/START' button. It is only after the button is clicked that the stopwatch assumes one or the other state.

No comments:

Post a Comment