• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Singleton Pattern Vs All Static Methods

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
Consider this scenario.
1. All the methods in a class are static.
2. We use singleton pattern.
I beleive that in both the cases we achive the same result, that is only one instance of the class.
While i think its better to use case 1, but case 2 is a very popular design pattern. The reasoning is that both achieve the same result, but in case 2 one has to do a extra getInstance() first and then one can call the functions.
Please, share your views. What do you think.
Suresh Bansal
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Singleton design would guarantee just one instance of the class.
A class with only static methods could be instantiated many times (pointless I admit, but possible all the same).
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what you want to achieve.
With the singleton implementation, you can hide the real implementation of your service. The client only knows the contract (API) but does not know about the implementation. This is easily configurable with a bridge or a property file.
You can not do that with a 'static method' implementation.
W.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not post the same question in mutliple forums. Thia thread is already going in OO, Patterns and Design. Go there for further information.
 
Do Re Mi Fa So La Tiny Ad
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic