0

Context:

This is for a library that reads some old games formats representing 3D models.

Let's call these low-level formats PRM1, PRM2, PRM3 and PRM4.

  • each of these formats contains the position, origin and primitives of a 3D model
  • they have minor implementation differences but in the end produce the same type of content
  • currently they all implement an IModel interface from which data is read to produce graphics

Now, I end up with duplicate methods in each of these types, they all do the same thing:

  • private GetMesh(IPolygon, ...)
  • public GetMesh(...) calls above method to produce a 3D model

So I thought, why not ... ?

  • making a Model class that implements IModel directly, it would contain the whole mesh generation process since it's unique, effectively removing duplicate code
  • PRM1, PRM2, PRM3 and PRM4 would then derive from Model and they would only contain the reading from the binary file itself through the Model(Stream stream) constructor entry point

While this sounds the way to go, it feels weird as now low-level types depend on a high-level type.

Question:

Is this a perfectly valid approach/pattern or is there something I am missing ?

  • 1
    Have you considered splitting the data out into a separate 'POCO' model instead of storing the data in the same classes which contain the behaviour to read that data? – Ben Cottrell Jul 13 '17 at 18:18
  • No, actually I adopted this solution since it was the simplest approach I came up with. – Aybe Jul 13 '17 at 18:24
0

I just gave it a try and seems like it was a good move,

  • I stripped out many redundant code
  • locations where to look at are now reduced to a single one
  • concrete types only deal with the reading of data, base class does the transform
  • rendering didn't break
  • worked instantly
  • more generalization
  • etc ...

As a consequence I need some 'builders' to further abstract it but this is a good compromise IMO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.