Visitor pattern - adding new ConcreteElement classes is hard?

前端 未结 4 856
孤街浪徒
孤街浪徒 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:23

    This came up in an SO question just recently. To quote myself from this question, and more specifically the discussion

    The reason a precondition of not changing the set of entities (classes you visit) is because it forces you to implement a new VisitXYZ in each concrete visitor. But I never took much stock in that reasoning becasue if you are supporting a persistance visitor, and a text search visitor, and a print visitor, and a validation visitor and you go and add a new entity you are going to want to implement all that functionality anyway. The visitor pattern (with a common base class) just lets the compiler find the ones you forgot to implement for you.

    So yes it is often said that its hard to implement addition conrete elements (or entities), but in my opinion it is hogwash.

提交回复
热议问题