Singleton / static classes for services

I have an application that has some classes that handle certain specific functions, have a lifetime equal to the application itself, and are intended for use in many parts of the program. For this last reason, I call them "Services." For example, the Audio Service plays audio files and does many other things related to audio.

These classes are created only once at application launch, and it makes no sense to have more than one type.

Since I read many answers about singles here on SO, where their use is discouraged, I continued by passing a link to thoose services when needed. As the project grows, I find myself with many classes that need a service link to their constructor, and in some cases even a facade of these services, so as not to add all the links to the services.

I think I'm doing it wrong. I think this should be useful for static / single classes.

Is this the right approach?

+5
source share
4 answers

I see one answer suggesting introducing Spring. Under the covers of Spring, this link is still passed where it is needed.

, , Singleton? , , .

Singletons , Injection of Dependency (, ), .

+1

, . Java, Spring .

+2

Singleton , . . , , html. HTML "". , , . html X. , , . , , , , , , , , .

0
source

I agree with MikeG. Use singleton. Since you are saying that these classes are mostly service oriented, I would not expect this to cause any problems.

0
source

All Articles