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条回答
Explosion°爆炸
2楼-- · 2020-06-17 06:23

Technically, strategies can do whatever they want.

It is only when the "outer context" dictates some fixed and repeatable behaviour that cannot be captured in the programmatical interface (call them "desirable properties"), that you need to take care that your strategies are truly substitutable à la Liskov with respect to these desirable properties.

查看更多
Animai°情兽
3楼-- · 2020-06-17 06:24

FWIW, the Wikipedia article agrees with you and has never heard of her position.

查看更多
4楼-- · 2020-06-17 06:24

I also have to agree. A good example would be a pricing calculator strategy. You could have different strategies for calculating the final amount of an invoice depending on several variables like quantity of items, type of customer, shipping destination, etc. Each of those strategies would definitely be expected to return a different result and it would still be considered a Strategy pattern.

查看更多
闹够了就滚
5楼-- · 2020-06-17 06:26

According to "Head first Design Patterns" (see here) page 24

"The Strategy Pattern defines a family of algorithms,
encapsulates each one, and makes them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it. "

so sir you are correct, at least according to the people who defined the pattern, but what do they know.

查看更多
Lonely孤独者°
6楼-- · 2020-06-17 06:34

The first two are strategies. Becuase for any input they will give you the EXACT same answer. the last one is not. Just becuase it gives you an int does not make it a strategy. They have to "DO" the same thing.

They have to do the same thing, but that doesn't mean they give the exact same result. The motivating example from the GoF is one of different layout algorithms, or different register allocation algorithms. The strategies have the same goal - layout blocks of text and images on a page, or assigning virtual registers to hardware registers - but they don't have to create exactly the same result.

So if the goal of the Strategy in your example is to do any arithmetic with the input, then each example is a strategy for that goal. If the goal was to sum the array it is passed, DoArithmatic would have been called CalculateSum, and the final example would fail to conform to the contract of the strategy, and so violate LSP.

查看更多
Viruses.
7楼-- · 2020-06-17 06:36

I support your opinion. Different strategies can do very different things as long as they can be used in the same context.

For example, if you want to visit each node in a tree, valid strategies could be:

  • Depths-first search pre-order
  • DFS post-order
  • BFS
  • randomized
  • ...

All strategies would visit the nodes in a different order, yet the objective (visiting each node) would be the same. So if the order does not matter, either strategy fits your needs.

查看更多
登录 后发表回答