C++: doubts about visitor pattern

前端 未结 5 1569
天命终不由人
天命终不由人 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:32

    There a many possible solutions, but you could do this, for example: Start new hierarchy, which renders Shapes in a specific Context:

    // contracts:
    
    class RenderingContext {
    public: virtual void DrawLine(const Point&, const Point&) = 0; 
        // and so on...
    };
    
    class ShapeRenderer {
    public: virtual void Render(RenderingContext&) = 0;
    };
    
    // implementations:
    
    class RectangleRenderer : public ShapeRenderer {
     Rectangle& mR;
    
    public: 
     virtual void Render(RenderingContext& pContext) {
       pContext.DrawLine(mR.GetLeftLower(), mR.GetRightLower());
       // and so on...
     }
    
     RectangleRenderer(Rectangle& pR) : mR(pR) {}
    };
    

提交回复
热议问题