Using a strategy pattern and a command pattern

后端 未结 7 2115
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 03:56

Both design patterns encapsulate an algorithm and decouple implementation details from their calling classes. The only difference I can discern is that the Strategy pattern

7条回答
  •  情话喂你
    2020-12-02 04:12

    The way that I look at it is that you have multiple ways of doing the same thing, each of those is a strategy, and something at runtime determines which strategy gets executed.

    Maybe first try StrategyOne, if the results aren't good enough, try StrategyTwo...

    Commands are bound to distinct things that need to happen like TryToWalkAcrossTheRoomCommand. This command will be fired whenever some object should try to walk across the room, but inside it, it might try StrategyOne and StrategyTwo for trying to walk across the room.

    Mark

提交回复
热议问题