Doubleton pattern in C ++

I know a singleton pattern in C ++, but how do you get two instances of an object? Is there such a template where we could easily get two objects?

For the logic I could think of, I can modify the singleton pattern itself to create two objects inside it. This works, but if the requirement grows, as if I needed 3 or only 4 objects, which design pattern would fit this requirement?

+3
source share
9 answers

There is a principle in software development that “there are only three digits: 0, 1 and infinity”, which means that arbitrary restrictions are bad. Sometimes there are good reasons to have only one of the resources (for example, a log file). As soon as you have more than one, any arbitrary limit will eventually become a problem. Therefore, if you need more than one resource or object, you will need a pool that can contain an arbitrary number. You also need to choose how to allocate resources from the pool.

+9
source

, . , : ( , ?)

, .

+7
class myclass {...};

myclass obj1;
myclass obj2;

. , , , .

, .

+6

, .

+1

, , . , , , , (.. ), / / . ( , ), , .

- . , . - , .

, .

+1

?;)

+1

singleton - GoF - . , ; , , .

, - factory Singleton, . , .

, "Modern ++ Design" - .

0

( ) List ( )?

0, 1 ; , , . , , . .

0

If you need multiple instances of the same object, you can use the object pool.

If you need a map of many Singleton objects, you can use the Multiton template.

0
source

Source: https://habr.com/ru/post/1750148/


All Articles