Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can solve both problems by using <a href="https://wiki.engr.illinois.edu/download/attachments/220441849/double-dispatch.pdf?version=1&amp;modificationDate=1359481987000" rel="nofollow" title="Double Dispatch">Double Dispatch</a> between the state instances and the <code>superAgent</code> instance to avoid breaking encapsulation.</p> <p>Say you already implemented the State Pattern. <code>instructionResponseRecievedFromAgent</code> would look like:</p> <pre><code>public void instructionResponseRecievedFromAgent() { state.instructionResponseRecievedFromAgent(); } </code></pre> <p>Each <code>State</code> implements <code>instructionResponseRecievedFromAgent</code> using double dispatch this way: </p> <pre><code>abstract class State { abstract void instructionResponseRecievedFromAgent(); } class State1 extends State { void instructionResponseRecievedFromAgent() { // instance variable agent.instructionResponseRecievedFromAgentFromState1(); } } class State1 extends State { void instructionResponseRecievedFromAgent() { // instance variable agent.instructionResponseRecievedFromAgentFromState2(); } } </code></pre> <p>Doing this you let the each <code>State</code> specify <strong>what</strong> to do, but is the <code>superAgent</code> instance who decides <strong>how</strong> to do it. You have full access to the state and locks without making them public. </p> <p>Finally, you implement both <code>instructionResponseRecievedFromAgentFromState1</code> and <code>instructionResponseRecievedFromAgentFromState2</code> in <code>superAgent</code>:</p> <pre><code>public void instructionResponseRecievedFromAgentFromState1() { //this is doState1Behavior original implementation synchronized(instructionLock) { //make state specific modifications to instructionData } } public void instructionResponseRecievedFromAgentFromState2() { //this is doState1Behavior original implementation synchronized(instructionLock) { //make state specific modifications to instructionData } } </code></pre> <p>Keep in mind that, event though <code>instructionResponseRecievedFromAgentFromState1</code> and <code>instructionResponseRecievedFromAgentFromState2</code> are public methods (so the <code>State</code> instances can call them), they're only meant to be used only in the context of the State Pattern.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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