Using a strategy pattern and a command pattern

后端 未结 7 2094
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  Happy的楠姐
    2020-12-02 04:12

    Strategies encapsulate algorithms. Commands separate the sender from the receiver of a request, they turn a request into an object.

    If it's an algorithm, how something will be done, use a Strategy. If you need to separate the call of a method from its execution use a Command. Commands are often used when you queue up messages for later use, like a task or a transaction.

提交回复
热议问题