lundi 20 septembre 2021

Converting between classes representing the same thing in different libraries

I’m working on a project which uses many independent libraries, so they are not somehow connected at all. For example imagine having a simple physics library, and a 2D renderer. Both of those use their own implementation of a Vector2D class. I would like to use instances of the Vector2D class generated by the physics library to work with the renderer. The problem is the classes, despite representing the same thing, are incompactible, in fact they are 2 different classes not knowing anything about each other. I do know how to convert a Vector2D produced by the physics system to a Vector2D class which the renderer uses - you just need to get the values (X, Y) and create a new instance of the other class. What I’m looking for is a more sophisticated solution, ideally a design pattern which solves this while keeping the code clean. I have seen the adapter design pattern, but it’s unnecesarily too complicated and it should be a one-time conversion.

Aucun commentaire:

Enregistrer un commentaire