Visitor pattern - adding new ConcreteElement classes is hard?

前端 未结 4 812
孤街浪徒
孤街浪徒 2021-01-27 06:50

I read a book about the visitor pattern. It gives the same class diagram as in the oodesign\'s website.

It says that adding new ConcreteElement classes is hard. But I di

4条回答
  •  情歌与酒
    2021-01-27 07:07

    Essentially, visitor pattern is kind of data manipulator, it will

    • Traverse among elements following some rules
    • Do some calculation and manipulation with the data those elements provide

    In one word, visitor pattern will extend the system functionality, without touch the element class definition.

    But does this mean the visitor class must be revised if new concrete element class is added? It depends, I believe, on how the visitor pattern is designed and implemented.

    If separate all the visit methods of visitor into visit functors, and dynamically bind the them together, then it might be easier when extending the how system, for both visitor and visitee.

    Here is an implementation of visitor pattern I wrote several years ago, the code is a bit old and not well polished, but somehow works :)

    https://github.com/tezheng/visitor

提交回复
热议问题