Yasser1984
Yasser1984

Reputation: 2441

Is keeping a class in singleton faster than instantiating a new object of that class?

In php,

I have heard that too much singleton is not really a good idea.

If I have classes that I have to instantiate over and over in different parts of my code does it not make sense to keep the objects available after the first initiation as compared to redefining them every time?

I guess the answer to this partially depends on how heavy the constructors of those classes are, so let's say we have scenario A and B, in scenario A the constructors do some heavy processing, and it takes like 2 seconds to run the constructor only, and in scenario B the constructors have minimal or no code at all.

In these two scenarios, does singleton make sense?

Feel free to elaborate more on your answers, I'm trying to see the down side of singleton.

Upvotes: 2

Views: 200

Answers (1)

Sgn.
Sgn.

Reputation: 1696

Writing an OO code depends on your design. Using or not using singleton classes is not an exception. Singleton is useful when exactly one object is needed to coordinate actions across the system.

Upvotes: 1

Related Questions