title: How would you code a washing machine?
body: <p>Imagine I have a class that represents a simple washing machine. It can perform following operations in the following order: <strong>turn on -> wash -> centrifuge -> turn off</strong>. I see two basic alternatives:</p>
  
  <ol>
  <li><p>I can have a class WashingMachine with methods <strong>turnOn(), wash(int minutes), centrifuge(int revs), turnOff().</strong> The problem with this is that the interface says nothing about the correct order of operations. I can at best throw InvalidOprationException if the client tries to centrifuge before machine was turned on. I can also use a separete Program class that will pass centrifuge revs and wash minutes to the WashingMachine and will simplify these methods.</p></li>
  <li><p>I can let the class itself take care of correct transitions and have the single method <strong>nextOperation()</strong>. The problem with this on the other hand, is that the semantics is poor. Client will not know what will happen when he calls the nextOperation(). Imagine you implement the centrifuge button’s click event so it calls nextOperation(). User presses the centrifuge button after machine was turned on and ups! machine starts to wash. I will probably need a few properties on my class to parameterize operations, or maybe a separate Program class with washLength and centrifugeRevs fields, but that is not really the problem.</p></li>
  </ol>
  
  <p>Which alternative is better? Or maybe there are some other, better alternatives that I missed to describe? </p>