title: State design pattern - Passing Objects
body: <p>I have a state design pattern implementation with 3 states:</p>
  
  <pre><code>State1::DoTask(){...}
  
  State2::DoTask(){...}
  
  State3::DoTask(){...}
  </code></pre>
  
  <p>These all 3 states need to use 2 classes already instanced</p>
  
  <p>e.g.</p>
  
  <pre><code>State1::DoTask()
  { 
      sensor-&gt;GetData(...)
      server-&gt;Send(buf,size)
  }
  </code></pre>
  
  <p>How is the best way to pass these 2 class pointer objects to the State1, State2 and State3?</p>
  
  <p>I thought in two options:</p>
  
  <p><strong>1)</strong> The Sensor and Server class could be a singleton:</p>
  
  <pre><code>State1::DoTask()
  {
      sensor = Sensor::GetInstance();
      server = Server::GetInstance();
      ....
  }
  </code></pre>
  
  <p><strong>2)</strong> Passing by using other object to hold the class pointer:</p>
  
  <pre><code>class ComClasses
  {
      Sensor *sensor;
      Server *server;
  }
  
  State1::DoTask(ComClasses *c)
  {
      ....
  }
  </code></pre>
  
  <p>In your opinion, how is the best method? Is there a better solution?</p>
  
  <p>Best regards,</p>