C++: doubts about visitor pattern

前端 未结 5 1656
天命终不由人
天命终不由人 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条回答
  •  萌比男神i
    2021-02-08 21:35

    This doesn't look like a case for the Visitor pattern to me.

    I would suggest you have a RenderableShape class that aggregates a Shape object, then create subclasses for each shape. RenderableShape would have a virtual render method.

    If you want to support multiple rendering engines, you can have a RenderContext base class that abstracts drawing operations, with subclasses for each rendering engine, each subclass implementing the drawing operations in terms of its rendering engine. You then have RenderableShape::render take a RenderContext as an argument, and draw to it using its abstracted API.

提交回复
热议问题