• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Singleton class and Class With all its methods as static

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am wondering why to create singleton class in application when same purpose (having single instance) can be served with a Class and all its methods as static, because even in this we donot need to create object to invoke methods .

Can any body put light on this point?
 
Rancher
Posts: 618
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One advantage of a singleton class is that you can combine it with a class factory (and interfaces). By restricting the creation of the singleton to the factory, you can change what instance is actually created when the situation warrants it. For example, let's say you have an application that needs access to a particular business workflow. The first version can be a POJO (plain old Java object). Later on, you need to access the workflow on a separate server through a web service. The only change you will have to make is, in the class factory, change from creating the POJO to creating an instance of an object that calls the web service. Your client will not need changing.

With the class factory pattern, you can even support both versions. For example, if your application must work "online" and "offline", the factory can decide which version to use depending on whether it can establish a connection to the remote server.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic