C++: doubts about visitor pattern

前端 未结 5 1570
天命终不由人
天命终不由人 2021-02-08 21:23

I know what Visitor Pattern is and how to use it; this question is not a duplicate of this one.


I\'ve got a library where I put most of the reusable code I write,

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-08 21:26

    So there's a class xxxShape, which in some way contains information that "drives" the rendering. For Circles that might be centre, radius, for Squares some corner coordinates or some such. Maybe some other stuff about fillings and colours.

    You dont't want to/cannot update those classes to add the actual rendering logic, and I think your reasons for not doing so are valid/inevitable.

    But presumably, you have enough public access methods on the classes to allow you to get at the "driving" information, otherwise you are doomed.

    So in which case why can you not just wrap these items:

     CircleRenderer hasA Cicle, knows how to render Circles
    

    and so on. Now use the Visitor pattern across the Renderer classes.

提交回复
热议问题