Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One issue I see with your approach is that there would a tight coupling between the concrete <i>Context</i> class and the instances of the Strategy classes. This would imply that the Strategy classes can be used only with the Context class . One way to circumvent this is that to make your strategy classes dependent (or use) an interface that the 'Context' class would implement.</p> <p><b>EDIT</b> Also when the Strategy classes have an instance of Context class, these classes have to get the explicitly data from the Context class. This would mean adding getters (as necessary) in the Context class for strategy classes to get the data they need. But adding getters is not necessarily a good OO practice as more getters pose the risk of breaking the encapsulation. </p> <p>An alternative you can think of is to <b>not</b> pass the reference (this) of Context class to the method(s) in strategy class but to pass only the needed data to the Strategy class. </p> <p>For example if the Context class is something like this: (the code is in Java)</p> <p><code></p> <pre><code>Context { IStrategy strategy; List&lt;Integer&gt; scores; public Context(IStrategy strategy) { this.strategy = strategy; scores = new ArrayList&lt;Integer&gt; } public print() { strategy.sort(scores); } } public interface IStrategy&lt;Integer&gt; { public void sort(List&lt;Integer&gt; l); } </code></pre> <p></code></p> <p>In the above code the Strategy class operates on a generic Integer list and is not particularly bound to be used with the Context class. Also taking further one can use a generic method while defining the Strategy class so that <i>sort</i> method is not just applicable to Integers but also to generic types.</p>
    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. 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.
 

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