Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static class and singleton class desgin pattern? [duplicate]

Possible Duplicate:
Difference between static class and singleton pattern?

i am not able to understabnd the difference b/w static class and singleton class.

in single ton class we make sure we cretae only one object and no more objects are created.

in a the static class also there is no need to create an object we can call the properties and methods directly using the static class name.

here both looks same so whats teh use of using creating single ton class.

any help on this would be great.

like image 959
happysmile Avatar asked Jun 29 '26 12:06

happysmile


1 Answers

In Static class, there is no object. You directly call methods on the static class.

In Singleton, there is an object however, there can only be one instance of it.

Singleton is useful in conditional creation a resources intensive object. For ex, your application might need a connection to remote database. You might want to make it as singleton to limit the number of connections and also to ensure that its only created when required.

Static class and methods are more like utility functions which can be called whenever required.

like image 168
Madhur Ahuja Avatar answered Jul 01 '26 02:07

Madhur Ahuja