tags:

views:

161

answers:

5

Hello, everyone.

The diagram

Here's the Minimalist-UML diagram of an app I've been working on. It's supposed to simulate the management of a bunch of sensors relating to different measurements. Please ignore the House class, the diagram is outdated...

However, I have trouble. Each sensor (sound, contact, heat, pressure, gas - All of these inherit from sensor) has an unique ID, starting at 0 for the first one and ending at the total number of sensors - 1.

For good practices' sake, where shall I store the total number of sensors, so the classes I'm using for input/output of files (saving and loading) and insertion of new sensors can access and increment that counter?

Thank you for your time!

A: 

Have a look at the Singleton pattern assuming you don't want to do it with a database of some sort.

Chris Thompson
What's wrong with just a global? Why restrict anything to a single instance? http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/
GMan
As I understand the question, the OP wants an object that can the authority for giving out id numbers. Having more than one instance of that authority would create problems and violate the requirements and it would no longer be a unique id number.
Chris Thompson
Way to complicated for my beginner level. But thanks anyway.
Francisco P.
+5  A: 

Whatever object creates the sensors should assign the identifiers to the sensors.

If multiple objects create sensors, then they should be assigned a pointer or reference to a provider of identifiers when they are created and they should query that provider for a new unique identifier as they create new sensor objects.

James McNellis
Understood. Thank you. Where should I define that "provider of identifiers"?
Francisco P.
@Francisco: Whatever provides the identifiers has to keep track of how many identifiers it has assigned and should know how many it has assigned. I don't see any reason to use a global or singleton in this scenario at all.
James McNellis
+1  A: 

Your unique ID, like a database table ID will likely have some issues.

You will probably find, eventually, that your ID needs to persist across sessions--that your window's ID is used in some other relationship.

You may, some day, also find that it needs to be unique across multiple server/client sets.

I'm just suggesting that you should consider these issues off the bat.

As for where the ID should be generated, since all your "Sensor" classes inherit from one base class, I think I'd generate it via a threadsafe method in that base class--and I'd store it there as well.

Bill K
Please provide more detail if you have the time. How should I define it, etc.! Thank you.
Francisco P.
A: 

what's the problem? do you use a Vector to store your sensors? define a Vector of holding sensor-objects in the house.

can access and increment that counter

you don't have to do this, the Vector does it for you

Oops
No! Do not define a `std::list`! That is the container of *last* resort. Use `std::vector` or `std::deque`, and only if they don't meet your needs should you look at `std::list`.
Billy ONeal
OK, I was speaking more "generally"
Oops
+4  A: 

One option would be to create a static function in your Sensor class that increments and returns a static counter variable. The constructor for Sensor could call this function to get an ID.

// header file
class Sensor
{
...
protected:
   static int getId() { return id++; }
private:
   static int id;
   int myId;
};

// .cpp file
int Sensor::id = 0;

Sensor::Sensor(...)
: myId(getId())
  ...
{}

I'm ignoring threading and persistence issues here. But hopefully this gives you a better idea.

Brian Neal
I see. Would that variable be defined as an attribute of the class?
Francisco P.
@Francisco - I added some code to make it clearer what I meant.
Brian Neal
Thanks! If I code my constructor like that, will each new specific sensor (`heat`, `gas`, etc.) have an unique ID? Or only the base `sensor`s I create?
Francisco P.
@Francisco - Since those other classes derive from `Sensor`, the `Sensor` constructor will be called automatically, so yes, they will get IDs too.
Brian Neal