Singleton vs factory?

I have 3 log classes that all implement the iLog interface:

DatabaseLog
FileLog
ScreenLog

there can be only one instance of them. at first I used one template for each class, but then I thought, why not use a factory to create an instance instead, so I don’t have to create a single template for each of them and for all future journal classes.

and maybe someone will want them as several objects in the future.

so my questions are: should i use factory or singleton pattern here?

+3
source share
5 answers

Logger? , ? - , ?

, , , , Factory . faatory , .

+6

factory . singleton-pattern , . factory -pattern . factory , , , . , .

singleton-pattern , . factory, , .

(, ), factory , (, ). , , .

+3

, - , .

factory, () ILOG

+1

Like others, I also suggest using factory. One advantage when not using Singletons is that you do not have a global state, which makes your code much more verified.

+1
source

I would use a factory here, singleton cannot satisfy your single instance requirements between all three classes.

0
source

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


All Articles