Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This definitely sounds to be a job for the <a href="http://en.wikipedia.org/wiki/State_pattern" rel="nofollow noreferrer">State Pattern</a></p> <p>Basically you create your application ( context ) and define the behavior for some operation into a separate class/interface ( state ) </p> <p>This class will have different implementing subclasses , one per state.</p> <p>When the state changes, the state object assign the new state to the context. This way you have all the state code into separate classes and avoid the classical spaghetti code. </p> <p><em>EDIT</em></p> <p>Here's the simplest implementation I could think of.</p> <p>Suppose you have a "Pet" A pate does something according to the sate it is into.</p> <p>When that state is complete, the state changes to the next ( being sleep -> wake up -> play etc. ) </p> <p>I don't even know if this compiles. This is only to get the idea*</p> <pre><code>//These are the states of the "Pet" class //sleep -&gt; wake up -&gt; play -&gt; dinner -&gt; sleep -&gt; wake up .. etc. class Pet { State currentState; static Pet createNew(){ Pet p = new Pet(); State s = new Sleep(); s.setContext( p ); p.currentState = return p; } public void doSomething(){ currentState.doSomething(); } } abstract class State { Pet context; void setContext( Pet c ) { this.context = c; } abstract doSomething(); void sout( Striing message ) { System.out.println( message ); } } class Sleep extends State { public void doSomething(){ sout( "zzzZZZ ... " ); State nextState = new WakeUp(); nextState.setContext( context ); context.currentState = nextState; } } class WakeUp extends State { public void doSomething(){ sout( "uuuaaaaww... zzzz What time is it? ... " ); State nextState = new Play(); nextState.setContext( context ); context.currentState = nextState; } } class Play extends State { public void doSomething(){ sout( "boing, boing, poing, poing" ); State nextState = new Dinner(); nextState.setContext( context ); context.currentState = nextState; } } class Dinner extends State { public void doSomething(){ sout( "Yum, yum..." ); State nextState = new Sleep(); nextState.setContext( context ); context.currentState = nextState; } </code></pre> <p>}</p> <p>And then your client class just use it! </p> <pre><code>Pet myPet = Pet.createNew(); while( I_FeelLikePlaying() ){ myPet.doSomething(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload