This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there any difference between singleton and static class?

 
Thennam Pandian
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume that we have a class with full of static methods. To access this methods we don't need to create a object.

Is there any difference between singleton object and this class?
which is better and how?
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton isn't about not creating an instance, Singleton makes sure that there is only one instance of a class (like a FileSystemManager object for example).
A class with only static methods is usually called a "Utility class".
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Todd wrote:Singleton isn't about not creating an instance, Singleton makes sure that there is only one instance of a class (like a FileSystemManager object for example).
A class with only static methods is usually called a "Utility class".


John,

Nice explanation. Concise & to the point.

Thennam,

Singleton is one of simplest OO pattern to explain but one of the rather difficult one to implement (depending on your system, class loaders etc).

As far as comparison goes as John said, it is apples vs oranges, they have completely different tasks in a system.

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While there are usually many more efficient ways to design OO applications without using the Simpleton design pattern, the pattern does have considerable benefits when compared to class operations, e.g. more flexibility.
 
Remko Strating
Ranch Hand
Posts: 893
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply because if you use an interface for your class you need an instance which implements that interface. This is for me the main reason for using the singleton pattern. I have some utility classes but I don't want the rest of my code to know the implementation details but I don't want the create lot of objects. So I use the singleton pattern for generating 1 object which implements the interface.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic