0

I have a database-like object that looks like:

static class Data
{ 
     private Dictionary<String,Car> cars;
}

class Car
{
     private string name;
     private Dictionary<String,Part> parts;
}
class Part
{
     private string name;
     private double paramA;
     private int paramB;
     private string paramC;
}

I use this database in a multi-core program, so i only read from it. Currently, reading is implemented thusly: in Data I have:

public double get_paramA(string car,string part)
{
     return cars[car].get_paramA(part);
}

and in Car I have:

public double get_paramA(string part)
{
     return Part[part].get_paramA();
}

This works, but i am worried about the future if there is a need to add more params to Part: in the current scheme it adds 3 functions to three objects.

How should i change the reading-scheme to make the code more readable/maintainable?

Sorry if this is a standard question, i have no idea what to search for to find an answer.

1 Answers1

1

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!

Community
  • 1
  • 1
soutoner
  • 571
  • 2
  • 14