Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Strategies are higher-level things that are used to change how things are computed. With dependency injection, you can change not just how things are computed but also change what is there.</p> <p>For me, it becomes clear when using unit tests. For production code execution, you have all the data hidden (i.e. private or protected); whereas, with unit tests, most of the data is public so I can look at it with the Asserts.</p> <hr> <p>Example of strategy:</p> <pre><code>public class Cosine { private CalcStrategy strat; // Constructor - strategy passed in as a type of DI public Cosine(CalcStrategy s) { strat = s; } } public abstract class CalcStrategy { public double goFigure(double angle); } public class RadianStrategy extends CalcStrategy { public double goFigure(double angle) { return (...); } } public class DegreeStrategy extends CalcStrategy { public double goFigure(double angle) { return (...); } } </code></pre> <p>Notice that there is no public data that is different between the strategies. Nor is there any different methods. Both strategies share all the same functions and signatures.</p> <hr> <p>Now for the dependency injection:</p> <pre><code>public class Cosine { private Calc strat; // Constructor - Dependency Injection. public Cosine(Calc s) { strat = s; } } public class Calc { private int numPasses = 0; private double total = 0; private double intermediate = 0; public double goFigure(double angle) { return(...); } public class CalcTestDouble extends Calc { // NOTICE THE PUBLIC DATA. public int numPasses = 0; public double total = 0; public double intermediate = 0; public double goFigure(double angle) { return (...); } } </code></pre> <p>Use:</p> <pre><code>public CosineTest { @Test public void testGoFigure() { // Setup CalcTestDouble calc = new CalcTestDouble(); Cosine instance = new Cosine(calc); // Exercise double actualAnswer = instance.goFigure(0.0); // Verify double tolerance = ...; double expectedAnswer = ...; assertEquals("GoFigure didn't work!", expectedAnswer, actualAnswer, tolerance); int expectedNumPasses = ...; assertEquals("GoFigure had wrong number passes!", expectedNumPasses, calc.numPasses); double expectedIntermediate = ...; assertEquals("GoFigure had wrong intermediate values!", expectedIntermediate, calc.intermediate, tolerance); } } </code></pre> <p>Notice the last 2 checks. They used the public data in the test double that was injected into the class under test. I couldn't do this with production code because of the data hiding principle. I didn't want to have special purpose testing code inserted in the production code. The public data had to be in a different class.</p> <p>The test double was injected. That is different than just a strategy since it affected <strong>data</strong> and not just functions.</p>
 

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