I am building simulators which simulates the evolution of a body through time. I need to store its position / velocity coordinates for every simulated time steps and sometimes some other data which could be for example the orientation of the body, or any other physical related timestamped data based on simulation time.
I run into a problem which is that the available data that I need to store is often only known by one component of my system, let us say a sensor.
Currently I have implemented a singleton to store the data in order to avoid passing a data structure down from the body down to the sensor. This singleton is used for the body and the sensor (and some other parts of the system):
class Body {
HistoricalData historicalData = new HistoricalData(); // SINGLETON !
Body() {
this.sensor = new Sensor();
}
[...]
public void compute() {
[...]
this.sensor.compute();
this.data.append(currentTime, "variableName", currentVariableValue);
}
}
class Sensor {
HistoricalData historicalData = new HistoricalData(); // SINGLETON !
[...]
public void compute() {
[...]
this.data.append(currentTime, "sensorVariableName", currentSensorVariableValue);
}
}
The problem is that I don't really like singletons (problems if run multiple times, concurrency problems, etc.).
The other approach using a data structure I was thinking is as follow:
class Body {
HistoricalData historicalData = null;
Body(HistoricalData historicalData) {
this.sensor = new Sensor(historicalData);
}
[...]
public void compute() {
[...]
this.data.append(currentTime, "variableName", currentVariableValue);
}
}
The problem with this last approach is that when it is a sub sub component that has information to be stored, well, I need to pass the HistoricalData object from the master component down to the sub sub component, going through all sub components...complexifying constructors signatures.
I was thinking of using a logger system, and then parse logs information to reconstruct my data structure. For example in Java, it would look like:
public class Main {
private static Logger logger = LogManager.getLogger(Main.class);
public static void main(String[] args) throws ParseException {
[...]
body.compute();
logger.info("simlation_time: " + time + " ; subsubsystemvalue: " + currentValue);
[...]
// reconstruction of data from logger
var historicalData = new HistoricalData(logger);
}
Is this a bad practice ? I am wondering if you have another elegant solution...
Aucun commentaire:
Enregistrer un commentaire