State Pattern share common knowledge between state object efficient and extandable implementation
<p>I am using the State Pattern, but the examples I found are for educational purpose and I wanted to know what are best practices in this pattern for <strong>sharing objects between states</strong>, i.e. a boolean, a List and also <strong>for one state object to change the state reference in the automata object</strong>.</p>

<p>I will lay down a simple model to be used as an example.</p>

<pre><code>public abstract class State {

    Automata automata;

    public State( Automata automata){
         this.automata = automata;
    }

    public abstract void action();

}

public class State1 extends State {

    public State1(Automata automata){
         super(automata)
    }

    @Override
    public void action() {
        // GET  : Use obj1 
        // POST :Set in automata state to State2 
    }
} 

public class State2 extends State {

     public State2(Automata automata){
         super(automata)
    }

    @Override
    public void action() {
        // GET  :Use obj1 
        // POST :Set in automata state to State1 
    }
}  

public class Automata {

     private State state1 = new State1(this);
     private State state2 = new State2(this);
     private State state = state1;

     public void takeAction() {
         state.action()
     }
}
</code></pre>

<p>I have tried the following solutions:</p>

<ol>
<li>For <em>GET</em> store <code>obj1</code> in Automata and use getters and setters. For <em>POST</em> store states in Automata and use getters and setters. This approach will make the code unnecessarily long by using the getters and becomes hard to maintain with a changing list of <code>obj1</code>'s and states.</li>
<li>Use private inner classes. Declare <code>State, State1</code> and <code>State2</code> as private inner classes and access directly <code>obj1</code> and states. Hard to maintain and update simply because of length of file. Can not be shared with another <code>Automata</code> class.</li>
<li>Make fields public. I do not want to expose all of this fields.</li>
<li>Use a singleton/static class approach for sharing <code>obj1</code>'s</li>
</ol>

<p>I am not very found of package private access.</p>

<p>In my design I am combining this pattern with the Template Method Pattern as a side information.</p>

<p>I know a <em>one-fits-all</em> approach does not exist, but <strong>what are common best practices in working with this pattern?</strong></p>