I have a doubt on the best approach to handle a class dependency and I was hoping to have some feedback on the issue.
Let's assume I have a class to model the Earth's ionosphere
class EarthIonosphere {
// Methods that implement physical models of the Earth's ionosphere
}
To do some computations within EarthIonosphere, I might have the need to know the value of the Earth's magnetic model. Therefore I have an independent class for this:
class EarthMagneticField {
// Methods that implement physical models of the Earth's magnetosphere
}
EarthIonosphere might depend on EarthMagneticField (but not the other way round). By the way, EarthMagneticField might also be needed in other parts of the code. With this in mind I was wondering which is the best solution (in terms of re-usability and maintainability) to approach this dependency. The two options I was considering were:
EarthIonospherecontains a private/public member of classEarthMagneticField, but I am concerned on duplication in case the sameEarthMagneticFieldhas to be used in other parts of the code.EarthIonospherecontains astd::shared_ptrthat points to an object of classEarthMagneticFieldthat has been instantiated using thenewoperator. This would allow me to use exactly the same object to compute the magnetic field in other parts of the code.
Do you think any of these is a viable solution in terms of "clean code"? otherwise, do you have any suggestion like some "interface" class or similar?
Many thanks!
Aucun commentaire:
Enregistrer un commentaire