I think the best pattern that fits this case is the Singleton pattern
.
In this case you will apply this to your Data
class, so it acts as a "database-object" as you're correctly stating. Data
class will never change (it's only instantiated once) nor his reference to the dictionary.
As you need it to be thread safe (although you're only reading, maybe in the future you will need to modify it) I will base my implementation in this answer by Bohemian where things are better explained than here (sorry, I'm not very good at synchronization hahah).
public class Data {
private Dictionary<String, Car> cars = new ...<String, Car>();
private Data() {}
private static class Holder {
static final Data INSTANCE = new Data();
}
public static Data getInstance() {
return Holder.INSTANCE;
}
public double get_paramA(string car,string part){
return cars[car].get_paramA(part);
}
}
Note that if you need to access to the dictionary or the get_paramA()
method you have to call it in this way:
double part = Data.getInstance().get_paramA(car1, part1);
So you ensure you're always dealing with the same object: the first time that you call it, it will create the object, and the rest of times, it will return the created object hold by the inner class.
About the last question of adding later a new param, you're doing it correctly, because of the OOP paradigm, each object need to "do his job" that is, return and request the necessary methods of other objects (so you will end with a lot of "dumb" methods that return a private parameter or calls to the same method of another object).
I don't know very well what are you modeling with that Part
class, but as to me it sounds generic, maybe using inheritance
will be ok. You can define your Part
class as an Abstract Class
where you define the common methods to all the possible Part
objects that you can have, and then you can extend
from this class and create other classes like Whell
or Chasis
where they have their own instance variables.
If you need more info don't hesitate to ask again and sorry if I'm saying something that is not correct at all.
Greetings!