Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the exact definition of the strategy design pattern?
    primarykey
    data
    text
    <p>I had a geek fight with someone over what the strategy pattern really is and I need a expert to settle the matter.</p> <p>We both agree that the strategy pattern allows for the guts of a class (e.g., the behavior) to be swapped out at runtime while maintaining the same interface. However, her contention is that "For [the algorithms] to be a strategy, You would have to get the same results". My contention is that swapping an "algorithm" or logic of a class could mean that the results of the overridden operation are different, but that it still meets the purpose, intent (and classification) of the strategy pattern.</p> <p>Her code example with comments:</p> <hr> <p>By your definition, any subclasses of a class would be a strategy. They have the same method definitions (signatures), and are therefore interchangeable.</p> <pre><code>Interface Strategy { DoArithmatic(int[] a) } Class A : Strategy public int DoArithmatic(int[]a) { int temp = 0; for(int i =0; i&lt; a.length; i++) temp += a[i] } Class B : Strategy public int DoArithmaticB(int[]a) { int temp = 0; for(int i =a.length -1; i&gt;-1; i--) temp += a[i] } Class C : Strategy public int DoArithmatic(int[]a) { int temp = 0; for(int i =0; i&lt; a.length; i++) temp -= a; } int[] a = { 1,2,3 } ClassA.DoArithmatic(a) = 6 ClassB.DoArithmatic(a) = 6 ClassC.DoArithmatic(a) = -6//This one is not interchangeable </code></pre> <p>The first two are strategies. Because for any input they will give you the EXACT same answer. the last one is not. Just because it gives you an int does not make it a strategy. They have to "DO" the same thing. </p> <p>You can't use a "higher" abstraction term just to make them a strategy.</p> <p>These all do "MATH" but they are not all doing the "same" thing in a different way. That is the essence of a strategy.</p> <p>So, who's right?</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.
 

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