Modifying a singleton design pattern

Hello,
I have a program currently working which is doing the communication with a hardware module.
This program has been written using a singleton design pattern, since it was assumed that only one hardware module would be controlled at the same time.
Now I want to modify this program in order to enable it to communicate with several modules.
I have an idea about how to do it, but I'm afraid I would have to modify almost all the code (which represents a quite big work, since the program is consequent), or to program something very dirty. Could you give me some advises on how to do that properly ?

At the moment I have a top-level class, which has among others a handle to the hardware as a parameter, and a getInstance method implementing the single design pattern:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class TopLevel
{

private:
TopLevel(){}
handler DeviceHandler;
...
...
public:
&TopLevel getInstance(){
static TopLevel instance;
return instance;
}
...
...
}


I thought about modifying the method getInstance to implement a multiton design pattern, thus storing a vector of instances instead of a single instance. then the method would take i as a parameter, which would be the number of the instance. Every time the function would be called with an i corresponding to an instance which doesn't exist, the instance would be created with a "new" operator and added to the vector.
After that I would modify every function using the getInstance() method to make it use getInstance(int i).

Is that the right way to do it ? I would appreciate any review, since I prefere not to modify everything before I get it's a bad pattern. Thanks
Last edited on
Another solution would be to completely remove this getInstance method, make the explicit creator TopLevel() public, and then change all the methods in my program using getInstance. Do you think that would be better?
Topic archived. No new replies allowed.