samedi 27 juin 2015

Program design advise

I have to write a program which does some more or less complex operations on sets of lines and surfaces. At first I tried a purely object oriented approach but now this feels a bit like it somehow has become more of an anti-pattern. Since I never really studied computer science except 3 basic courses, my knowledge and experience is very limited. But I would like to find a more elegant way to solve the problem and need some help. I try to elaborate:

This data needs to be handled:

  • simple lines (as a list of vectors)
  • extended lines (as a list of position, normal vectors and maybe more)
  • lists of both line types
  • lists of lists of both line types
  • surfaces
  • all lines and surfaces are stored in a "project" object

Now these have all kinds of procedures that operate on themselves only, like translation and rotation. But they also interact. For example a procedure like "project a line onto a surface with a extended line as a result". This is where the object oriented approach works quite well. But since the data needs to be visually represented, there has to be quite some render logic which was also added to the according classes which made them really heavy.

This is what I actually did (c++ pseudo code):

class SimpleLine {
  protected:
    points
  public:
    access points
    translation
    rotation
    render
    save to file
    ...
};

class ExtendedLine: public SimpleLine {
  protected:
    (points are inherited)
    normal vectors
  public:
    access data
    special translate
    special rotate
    render
    save to file
    ...
};

class SimpleLines: public std::vector<SimpleLine> {
public:
  translate
  project onto surface
  render
  save to file
  ...
};

class ExtendedLines: public std::vector<ExtendedLine> ...

class Project {
  simple lines
  extended lines
  surfaces

  render
  load from file
  save to file
};

Now my question is: How do I do a better separation of the different kind of operations on data and the data itself? Should I follow a more functional approach? Or is this already a acceptable solution and I'm just overthinking it? And: How is a problem like this typically solved, for example in something more complex (like a game)?

Thanks!

Aucun commentaire:

Enregistrer un commentaire