Topic: Class Hierarchy Design


Author: sally@cattell.psych.upenn.edu (Sally Davis)
Date: 7 Dec 1994 02:39:56 GMT
Raw View
Hello,
 I have geometric classes for lines, arcs, bezier curves,
ellipses, etc.  I also have several formats in which I want to
output various coordinates, feedrates, colors, linetype, etc.
For example, I need to output to CAD programs, CNC machines, and
plotters.  So, how would I best design a class hierarchy
that elegantly outputs the information?

Sincerely,
Ben Meyer
sally@psych.upenn.edu




Author: pjl@graceland.att.com (Paul J. Lucas)
Date: Wed, 7 Dec 1994 03:48:26 GMT
Raw View
In <3c379s$d26@netnews.upenn.edu> sally@cattell.psych.upenn.edu (Sally Davis) writes:

> I have geometric classes for lines, arcs, bezier curves,
>ellipses, etc.  I also have several formats in which I want to
>output various coordinates, feedrates, colors, linetype, etc.
>For example, I need to output to CAD programs, CNC machines, and
>plotters.  So, how would I best design a class hierarchy
>that elegantly outputs the information?

 class AbstractRenderEngine {
 public:
  virtual void Line( int x1, int y1, int x2, int y2 ) = 0;
  virtual void Arc( int x, int y, int start, int end ) = 0;
  // ...
 };

 class CAD_RenderEngine : public AbstractRenderEngine {
 public:
  // override all pure virtuals
 };

 class CNC_RenderEngine : public AbstractRenderEngine {
 public:
  // override all pure virtuals
 };

 class Plotter_RenderEngine : public AbstractRenderEngine {
 public:
  // override all pure virtuals
 };

 class Renderer {
 public:
  Renderer( AbstractRenderEngine *e ) : engine( e ) { }
  ~Renderer() { delete engine; }

  Renderer& operator=( AbstractRenderEngine *e ) {
   delete engine;
   engine = e;
   return *this;
  }

  AbstractRenderEngine* operator->() { return engine; }
 private:
  AbstractRenderEngine *engine;
 };

 /*

 This allows you to have a single Renderer class into which any
 RenderEngine cab be "plugged" and can even be changed on the
 fly at run-time.

 */
--
 - Paul J. Lucas
   AT&T Bell Laboratories
   Naperville, IL