Hey hey, you don't know how to implement this strategy. It seems that it is not very difficult to learn the design model.
Rule Mode
Official explanation: strategy is an object behavior pattern in design pattern, mainly defining a seriesAlgorithmAnd encapsulate these algorithms into separate classes.
We have achieved the goal of freely switching algorithms during operation.
In reality, the core part of strategy is the use of abstract classes. The strategy mode can be used with a small amount of modifications and fast modification when users need to change.
There are some similarities between strategy and factory. Strategy is relatively simple and easy to understand, and can switch freely at runtime. Factory focuses on creating objects.
Strategy is applicable to the following scenarios:
1. save files in different formats;
2. Use different algorithms to compress files;
3. capture images using different algorithms;
4. output images of the same data in different formats, such as curves or Block Bar
My personal understanding: It's so handsome ...... In fact, it seems that it is not very complicated, that is, to abstract the behavior (algorithm) of the object, and then assemble it with a class when other objects are used, so that specific objects can use their favorite methods to make love.
Finally, let's go to the previous example, "duck story", first:
Click to download source code