What is the difference between Strategy pattern and Visitor Pattern?

后端 未结 11 1503
灰色年华
灰色年华 2021-01-29 22:18

I have trouble understanding these two design patterns.

Can you please give me contextual information or an example so I can get a clear idea and be able to map the dif

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 22:46

    The visitor is like a one-night stand - you create it when you call the accept function and then they get separated and the visitor can be cleaned from the memory, it doesn't take any room for the class that use it.

    The strategy is like a marriage - you create the object, it lives in the class that uses it, takes memory, has a room and makes itself a coffee in the morning :) . Of course they can get a divorce and switch to another class but that class would also live in its owner's context.

    Hope it helps you remember :)

提交回复
热议问题