CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Ranjan Sakalley


Practical examples of where to use Static/ Singleton instances

Recent questions in one of the newsgroups, regarding practical examples of Static instances. I’m sure there are many ideas out there, here is my take

Normally you would need to create a static instance when the secondary ( the primary ofcourse is the busines logic) need is -
 
 1. Keeping an instance which holds the state of the application, while all other objects around this one in the application can live and die at will ( eg. the Request/ Application objects in ASP.Net  which are static.)
 
 2. To remove the overhead of creating  and collecting objects which are more or less expected to be stateless ( consider the case of a remoting server/webservice providing math functions, totally stateless, and hence the service should mask a singleton/static instance that provides the services , thus avoiding the creation and collection of the objects created, one each for each call.)

 The question sometimes raised is whether singleton is really better than static. Situational, is it?


Published Apr 03 2005, 09:19 PM by rsakalley
Filed under:

Comments

Raymond Lewallen said:

That is exactly what I use them for. I use a singleton pattern for state management and use static/shared in places where the object is stateless. I find myself using modules and statics much more often than implementing singletons, as I'm sure most people do. Situational, indeed.
# April 4, 2005 6:40 AM

Jeremy MIller said:

I agree with number 1 completely. Also add in cases where it is simply more convenient to "get" to a central object from a Singleton instead of passing it down through several levels. The IPrincipal on the thread or some sort of "ApplicationController" in a heavy UI client are examples.

I would strongly disagree with parts of number 2 on testability grounds. I would never advise making any kind of web service call from a static method because you are too tightly coupled to the web service. You cannot exercise your code with a mock object or a stub in unit tests if you call through a static.

Here's some stuff on ways to make Singleton's testable -- http://structuremap.sourceforge.net/SingletonInjection.htm
# April 29, 2005 11:20 AM

rsakalley said:

Jeremy,
What I meant by 2 was that a webservice should mask a singleton object that provides the stateless functionalities. I think this is pretty easily testable. Please tell me if there are any problems with that. I never meant to say that a static method should make calls to a webservice. Sorry if my statement lead to that misconception.
# April 29, 2005 1:11 PM

NeVaL said:

http://www.l-oreal.beibi.info ^^^ http://www.l-porn.beibi.info ^^^ http://www.gutter-striptease-porno.biseksuell.info ^^^ http://www.penis-rampete-video.biseksuell.info ^^^ http://www.gutter-striptease-porno.erotiska.info ^^^ http://www.penis-rampete-video.erotiska.info ^^^ http://www.anal-brutal-bilder.fitta69.info ^^^ http://www.erotiska-novell-mp3.fitta69.info ^^^ http://www.porr-svensk-bmp.fotsex.info ^^^ http://www.bild-kuk-urinerar.fotsex.info ^^^ http://www.ystavallinen-neito.isomuna.info ^^^ http://www.vittu-jente-homo.isomuna.info ^^^ http://www.korea-poika.laukeaminen.info ^^^ http://www.tanssi-perverssi-penis.laukeaminen.info ^^^ http://www.foto-naturlig-virgin.rype.info ^^^ http://www.hetest-masturbering-gallerier.rype.info ^^^ http://www.stripping-rav.sadsprut.info ^^^ http://www.portretter-rype-billig.sadsprut.info ^^^ http://www.otos-vittu-smek.tytsy.info ^^^ http://www.kela-jumalallinen-tytot.tytsy.info ^^^ http://www.photos-ass-mpgs.18analsex.com ^^^ http://www.thai-ass-xxx.18analsex.com ^^^ http://www.gif-umidificatore-caldo.pazzesesso.com ^^^ http://www.mpgs-patrizia-meloni.pazzesesso.com ^^^ http://www.avi-hombre-peludos.figanere.com ^^^ http://www.film-bellissima-federica.figanere.com ^^^ http://www.froci-gratis-clip.inculatexxx.com ^^^ http://www.clip-calza-piede.inculatexxx.com ^^^ http://www.filmato-minchia-piccola.prostitutaculo.com ^^^ http://www.video-clisteri-xxx.prostitutaculo.com ^^^ http://www.marino-porcona-immagini.lesbicastrip.com ^^^ http://www.giovane-arrapato-immagine.lesbicastrip.com ^^^ http://www.brasil-compras-clips.007sexogratis.com ^^^ http://www.maria-jesus.007sexogratis.com ^^^ http://www.avi-cocina-paso.3sexogratis.com ^^^ http://www.clips-sexo-famosas.3sexogratis.com ^^^ http://www.arte-teta-mujer.analsexogratis.com ^^^ http://www.trailer-pornostar-peruana.analsexogratis.com ^^^ http://www.tetas-porn.cam-sexo-gratis.com ^^^ http://www.mpeg-militar-gay.cam-sexo-gratis.com ^^^

# December 28, 2006 5:56 AM

Smułko said:

http://prenotazione-hotel-marzio.u4re8o6n1qf.info/

http://148691768.zp9as3i9llc.info/

http://buono-asiatiche-strip.3wcekxwg3md.info/

http://foto-donne-pelose-che-fanno-pipi.q88p8bre85g.info/

http://nel-sex-succhione.dewo9m907by.info/

http://roma-immobiliare.5evij5hitsj.info/

http://hidden-camera-video.5evij5hitsj.info/

http://libri-usati-antiquario-rari.cn3u6te3gqn.info/

http://ragazza-cerca-amore.nmlnrjuk87j.info/

http://amsterdam-camera.x7lo2fujz5l.info/

http://bionde-fotti-nell-appartamento.crlawhxqgwi.info/

http://www.2qhtadoaxcg.info/e-brunello-montalcino.html

http://www.kmxb954ry0e.info/daniela-lumbroso-paparazzi.html

http://capricciose-foto.79pkaqzrpg2.info/

http://www.dhffimsbmup.info/mostra-culi-donne.html

http://prestito-gallicano-lazio.37of3kjgtmf.info/

http://www.hgmtpx1eyo1.info/vergognoso-agente-di-polizia-prostituta.html

http://www.zp9as3i9llc.info/vacanza-natura-dolomiti.html

http://duplicazione-genetica.kmxb954ry0e.info/http://www.dmdaa.org.es/freddissimo-urinate.html

http://piu-bollente-schioccare-madre.vtya.com.es/

http://6i4bvc2.subli.org.es/

http://www.neila.org.es/appalto-regione.html

http://www.ducis.nom.es/comprensivo-agente-di-polizia-figa-fotti.html

http://144951444.bibbo.com.es/

http://www.bomet.nom.es/cute-fighetta-gruppo.html

http://www.iolao.nom.es/152635028/

http://www.dmdaa.org.es/senssex-asiatiche-merda.html

http://danno-cocaina.dfms.org.es/

http://www.aegon.nom.es/scopata-casalinghe.html

http://bdsm.vidot.com.es/

http://www.bomet.nom.es/lotta-sexy-femminile.html

http://www.sbmr.org.es/148107728/

http://caldo-brutta.worri.org.es/

http://www.bisg.org.es/freddo-famoso-figlio.html

http://b42ial38m.cdoc.nom.es/

http://annuncio-incontri-sexi.worri.org.es/

# January 24, 2007 1:04 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!