What is the exact definition of the strategy desig

2020-06-17 06:05发布

I had a geek fight with someone over what the strategy pattern really is and I need a expert to settle the matter.

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.

Her code example with comments:


By your definition, any subclasses of a class would be a strategy. They have the same method definitions (signatures), and are therefore interchangeable.

Interface Strategy
{
    DoArithmatic(int[] a)
}

Class A : Strategy
public int DoArithmatic(int[]a)
{
     int temp = 0;
     for(int i =0; i< a.length; i++)
          temp += a[i]
}

Class B : Strategy
public int DoArithmaticB(int[]a)
{
     int temp = 0;
     for(int i =a.length -1; i>-1; i--)
          temp += a[i]
}

Class C : Strategy
public int DoArithmatic(int[]a)
{
     int temp = 0;
     for(int i =0; i< 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

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.

You can't use a "higher" abstraction term just to make them a strategy.

These all do "MATH" but they are not all doing the "same" thing in a different way. That is the essence of a strategy.

So, who's right?

9条回答
别忘想泡老子
2楼-- · 2020-06-17 06:38

I think it would be more correct to say that the question of whether the strategies must be deterministically identical is outside the scope of the definition of the strategy pattern.

If a function always returns the same result for given inputs, it is deterministic. If two functions are deterministic and they always return the same value for the same inputs then they are deterministically equivalent. They may or may not have the same side effects; if they do then they are just plain equivalent.

Typically this is not the case. Let us consider an example that appears to require deterministic equivalence: sorting. You might think that if two comparer implementations fail to return the same result for the same inputs then at least one of them must be faulty, but this is not necessarily the case.

Sort orders vary between countries. Some places sort accent-insensitively. Some put McDuck with MacDuck, and so forth. These are strategies, this is a perfect application of strategy pattern, and the strategies are most certainly not deterministically equivalent.

You win.

查看更多
太酷不给撩
3楼-- · 2020-06-17 06:41

You sir are correct and your coworker needs to read the GoF.

"The strategy pattern lets the algorithms vary independently from clients that use them."

See:

http://www.dofactory.com/Patterns/PatternStrategy.aspx

查看更多
甜甜的少女心
4楼-- · 2020-06-17 06:43

You are. The point of the strategy is to substitute the algorithm. Whether they deliver the same result is a by-product of the desired behavior.

查看更多
登录 后发表回答