I think the strategy pattern is very similar to the factory method pattern!

Strategy mode:

\

Factory method pattern:

\

\

If you just look at the picture, you don’t see any similarities. But just look at the calling method:

\

Strategy mode:

    Context context = new Context();
    AbstractStrategy strategy;
    // It is up to the caller to decide which policy to use
    strategy = new ConcreteStrategyA();
    context.setStrategy(strategy);
    context.algorithm();
Copy the code

Factory method pattern:

    IFactory factory;
    // Which factory to use depends on the calling method
    factory = new ConcreteFactoryA();
    IProduct product = factory.Create();
Copy the code

The policy pattern has a context class, much like the factory class. In the code that applies both patterns, it is up to the caller to decide which policy to apply or which factory to apply. The policy code and the factory code themselves, in accordance with the open closed principle, change the caller if you want. Of course, this can also be viewed as a disadvantage, since the caller needs to know a lot about policies and factories.

\